2018.4.25 18周1次課
20.27 分發系統介紹
20.28 expect腳本遠程登錄
20.29 expect腳本遠程執行命令
20.30 expect腳本傳遞參數
20.27 分發系統介紹
expect是一種能夠按照腳本內容裏面設定的方式與交互式程序進行“會話”的程序。根據腳本內容,Expect可以知道程序會提示或反饋什麽內容以及什麽是正確的應答。它是一種可以提供“分支和嵌套結構”來引導程序流程的解釋型腳本語言。
我們熟知的shell編程功能雖然很強大,但是不能實現有交互功能的多機器之前的操作,例如ssh和scp等。而expect可以幫助我們來實現。
20.28 expect腳本遠程登錄
yum install -y expect
自動遠程登錄192.168.37.100
編輯expect文件:vi 1.expect
#! /usr/bin/expect //說明用expect來執行
set host "192.168.37.100" //set是設置變量,這裏設置host變量的值,就是登陸主機的ip地址
set passwd "123456" //set設置passwd變量的值,就是登陸主機的密碼
spawn ssh root@$host //是進入expect環境後才可以執行的expect內部命令,用來執行它後面的命令。這裏是登陸主機的語句
expect { //
"yes/no" { send "yes\r"; exp_continue} //執行交互式動作,截取ssh連接主機時,"yes/no"這段,發送yes,\r:表示回車。exp_continue:表示繼續
"assword:" { send "$passwd\r" } //執行交互式動作,截取"assword:"這段,發送上面設置的passwd變量的值
}
Interact //
用ssh登陸100主機:
ssh 192.168.37.100
[root@aming-02 /usr/local/sbin]# ssh 192.168.37.100
Last login: Tue Apr 24 15:27:32 2018 from 192.168.37.1
因為登陸過,所以沒有提示就直接登陸了。清空/root/.ssh/known_hosts文件,讓登陸時有提示
vi .ssh/known_hosts
ssh 192.168.37.100
[root@aming-01 ~]# ssh 192.168.37.100
The authenticity of host '192.168.37.100 (192.168.37.100)' can't be established.
ECDSA key fingerprint is SHA256:O/psyoi564EA1rmbUfUBPXikCFUf6bMTmwfAb8wwi7A.
ECDSA key fingerprint is MD5:47:ce:6b:84:7d:4f:e2:5a:cc:bb:0f:4b:61:be:ca:d6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.37.100' (ECDSA) to the list of known hosts.
[email protected]'s password:
更改權限:chmod a+x 1.expect
執行腳本:./1.expect
[root@aming-01 /usr/local/sbin]# ./1.expect
spawn ssh [email protected]
The authenticity of host '192.168.37.100 (192.168.37.100)' can't be established.
ECDSA key fingerprint is SHA256:O/psyoi564EA1rmbUfUBPXikCFUf6bMTmwfAb8wwi7A.
ECDSA key fingerprint is MD5:47:ce:6b:84:7d:4f:e2:5a:cc:bb:0f:4b:61:be:ca:d6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.37.100' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Tue Apr 24 16:12:28 2018 from 192.168.37.100
20.29 expect腳本遠程執行命令
自動遠程登錄後,執行命令並退出
編輯expect文件:vi 2.expect
#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh [email protected]
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*" //登陸到01主機上後,最後一行顯示的是“[root@aming-01 ~]#”,這裏用*表示通配,這行要有關鍵字符串“]*”,就執行下面的命令
send "touch /tmp/12.txt\r"
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r" //直接退出01主機
先退出01主機:logout
[root@aming-01 ~]# logout
Connection to 192.168.37.100 closed.
更改權限:chmod a+x 2.expect
執行:./2.expect
[root@aming-02 /usr/local/sbin]# ./2.expect
spawn ssh [email protected]
Last login: Wed Apr 25 09:56:25 2018 from 192.168.37.101
[root@aming-01 ~]# touch /tmp/12.txt
[root@aming-01 ~]# echo 1212 > /tmp/12.txt
[root@aming-01 ~]# [root@aming-02 /usr/local/sbin]#
[root@aming-02 /usr/local/sbin]#
查看01機器上的12.txt
[root@aming-01 ~]# ls /tmp/12.txt
/tmp/12.txt
[root@aming-01 ~]# cat !$
cat /tmp/12.txt
1212
20.30 expect腳本傳遞參數
編輯expect文件:vi 3.expect
傳遞參數
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456"
set cm [lindex $argv 2]
spawn ssh $user@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
更改權限:chmod a+x 3.expect
執行腳本:./3.expect root 192.168.37.100 ls
[root@aming-02 /usr/local/sbin]# ./3.expect root 192.168.37.100 ls
spawn ssh [email protected]
Last login: Wed Apr 25 10:39:59 2018 from 192.168.37.101
[root@aming-01 ~]# ls
123 1.txt 3.txt anaconda-ks.cfg data sed test1
1_heard.txt 234 aminglinux awk grep soft_link
1_sorft.txt 2.txt aming.txt bb.txt oldboy test
如果要執行多條命令,要用雙引號,雙引號裏面用分號分隔:./3.expect root 192.168.37.100 "ls;w"
[root@aming-02 /usr/local/sbin]# ./3.expect root 192.168.37.100 "ls;w"
spawn ssh [email protected]
Last login: Wed Apr 25 11:27:47 2018 from 192.168.37.101
[root@aming-01 ~]# ls;w
123 1.txt 3.txt anaconda-ks.cfg data sed test1
1_heard.txt 234 aminglinux awk grep soft_link
1_sorft.txt 2.txt aming.txt bb.txt oldboy test
11:32:43 up 1:41, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.37.1 09:52 1:03m 0.08s 0.08s -bash
root pts/1 192.168.37.101 11:32 3.00s 0.01s 0.00s w
[root@aming-01 ~]# [root@aming-02 /usr/local/sbin]#
2018.4.25 18周1次課