yum install -y expectphp
自動遠程登陸 vim 1.expect編程
[root@yong-01 sbin]# vim 1.expect #! /usr/bin/expect set host "192.168.180.135" set passwd "20655739" spawn ssh root@$host expect { "yes/no" { send "yes\r"; exp_continue} // "password:" { send "$passwd\r" } } interact //腳本結束
加入執行權限vim
[root@yong-01 sbin]# ./1.expect -bash: ./1.expect: 權限不夠 [root@yong-01 sbin]# chmod a+x 1.expect [root@yong-01 sbin]# ll 1.expect -rwxr-xr-x 1 root root 191 7月 19 22:45 1.expect
[root@yong-01 sbin]# ./1.expect spawn ssh root@192.168.180.135 The authenticity of host '192.168.180.135 (192.168.180.135)' can't be established. ECDSA key fingerprint is SHA256:oYqovve1b2BwHDBYcFasCiiFzZTHJvKDbTGZAjmlMXc. ECDSA key fingerprint is MD5:3d:f8:af:0d:85:48:db:2a:46:0e:68:5f:eb:43:3e:43. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.180.135' (ECDSA) to the list of known hosts. Last login: Thu Jul 19 21:43:59 2018 [root@yong-02 ~]#
[root@yong-01 sbin]# vim 2.expect #!/usr/bin/expect set user "root" set passwd "20655739" spawn ssh $user@192.168.180.135 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"
expect "]" *「」 通配]右邊全部字符,windows
send 執行命令後端
A 機器執行腳本bash
[root@yong-01 sbin]# ll 2.expect -rw-r--r-- 1 root root 283 7月 19 22:49 2.expect [root@yong-01 sbin]# chmod a+x 2.expect [root@yong-01 sbin]# ./2.expect spawn ssh root@192.168.180.135 Last login: Thu Jul 19 22:45:41 2018 from 192.168.180.134 [root@yong-02 ~]# touch /tmp/12.txt [root@yong-02 ~]# echo 1212 > /tmp/12.txt [root@yong-02 ~]# [root@yong-01 sbin]#
[root@yong-02 ~]# ll /tmp/12.txt -rw-r--r-- 1 root root 5 7月 19 22:50 /tmp/12.txt [root@yong-02 ~]# cat /tmp/12.txt 1212
root@yong-01 sbin]# vim 3.expect #!/usr/bin/expect set user [lindex $argv 0] //第一個參數 set host [lindex $argv 1] //第二個參數 set passwd "20655739" 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"
[root@yong-01 sbin]# chmod a+x 3.expect [root@yong-01 sbin]# ./3.expect root 192.168.180.135 "ls;w" spawn ssh root@192.168.180.135 Last login: Thu Jul 19 22:51:33 2018 from 192.168.180.134 [root@yong-02 ~]# ls;w anaconda-ks.cfg cn_windows_7_ultimate_with_sp1_x64_dvd_618537.iso multi-user.target rescue.target a.txt c.txt poweroff.target zabbix-release-3.2-1.el7.noarch.rpm b.txt graphical.target reboot.target 這裏須要指定一個用戶名的密碼文件 22:56:20 up 29 min, 1 user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.180.134 22:56 4.00s 0.03s 0.00s w
[root@yong-01 sbin]# ./3.expect root 192.168.180.135 "ls;vmstat 1" spawn ssh root@192.168.180.135 Last login: Thu Jul 19 22:56:10 2018 from 192.168.180.134 [root@yong-02 ~]# ls;vmstat 1 anaconda-ks.cfg cn_windows_7_ultimate_with_sp1_x64_dvd_618537.iso multi-user.target rescue.target a.txt c.txt poweroff.target zabbix-release-3.2-1.el7.noarch.rpm b.txt graphical.target reboot.target 這裏須要指定一個用戶名的密碼文件 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 1119512 2112 190328 0 0 95 50 122 163 0 1 99 0 0 0 0 0 1119516 2112 190360 0 0 0 0 87 95 0 1 99 0 0 0 0 0 1119516 2112 190364 0 0 4 10 108 120 0 0 100 0 0 0 0 0 1119516 2112 190364 0 0 0 0 87 92 0 0 100 0 0 0 0 0 1119516 2112 190364 0 0 0 99 81 95 0 0 100 0 0 0 0 0 1119516 2112 190364 0 0 0 10 113 125 0 1 99 0 0