分發系統介紹
expect可以讓我們實現自動登錄遠程機器,並且可以實現自動遠程執行命令。當然若是使用不帶密碼的密鑰驗證同樣可以實現自動登錄和自動遠程執行命令。但當不能使用密鑰驗證的時候,我們就沒有辦法了。所以,這時候只要知道對方機器的賬號和密碼就可以通過expect腳本實現登錄和遠程命令。
expect腳本遠程執行命令
1.安裝expect
[root@garytao-01 mon]# yum install -y expect
2.自動遠程登錄
[root@garytao-01 shell]# vi 1.expect
增加以下腳本內容:
#! /usr/bin/expect
set host "172.16.111.110" #遠程登錄IP
set passwd "123456" #遠程登錄密碼
spawn ssh root@$host #登錄語句
expect {
"yes/no" { send "yes\r"; exp_continue} #初次登錄需要輸入yes才能進入
"assword:" { send "$passwd\r" } #自動輸入密碼
}
interact #作用,表示要停留在遠程機器了,不會退出來,如果不加就會退出來
#如果是expect eof 就會在機器上停留一兩秒退出來
[root@garytao-01 shell]# chmod a+x 1.expect
[root@garytao-01 shell]# ./1.expect
spawn ssh [email protected]
The authenticity of host '172.16.111.110 (172.16.111.110)' can't be established.
ECDSA key fingerprint is 09:6d:70:42:42:9a:12:69:51:9b:ad:e5:73:98:b9:c0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.111.110' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Mon Feb 26 18:22:32 2018 from 172.16.111.100
[root@garytao-02 ~]# 登出
Connection to 172.16.111.110 closed.
[root@garytao-01 shell]#
expect腳本運程傳遞參數
1.自動遠程登錄後,執行命令並退出
[root@garytao-01 shell]# 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 "]*" ##表示圖中括號裏的,表示當檢測到這個符號時就執行我們要執行的命令
send "touch /tmp/12.txt\r"
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"
## 執行腳本
[root@garytao-01 shell]# ./2.expect
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Feb 27 10:23:28 2018 from 172.16.111.100
[root@garytao-02 ~]# touch /tmp/12.txt
[root@garytao-02 ~]# echo 1212 > /tmp/12.txt
## 回車退出
[root@garytao-02 ~]# [root@garytao-01 shell]#
[root@garytao-01 shell]#
##重新執行自動登錄腳本
[root@garytao-01 shell]# ./1.expect
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Feb 27 10:34:42 2018 from 172.16.111.100
##查看遠程創建的文件
[root@garytao-02 ~]# ls -l /tmp/12.txt
-rw-r--r-- 1 root root 5 2月 27 10:34 /tmp/12.txt
##查看遠程腳本創建的文件內容
[root@garytao-02 ~]# cat /tmp/12.txt
1212
[root@garytao-02 ~]#
expect腳本傳遞參數
1.傳遞參數
[root@garytao-01 shell]# vi 3.expect
增加如下腳本內容:
#!/usr/bin/expect
set user [lindex $argv 0] #把第一個參數的值賦給user
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"
set timeout -1 #-1表示永遠不超時,1表示1秒,2表示2秒....,表示執行命令幾秒後停止
expect "]*"
send "exit\r"
[root@garytao-01 shell]# chmod a+x 3.expect
[root@garytao-01 shell]# ./3.expect root 172.16.111.110 ls
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Feb 27 10:39:46 2018 from 172.16.111.100
[root@garytao-02 ~]# ls
123.txt 1_heard.txt 1.txt aming anaconda-ks.cfg.1 shell zabbix-release-3.2-1.el7.noarch.rpm
123.txt~ 1_sorft.txt 2.txt aminglinux rsync yum.log
[root@garytao-02 ~]# [root@garytao-01 shell]# ./3.expect root 172.16.111.110 "ls;w;vmstat 1"
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Feb 27 10:53:39 2018 from 172.16.111.100
[root@garytao-02 ~]# ls;w;vmstat 1
123.txt 1_heard.txt 1.txt aming anaconda-ks.cfg.1 shell zabbix-release-3.2-1.el7.noarch.rpm
123.txt~ 1_sorft.txt 2.txt aminglinux rsync yum.log
10:56:21 up 6 days, 9:04, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 172.16.111.1 一18 16:31m 0.00s 0.00s -bash
root pts/1 172.16.111.100 10:56 0.00s 0.01s 0.01s w
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 204888 876 239148 0 0 0 1 46 14 0 0 100 0 0
0 0 0 204904 876 239164 0 0 0 21 55 116 0 0 100 0 0
0 0 0 204904 876 239164 0 0 0 0 55 113 0 0 100 0 0
0 0 0 204904 876 239164 0 0 0 0 56 112 0 0 100 0 0
0 0 0 204904 876 239164 0 0 0 0 56 111 0 0 100 0 0
0 0 0 204904 876 239164 0 0 0 0 58 110 0 0 100 0 0