腳本目錄列表
一、在windows編寫的shell腳本利用dos2unix命令格式化一下(P308)html
二、bash命令參數調試(P309)node
三、使用set命令調試部分腳本內容(P312)linux
四、開發腳本實現觸發信號後清理文件功能(P331)shell
五、expect的自動交互工做流程(P337)vim
六、執行ssh命令遠程獲取服務器負載值,並自動輸入「yes」及用戶密碼(P339)windows
七、利用expect響應shell腳本中的多個read讀入(P340)數組
八、開發expect腳本實現自動交互式批量執行命令(P348)bash
十、開發expect腳本以實現自動交互式批量發送文件或目錄(P350)服務器
十一、開發一個綜合上述expect的腳本,實現自動交互,批量執行shell的腳本(P352)ssh
十二、批量分發SSH密鑰並安裝ansible(P355)
1三、天天晚上12點,打包站點目錄/var/www/html備份到/data目錄下
1四、永久修改主機名
一、在windows編寫的shell腳本利用dos2unix命令格式化一下(P308)
#dos2unix *.sh
#yum install dos2unix -y //若是沒有安裝。。。
二、bash命令參數調試(P309)
-n:不會執行腳本,只檢查腳本是否有問題,並給出錯誤提示;
-v:在執行腳本時,先將內容輸出到屏幕上,而後執行腳本,若有錯誤並輸出錯誤;
-x:將執行的腳本內容及輸出顯示到屏幕上
三、使用set命令調試部分腳本內容(P312)
set -n:讀命令但並不執行
set -v:顯示讀取的全部行
set -x:顯示全部命令及其參數


1 #!/bin/bash 2 # 3 4 set -x 5 for a in `seq 9`; do 6 for b in `seq 9`; do 7 [ $a -ge $b ] && echo -en "$a x $b = $(expr $a \* $b) " 8 done 9 set +x 10 echo " " 11 done
四、開發腳本實現觸發信號後清理文件功能(P331)


1 #!/bin/bash 2 # 3 4 trap "find /tmp -type f -name "hello_*" | xargs rm -f && exit" INT 5 while true; do 6 touch /tmp/hello_$(date +%F-%H-%M-%S) 7 sleep 2 8 ls -l /tmp/hello* 9 done
五、expect的自動交互工做流程(P337)
spawn啓動指定進程-->expect獲取期待的關鍵字-->send向指定進程發送指定字符-->進程執行完畢,退出結束
spawn [選項][須要自動交互的命令或程序]
-open:表示啓動文件進程
-ignore:表示忽略某些信號
expect 表達式 [動做]
expect "*passowrd" {send "123456\r"}
#rpm -qa expect //查看是否安裝
#vim expect.exp //擴展名須要使用exp表明是expect腳本
#which expect //查看expect的路徑


1 #!/bin/expect //腳本解釋器 2 3 spawn ssh root@192.168.184.141 uptime //開啓expect自動交互,執行ssh命令 4 5 expect "*password" {send "123456\n"} //若是ssh命令輸出匹配*password,就發送123456給系統 6 expect eof //要想輸出結果,必須加eof,表示expect結束
expect "*password"
send "123456\n" //也能夠把匹配動做放到下一行
expect eof
#expect 2.exp //執行腳本要是expect
六、執行ssh命令遠程獲取服務器負載值,並自動輸入「yes」及用戶密碼(P339)


1 #!/bin/expect 2 3 spawn ssh root@192.168.184.143 uptime 4 expect { 5 "yes/no" {exp_send "yes\r";exp_continue} 6 "*password" {exp_send "123456\r"} 7 } 8 9 expect eof
exp_send和send相似,後面的\r(回車)和前文的\n相似
expect{},相似多行expect
匹配多個字符串,須要在每次匹配並執行動做後,加上exp_continue
七、利用expect響應shell腳本中的多個read讀入(P340)


1 #!/bin/bash 2 # 3 read -p "Please inpunt your username: " name 4 read -p "Please inpunt your password: " pass 5 read -p "Please inpunt your email: " mail 6 echo "your name is $name," 7 echo "your password is $pass," 8 echo "your email is $mail."


1 #!/bin/expect 2 3 spawn /bin/bash 4.sh 4 expect { 5 "username" {exp_send "oldboy\r";exp_continue} 6 "*pass*" {send "123456\r";exp_continue} 7 "*mail*" {exp_send "1020421472@qq.com\r"} 8 } 9 10 expect eof
\r:表示回車
\n:表示換行
\t:表示製表符
若是須要一次匹配多個字符串,那麼不一樣的匹配之間就要加上exp_continue,不然expect將不會自動輸入指定的字符串,最後一個的結尾就不須要加上exp_continue了,由於都完成了。
exit功能


1 #!/bin/expect 2 # 3 send_user "I am hello.\n" 4 send_user "I am linuxer.\t" 5 send_user "I am world.\n" 6 7 exit -onexit { 8 send_user "Good bye.\n" 9 }
send_user功能


1 #!/bin/expect 2 # 3 send_user "I am hello.\n" 4 send_user "I am linuxer.\t" 5 send_user "I am world.\n"
expect參數變量
set 變量名 變量值
例:set password "123456"
puts $變量名
或
send_user "$變量名"
在expect中$argv表示參數數組,可使用[lindex $argv n]接收expect腳本傳參,n從0開始,分別表示一個[lindex $argv 0]參數,第二個[lindex $argv 1]參數等。
$argc表示傳參的個數,$argv0表示腳本的名字
定義及輸出特殊參數變量:


#!/bin/expect set file [lindex $argv 0] set host [lindex $argv 1] set dir [lindex $argv 2] send_user "$file\t$host\t$dir\n" puts "$file\t$host\t$dir"
#expect 1.exp hello 192.168.184.141 /tmp
八、開發expect腳本實現自動交互式批量執行命令(P348)


1 #!/bin/expect 2 3 if { $argc != 2 } { 4 puts "Usage: expect $argv0 ip command" 5 exit 6 } 7 8 set ip [lindex $argv 0] 9 set cmd [lindex $argv 1] 10 set password "123456" 11 12 spawn ssh root@$ip $cmd 13 expect { 14 "yes/no" {send "yes\r";exp_continue} 15 "*password" {send "$password\r"} 16 } 17 expect eof
九、利用shell循環執行expect腳本命令(P349)


1 #!/bin/bash 2 # 3 if [ $# -ne 1 ]; then 4 echo "Usage:$0 cmd" 5 exit 1 6 fi 7 8 cmd=$1 9 for n in 140 141 143; do 10 expect 2.exp 192.168.184.$n "$cmd" 11 done
十、開發expect腳本以實現自動交互式批量發送文件或目錄(P350)


1 #!/bin/expect 2 3 if { $argc != 3 } { 4 puts "usage: expect $argv0 file host dir" 5 exit 6 } 7 8 set file [lindex $argv 0] 9 set host [lindex $argv 1] 10 set dir [lindex $argv 2] 11 set password "123456" 12 13 spawn scp -P22 $file root@$host:$dir 14 expect { 15 "yes/no" {send "yes\r";exp_continue} 16 "*password" {send "$password\r"} 17 } 18 19 expect eof
# expect 3.exp /root/scripts/2.exp 192.168.184.141 scripts
十一、開發一個綜合上述expect的腳本,實現自動交互,批量執行shell的腳本(P352)
例如192.168.184.140、14一、142三臺服務器都沒有安裝wget這個命令軟件,以140爲主節點,批量安裝這三臺服務器
#rpm -qa wget //沒有輸出任何信息
步驟1、首先實現主節點140與其餘兩臺服務器自動交互,即無人工干預便可實現ssh密鑰認證,並執行指定ip,指定命令


1 #!/bin/expect 2 3 if { $argc != 2 } { 4 puts "Usage: expect $argv0 ip command" 5 exit 6 } 7 8 set ip [lindex $argv 0] 9 set cmd [lindex $argv 1] 10 set password "123456" 11 12 spawn ssh root@$ip $cmd 13 expect { 14 "yes/no" {send "yes\r";exp_continue} 15 "*password" {send "$password\r"} 16 } 17 18 expect eof
# expect alter.exp 192.168.184.141 uptime //指定IP 指定命令
步驟2、藉助步驟1的自動交互功能,同時利用shell腳本循環執行多個IP地址上指定的命令


1 #!/bin/bash 2 # 3 4 if [ $# -ne 1 ]; then 5 echo "Usage:$0 cmd" 6 exit 1 7 fi 8 9 cmd=$1 10 for n in 140 141 143; do //這裏IP地址已經給出,只需加上想要執行的命令便可 11 expect alter.exp 192.168.184.$n "$cmd" 12 done
# bash install.sh "free -m" //不連貫字符要用雙引號
步驟1和步驟2是不可分離的,由於只有實現服務器之間自動交互,才能利用shell循環執行指定在多個IP上的命令;
步驟3、這也是一個自動交互的腳本,可是定義的變量不同,同時spawn執行的命令也不同,這裏是實現自動複製文件到另外一個服務器的腳本


1 #!/bin/expect 2 3 if { $argc != 3 } { 4 puts "Usage: expect $argv0 file host dir" 5 exit 6 } 7 8 set file [lindex $argv 0] 9 set host [lindex $argv 1] 10 set dir [lindex $argv 2] 11 set password "123456" 12 13 spawn scp -P22 $file root@$host:$dir 14 expect { 15 "yes/no" {send "yes\r";exp_continue} 16 "*password" {send "$password\r"} 17 } 18 19 expect eof
# expect alter_file.exp /root/scripts/yum.sh 192.168.184.141 /tmp //輸入本地文件、要傳輸至的IP、目錄名
步驟4、藉助步驟3的自動交互且傳輸功能,同時利用shell腳本循環執行多個IP地址上同時指定傳輸文件到指定的目錄


1 #!/bin/bash 2 3 if [ $# -ne 2 ]; then 4 echo "Usage:$0 file dir" 5 exit 1 6 fi 7 8 file=$1 9 dir=$2 10 11 for n in 141 143; do //140本地已經有了,因此再也不添加 12 expect 1.exp $file 192.168.184.$n $dir 13 done
#echo "yum install wget -y" >/root/scripts/yum.sh //建立安裝wget的腳本文件
# bash cp_file.sh yum.sh /root/scripts/ //這裏的yum.sh是相對路徑即/root/scripts,把yum.sh文件批量發送到141和143主機上
步驟5、使用主節點服務器鏈接141和143服務器並指執行腳本yum.sh安裝wget
# bash install.sh "source scripts/yum.sh" //同時安裝3臺服務器
十二、批量分發SSH密鑰並安裝ansible(P355)
一、本地生成密鑰對
# rm -rf ~/.ssh/known_hosts //若是以前有認證信息能夠先清除
# ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa &>/dev/null //本地生成密鑰對
二、開發expect腳本自動化交互分發公鑰到其餘服務器


1 #!/bin/expect 2 3 if { $argc != 2 } { 4 send_user "Usage: expect.exp file host\n" 5 exit 6 } 7 8 set file [lindex $argv 0] 9 set host [lindex $argv 1] 10 set password "123456" 11 12 spawn ssh-copy-id -i $file -p 22 root@$host 13 expect { 14 "yes/no" {send "yes\r";exp_continue} 15 "*password" {send "$password\r"} 16 } 17 expect eof
# expect ssh_copy.exp /root/.ssh/id_dsa.pub 192.168.184.141 //測試是否能夠分發公鑰到其餘主機
三、編輯shell循環腳本


1 #!/bin/bash 2 # 3 4 for n in 141; do 5 expect ni.exp ~/.ssh/id_dsa.pub 192.168.184.$n 6 done
#bash cycl.sh //執行此腳本便可分發公鑰到各主機
四、編寫執行腳本進行測試,兩行
ssh 192.168.184.141 uptime
ssh 192.168.184.143 uptime
五、安裝epel源和ansiable軟件
yum install epel-release -y
yum install ansible -y
六、在ansible的配置文件/etc/ansible/hosts最後添加以下幾行
[hanshan]
192.168.184.140
192.168.184.141
七、執行命令
# ansible hanshan -m command -a "uptime"
1三、天天晚上12點,打包站點目錄/var/www/html備份到/data目錄下


1 #!/bin/bash 2 # 3 cd /var/www/ || { 4 echo "/var/www/ is not exist." 5 } 6 7 /bin/tar zcf /root/data/html-`date +%Y-%m-%d-%H:%M:%S`.tar.gz html/*
# crontab -e
00 00 * * * /bin/sh /root/a.sh
1四、永久修改主機名


1 #!/bin/bash 2 # 3 4 echo "Look over the static hostname." 5 hostnamectl --static 6 echo "" 7 echo "Look over the transient hostname." 8 hostnamectl --transient 9 echo "" 10 11 read -p "Do you want to change the num of node: " num 12 13 echo "The scripts will change the hostname" 14 hostnamectl set-hostname node$num 15 16 sed -i 's/^127/#127/g' /etc/hosts 17 sed -i 's/^::1/#::1/g' /etc/hosts 18 echo 127.0.0.1 node$num >> /etc/hosts 19 echo ::1 node$num >> /etc/hosts 20 cat /etc/hosts
你好