十四、企業實戰案例 (一)使用for迴圈在/oldboy目錄下批量創建10個html文件,其中文件名包含10個隨機小寫字母加固定字元串0ldoby。 思路分析,產生隨機數字方法: ①核心是創建10個隨機小寫字母。 應用實例 shell [root@centos6 kvm3 shili] cat 14 ...
十四、企業實戰案例
(一)使用for迴圈在/oldboy目錄下批量創建10個html文件,其中文件名包含10個隨機小寫字母加固定字元串0ldoby。
思路分析,產生隨機數字方法:
①核心是創建10個隨機小寫字母。
方法1:
echo $RANDOM 範圍是0-32767
openssl rand -base64 100
方法2:
date +%s%N
方法3:
head /dev/urandom/cksum
方法4:
uuidgen
方法5:
cat /proc/sys/kernel/random/uuid
方法6:
mkpasswd (yum install expect -y)
-l:長度
-d:數字
-c:小寫字母
-C:大寫字母
-s:特殊字元
應用實例
[root@centos6-kvm3 shili]# cat 14-01.sh
#!/bin/bash
path="oldboy"
[ -d /path ] || mkdir -p oldboy/
for n in {1..10}
do
random=`echo "OLDBOY$RANDOM" | md5sum | tr '0-9' 'm-z' | cut -c 2-11`
touch $path/${random}_oldboy.html
done
[root@centos6-kvm3 shili]#
(二) 、批量改名特殊案例。將以上面試題1中結果 文件名中的oldboy字元串全部改成oldgirl(最好用for迴圈實現),並且將擴展名html全部改成大寫。
方法1
[root@centos6-kvm3 shili]# cat 14-02.sh
#!/bin/bash
for file in `ls oldboy/*.html`
do
mv $file ${file/oldboy.html/oldgirl.HTML}
done
[root@centos6-kvm3 shili]#
方法2
[root@centos6-kvm3 oldboy]# ls | awk -F "oldgirl.HTML" '{print "mv",$0, $1 "oldboy.html"}' | bash
方法3
[root@centos6-kvm3 oldboy]# rename "oldboy.html" "oldgirl.HTML" *.html
(三)、企業shell面試題3:批量創建特殊要求用戶案例。
批量創建10個系統賬號oldboy01-oldboy10並設置密碼(密碼為隨機數,要求數字和字母混合)。
方法1
[root@centos6-kvm3 shili]# cat 14-03.sh
#!/bin/bash
. /etc/init.d/functions
if [ $UID -ne 0 ]
then
echo "please use root."
exit 1
fi
for n in {41..50}
do
pass=`openssl rand -base64 10`
if [ `grep -w "oldboy$n" /etc/passwd|wc -l` -eq 0 ]
then
useradd oldboy$n &>/dev/null &&\
echo $pass | passwd --stdin oldboy$n &&\
echo -e "oldboy$n\t$pass" >>/tmp/user.txt &&\
action "oldboy$n is successful." /bin/true
else
action "oldboy$n is exist." /bin/false
fi
done
[root@centos6-kvm3 shili]#
方法2
[root@centos6-kvm3 shili]# cat 14-03-01.sh
#!/bin/bash
for n in `seq -w 11 20`
do
pass=`openssl rand -base64 10`
useradd oldboy$n
echo "oldboy$n:$pass" >>/tmp/chpasswd.log
done
chpasswd </tmp/chpasswd.log
[root@centos6-kvm3 shili]#
(四)掃描網路記憶體活主機案例。寫一個shell腳本,判斷10.0.0.0/24當前網路中主機數量。當前哪些線上的有哪些。
如何判斷主機存活。
①ping
-c次數
-i 間隔
②nmap (yum)
nmap -sP 10.0.0.0/24
方法1
[root@centos6-kvm3 shili]# cat 14-04.sh
#!/bin/bash
for n in {1..254}
do
{
if `ping -c 1 -w 3 10.0.0.$n &>/dev/null`
then
echo "10.0.0.$n is up."
else
echo "10.0.0.$n is down."
fi
} &#&表示並行
done
方法2
[root@centos6-kvm3 shili]# nmap -sP 10.0.0.0/24 | awk '/Nmap scan report for/{print $NF}'
(五)、mysql資料庫分庫備份
mysql -uroot -poldboy123 -e "show databases" | grep -v _scheme|sed -1d
分庫備份:
mysqldump -B oldboy | gzip > bak.sql.gz
[root@centos6-kvm3 scripts]# vim mysql.sh
#!/bin/bash
path=/back
[ -d $path ] || mkdir $path -p
for dbname in `mysql -uroot -poldboy123 -e "show databases;" 2>/dev/null | grep -v _schema | sed -1d`
do
mysqldump -uroot -poldboy123 -B $dbname | gzip >$path/${dbname}.sql.gz
done
(六)、mysql分庫分表備份案例分析及開發實現
如何實現mysql資料庫進行分庫加分表備份,請用腳本實現。
解答:
mysqldump oldboy test test1| gzip >bak.sql.gz
1,oldboy 庫名
2,test\test1都是表名
方法:
mysqldump -B oldboy |gzip >bak.sql.gz
mysqldump oldboy test1
mysqldump -B oldgril | gzip >bak.sql.gz
mysqldump oldgril test1
案例
mysql -uroot -poldboy123 -e "show tables from wordpress;" 2>/dev/null | sed -1d
[root@centos6-kvm3 scripts]# cat mysql.sh
#!/bin/bash
path=/back
[ -d $path ] || mkdir $path -p
for dbname in `mysql -uroot -poldboy123 -e "show databases;" 2>/dev/null | grep -v _schema | sed 1d`
do
for tname in `mysql -uroot -poldboy123 -e "show tables from $dbname;" 2>/dev/null | grep 1d `
do
if [ "$dbname" = "mysql" ]
then
mysqldump -uroot -poldboy123 $dbname $tname | gzip >$path/${dbname}_${tname}.sql.gz
else
mysqldump $dbname $tname | gzip >$path/${dbname}_${tname}.sql.gz
fi
done
done
[root@centos6-kvm3 scripts]#
(七)、SSH免秘鑰批量分發文件專業腳本
有3台機器,m01,backup,nfs01,採用ssh免秘鑰實現從m01到其他兩台機器無密碼登錄後,請寫腳本實現從m01批量分發任意文件到其他兩台機器的任意目錄下。
免密環境生成:
[root@centos6-kvm3 scripts]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
12:af:26:77:1e:2d:2b:ce:94:aa:f9:be:c5:a3:c8:90 root@centos6-kvm3
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| . |
| o |
| . S |
| . . + . |
|E . X + . |
| o o X.+ + |
| =+*oo.o |
+-----------------+
[root@centos6-kvm3 scripts]# ll ~/.ssh/
total 8
-rw------- 1 root root 1675 Jan 27 21:48 id_rsa
-rw-r--r-- 1 root root 399 Jan 27 21:48 id_rsa.pub
[root@centos6-kvm3 scripts]# cd ~/.ssh/
[root@centos6-kvm3 .ssh]# ssh-copy-id id_rsa.pub 10.0.0.8
案例腳本
[root@centos6-kvm3 scripts]# cat fenfa.sh
#!/bin/bash
. /etc/init.d/functions
if [ $# -ne 2 ]
then
echo "usage:$0 localdir remotedir"
exit 1
fi
for n in 8 41 43
do
scp -rp $1 10.0.0.$n:$2 &>/dev/null
if [ $? -eq 0 ]
then
action "10.0.0.$n is successful" /bin/true
else
action "10.0.0.$n is failure" /bin/false
fi
done
[root@centos6-kvm3 scripts]#
(八)、破解RANDOM隨機數案例
已知下麵的字元串是通過RANDOM隨機數變數md5sum後,再截取一部分字元串的結果,請破解這些字元串對應的使用的md5sum處理前的RANDOM對應的數字。
21029299
00205d1c
a3da1677
1f6d12dd
890684b
解答:
[root@centos6-kvm3 .ssh]# cat random.sh
#!/bin/bash
array=(
21029299
00205d1c
a3da1677
1f6d12dd
890684b
)
funmd5(){
for n in {1..32767}
do
echo "$n\t `$n|md5sum`" >>/tmp/md5sum.log
done
}
funjudge(){
for n in ${array[*]}
do
if [ `echo grep $n /tmp/md5sum.log|wc -l` -eq 1 ]
then
echo `grep $n /tmp/md5sum.log`
fi
done
}
main(){
funmd5
funjudge
}
main
[root@centos6-kvm3 .ssh]#
優化方法1:
[root@centos6-kvm3 .ssh]# cat random1.sh
#!/bin/bash
array=(
21029299
00205d1c
a3da1677
1f6d12dd
890684b
)
funmd5(){
for n in {1..32767}
do
echo "$n\t `$n|md5sum`" >>/tmp/md5sum1.log
done
}
funjudge(){
char="`echo ${array[*]} | tr " " "|"`"
egrep $char /tmp/md5sum1.log
}
main(){
funmd5
funjudge
}
main
[root@centos6-kvm3 .ssh]#
(九)、批量檢查多個網站地址是否正常
企業面試題:批量檢查多個網站地址是否正常。
要求:
1、使用shell數組方法實現,檢查策略儘量模擬用戶訪問。
2、每10s做一次所有的檢測,無法訪問的輸出報警。
3、待檢測的地址如下:
解答:wget --spider #模擬爬蟲,不下載html文件。
方法1
方法1
[root@centos6-kvm3 .ssh]# cat url.sh
#!/bin/bash
. /etc/init.d/functions
array=(
http://blog.oldboyedu.com
http://www.baidu.com
http://blog.ettiantian.org
http://www.luffycity.com
http://10.0.0.7
)
checkurl(){
wget -t 2 -T 5 -o /dev/null -q $1
if [ $? -eq 0 ]
then
action "$1 is successfull." /bin/true
else
action "$1 is failure." /bin/false
fi
}
dealurl(){
for n in ${array[*]}
do
checkurl $n
done
}
main(){
while true
do
dealurl
sleep 2
echo "--------"
done
}
main
[root@centos6-kvm3 .ssh]#
方法2
方法2
[root@centos6-kvm3 .ssh]# cat url1.sh
#!/bin/bash
. /etc/init.d/functions
array=(
http://blog.oldboyedu.com
http://www.baidu.com
http://blog.ettiantian.org
http://www.luffycity.com
http://10.0.0.7
)
checkurl(){
wget -t 2 -T 5 -o /dev/null -q $1
if [ $? -eq 0 ]
then
action "$1 is successfull." /bin/true
else
action "$1 is failure." /bin/false
fi
}
dealurl(){
for ((i=0;i<${#array[*]};i++))
do
checkurl ${array[$i]}
done
}
main(){
while true
do
dealurl
sleep 2
echo "--------"
done
}
main
[root@centos6-kvm3 .ssh]#
方法3
方法3
[root@centos6-kvm3 .ssh]# cat url2.sh
#!/bin/bash
. /etc/init.d/functions
checkurl(){
wget -t 2 -T 5 -o /dev/null -q $1
if [ $? -eq 0 ]
then
action "$1 is successfull." /bin/true
else
action "$1 is failure." /bin/false
fi
}
dealurl(){
while read line
do
checkurl $line
done < ./url.log
}
main(){
while true
do
dealurl
sleep 2
echo "--------"
done
}
main
[root@centos6-kvm3 .ssh]#
(十)、解決dos攻擊生成案例
寫一個shell腳本解決dos攻擊生成案例。
請根據web日誌或者網路連接數,監控某個ip併發連接數或者短時間內pv達到100(讀者根據實際請設定),即調用防火牆命令封掉對應的ip。防火牆命令為:iptables -I INPUT -s IP 地址 -j DROP
解答:
DOS deny of service
DDOS
分析:
1、封ip的命令
iptables -I INPUT -s IP 地址 -j DROP
2、web日誌或者網路連接數
日誌文件,netstat -an| grep -i est 排序去重。
3、判斷pv或者連接數大於100,取出ip後封。
取攻擊ip的方法:
方法1
[root@centos6-kvm3 scripts]# awk '{s[$1]++}END{for(key in s) print s[key],key}' access_2010-12-8.log | uniq |sort -nr#依照數據大小排序,r逆序
35 59.33.26.105
23 123.122.65.226
8 124.115.4.18
方法2
[root@centos6-kvm3 scripts]# awk '{print $1}' access_2010-12-8.log | uniq -c | sort -nr
35 59.33.26.105
23 123.122.65.226
8 124.115.4.18
[root@centos6-kvm3 scripts]#
[root@centos6-kvm3 scripts]# cat dos.sh
#!/bin/bash
awk '{s[$1]++}END{for(key in s) print s[key],key}' access_2010-12-8.log |sort -nr | head >/tmp/ip.log
while read line
do
ip=`echo $line|awk '{print $2}'`
count=`echo $line|awk '{print $1}'`
if [ $count -gt 30 -a `grep "$ip" /tmp/drop.log|wc -l` -lt 1 ]
then
iptables -I INPUT -s `echo $line|awk '{print $2}'` -j DROP &&\
echo "echo $line|awk '{print $2}'" >>/tmp/drop.log
else
echo "echo $line|awk '{print $2}'" >>/tmp/accept.log
fi
done </tmp/ip.log
[root@centos6-kvm3 scripts]#
[root@centos6-kvm3 scripts]# iptables -nL
獲取網路連接數的方法:
[root@centos6-kvm3 scripts]# awk -F "[ :]+" '/ESTAB/{S[$(NF-3)]++}END{for(k in S) print S[k],k}' netstat.log| sort -rn | head
[root@centos6-kvm3 scripts]# awk '/ESTAB/{print $(NF-1)}' netstat.log | awk -F ":" '{print $1}'| uniq -c | sort -rn |head
[root@centos6-kvm3 scripts]# vim dos1.sh
#!/bin/bash
awk -F "[ :]+" '/ESTAB/{S[$(NF-3)]++}END{for(k in S) print S[k],k}' netstat.log| sort -rn | head >/tmp/ip.log
while read line
do
ip=`echo $line|awk '{print $2}'`
count=`echo $line|awk '{print $1}'`
if [ $count -gt 30 -a `grep "$ip" /tmp/drop.log|wc -l` -lt 1 ]
then
iptables -I INPUT -s `echo $line|awk '{print $2}'` -j DROP &&\
echo "echo $line|awk '{print $2}'" >>/tmp/drop.log
else
echo "echo $line|awk '{print $2}'" >>/tmp/accept.log
fi
done </tmp/ip.log
[root@centos6-kvm3 scripts]# iptables -nL
(十一)、開發mysql服務啟動停止腳本
要求:用函數,case語句,if語句等實現。
解答:
/etc/init.d/mysqld {start | stop | restart}
分析:
1、啟動
mysql_safe --user=mysql & 一定要在命令行測試成功。
2、停止
mysqladmin -uroot -ppasswd shutdown
killall,pkill
kill pid 推薦
[root@centos6-kvm3 scripts]# cat mysqld.sh
# chkconfig: 2345 20 80
# description: mysql start stop
#!/bin/bash
lockfile=/var/lock/subsys/mysqld
. /etc/init.d/functions
mysqld_pid_file_path="/application/mysql/data/web01.pid"
mysqld_safe=/application/mysql/bin/mysqld_safe
start(){
/bin/sh $mysqld_safe --datadir=/application/mysql/data --pid-file=$mysqld_pid_file_path &>/dev/null &
retval=$?
if [ $retval -eq 0 ]
then
action "mysql start ok" /bin/true
touch $lockfile
return $retval
else
action "mysql start fail" /bin/false
return $retval
fi
}
stop(){
if test -s "$mysqld_pid_file_path"
then
mysqld_pid=`cat $mysqld_pid_file_path`
if (kill -0 $mysql_pid &>/dev/null) #kill -0 檢查
then
kill $mysqld_pid
retval=$?
if [ $retval -eq 0 ]
then
action "mysql stop ok" /bin/true
rm $lockfile
return $retval
else
action "mysql stop fail" /bin/false
return $retval
fi
else
echo "mysqld process is not exits."
return 2
fi
else
echo "$mysqld_pid_file_path is not exist,or mysqld does not startup."
fi
}
case $1 in
start)
start
retval=$?
;;
stop)
stop
retval=$?
;;
restart)
stop
sleep 2
start
retval=$?
;;
*)
echo "usage:$0 {start|stop|restart}"
exit 1
esac
exit $retval
[root@centos6-kvm3 scripts]#
(十二)、單詞及字母去重排序案例
用shell腳本處理以下內容
1、按單詞出現頻率降序排序。
2、按字母出現頻率降序排序。
the squid project provides a number of resources to assist users design,implement and support squid installations. Please browse the documentation and support sections for more infomation,by oldboy training.
1、按單詞出現頻率降序排序。
方法1:
[root@centos6-kvm3 scripts]# tr " ," "\n" <12.log | sort|uniq -c| sort -rn
空格逗號替換成回車
方法2:
[root@centos6-kvm3 scripts]# tr " ," "\n" <12.log | awk '{S[$1]++}END{for(k in S) print S[k],k}'| sort -nr
方法3
[root@centos6-kvm3 scripts]# xargs -n1 <12.log | sort|uniq -c|sort -nr
2、按字母出現的頻率進行降序排序。
方法1
[root@centos6-kvm3 scripts]# grep -o "[^ ]" 12.log | sort|uniq -c|sort -nr
[^ ]表示非空
方法2
[root@centos6-kvm3 scripts]# grep -o "[^ ]" 12.log | awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -rn
方法3
[root@centos6-kvm3 scripts]# sed 's#[ ,\.]##g' 12.log | awk -F "" '{for(i=1;i<NF;i++)s[$i]++}END{for(k in s) print s[k],k}'| sort -nr
(十三)、按單詞去重排序案例高級方法
豎著處理:
[root@centos6-kvm3 scripts]# awk -F '[ ,.]+' '{for(i=1;i<NF;i++)s[$i]++}END{for(k in s) print s[k],k}' 12.log | sort -nr