expect可讓咱們實現自動登陸遠程機器,而且能夠實現自動遠程執行命令。固然如果使用不帶密碼的密鑰驗證一樣能夠實現自動登陸和自動遠程執行命令。但當不能使用密鑰驗證的時候,咱們就沒有辦法了。因此,這時候只要知道對方機器的帳號和密碼就能夠經過expect腳本實現登陸和遠程命令。linux
[root@garytao-01 mon]# yum install -y expect
[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 root@172.16.111.110 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. root@172.16.111.110'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]#
[root@garytao-01 shell]# vi 2.expect 增長腳本以下內容: #!/usr/bin/expect set user "root" set passwd 123456" spawn ssh $user@172.16.111.110 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 root@172.16.111.110 root@172.16.111.110'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 root@172.16.111.110 root@172.16.111.110'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 ~]#
[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 root@172.16.111.110 root@172.16.111.110'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 root@172.16.111.110 root@172.16.111.110'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