Introduction to the distribution system
1. Installation command:
[root@weixing01 ~]# yum install -y expect
已加载插件:fastestmirror
base | 3.6 kB 00:00:00
epel/x86_64/metalink | 8.1 kB 00:00:00
epel
2. Automatically log in remotely and execute: interact means stay in the remote without exiting
[root@weixing01 sbin]# vi 1.expect
#! /usr/bin/expect
set host "192.168.188.129"
set passwd "w914"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
interact
[root@weixing01 sbin]# chmod a+x 1.expect
[root@weixing01 sbin]# ./1.expect
spawn ssh [email protected]
The authenticity of host '192.168.188.129 (192.168.188.129)' can't be established.
ECDSA key fingerprint is SHA256:SL6ZOvpHHtEoro8AzeNBXqrM3mr2oXbPaSeXO+LQr1U.
ECDSA key fingerprint is MD5:26:0e:7a:96:d6:3a:c5:57:57:2b:6d:1a:1e:42:c9:01.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.188.129' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Wed Apr 25 21:24:50 2018 from 192.168.188.1
3. Log in remotely and execute the command, then exit:
[root@weixing01 sbin]# vi 2.expect
#!/usr/bin/expect
set user "root"
set passwd "w4"
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@weixing01 sbin]# chmod a+x 2.expect
[root@weixing01 sbin]# ./2.expect
spawn ssh [email protected]
[email protected]'s password:
Last login: Wed Apr 25 21:30:12 2018 from 192.168.188.130
[root@weixing-02 ~]# touch /tmp/12.txt
[root@weixing-02 ~]# echo 1212 > /tmp/12.txt
Test run
[root@weixing-02 ~]# [root@weixing01 sbin]# ./1.expect
spawn ssh [email protected]
[email protected]'s password:
Last login: Wed Apr 25 21:36:07 2018 from 192.168.188.130
[root@weixing-02 ~]# ls -l /tmp/12.txt
-rw-r--r-- 1 root root 5 4月 25 21:36 /tmp/12.txt
[root@weixing-02 ~]# cat /tmp/12.txt
1212
4. Pass parameters:
[root@weixing01 sbin]# vim 3.expect
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "w4"
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@weixing01 sbin]# chmod a+x 3.expect
[root@weixing01 sbin]# ./3.expect root 192.168.188.129 ls
spawn ssh [email protected]
[email protected]'s password:
Last login: Wed Apr 25 21:36:46 2018 from 192.168.188.130
[root@weixing-02 ~]# ls
1.txt 2.txt anaconda-ks.cfg a.txt BugReport.txt grep
execute multiple commands
[root@weixing-02 ~]# [root@weixing01 sbin]# ./3.expect root 192.168.188.129 "ls;w;vmstat 1"
spawn ssh [email protected]
[email protected]'s password:
Last login: Wed Apr 25 21:44:15 2018 from 192.168.188.130
[root@weixing-02 ~]# ls;w;vmstat 1
1.txt 2.txt anaconda-ks.cfg a.txt BugReport.txt grep
21:44:43 up 20 min, 2 users, load average: 0.00, 0.01, 0.03
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.188.1 21:24 17:31 0.02s 0.02s -bash
root pts/1 192.168.188.130 21:44 0.00s 0.01s 0.00s 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 305904 2076 142944 0 0 102 9 111 162 0 1 99 0 0
0 0 0 305904 2076 142936 0 0 0 0 59 75 0 0 100 0 0
0 0 0 305904 2076 142936 0 0 0 16 68 80 0 0 100 0 0
0 0 0 305904 2076 142936 0 0 0 0 54 65 0 0 100 0 0