Installing expect
[[email protected] ~]# yum install -y expect
Use a script to log into a remote machine
Scripting 1.expect
The contents are as follows:
[[email protected] expect]# vim 1.expect#!/usr/bin/expectset host "192.168.130.128" #定义变量set passwd "6811327" #定义变量spawn ssh [email protected]$hostexpect {"yes/no" {send "yes\r"; exp_continue } #当提示语句出现yes/no 输入yes然后继续"password:" {send "$passwd\r" } #当提示语句出现password 输入密码}interact
Execution results
Log in to remote machine to execute command and exit
Scripting 2.expect
The contents are as follows:
[[email protected] expect]# vim 2.expect#!/usr/bin/expectset user "root"set passwd "6811327"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 "ls -l /tmp/12.txt\r"expect "]*"send "cat /tmp/12.txt\r"expect "]*"send "exit\r"
Execution results
[[email protected] expect]# chmod a+x 2.expect ; ./2.expectspawn ssh [email protected][email protected]‘s password: Last login: Wed Apr 25 08:07:55 2018 from 192.168.130.116[[email protected] ~]# touch /tmp/12.txt[[email protected] ~]# echo 1212 > /tmp/12.txt[[email protected] ~]# ls -l /tmp/12.txt-rw-r--r-- 1 root root 5 4月
Script Pass Parameters
Scripting 3.expect
The contents are as follows:
[[email protected] expect]# vim 3.expect#!/usr/bin/expectset user [lindex $argv 0]set host [lindex $argv 1]set passwd "6811327"set cm [lindex $argv 2]spawn ssh [email protected]$hostexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect "]*"send "$cm\r"expect "]*"send "exit\r"
Execution results
[[email protected] expect]# chmod a+x 3.expect;./3.expect root 192.168.130.128 lsspawn ssh [email protected][ Email protected] ' s password:last login:wed Apr 08:32:02 2018 from 192.168.130.116[[email protected] ~]# LSA naconda-ks.cfg com zabbix-release-3.2-1.el7.noarch.rpm[[email protected] expect]#./3.expect Root 192.168.130.128 "Ls;w;ps aux|grep sshd" Spawn ssh [email protected][email protected] ' s password:last login: Wed Apr 08:50:42 2018 from 192.168.130.116[[email protected] ~]# ls;w;ps aux|grep sshdanaconda-ks.cfg com Zabbix -release-3.2-1.el7.noarch.rpm 08:52:15 up 1:04, 2 users, load average:0.00, 0.01, 0.01USER TTY from [Email protected] IDLE jcpu PCPU whatroot pts/0 192.168.130.1 07:48 15.00s 0.01s 0.01s-bashroot pts/1 192.168.130. 08:52 0.00s 0.00s 0.00s wroot 826 0.0 0.4 105996 4128? Ss 07:48 0:00/usr/sbin/sshd-droot 947 0.00.5 148316 5376? Ss 07:48 0:00 sshd: [email protected]/0root 1098 0.0 0.5 148312 5504? Ss 08:52 0:00 sshd: [email protected]/1root 1118 0.0 0.0 112680 984 pts/1 s+ 08:52 0:00 grep--c Olor=auto sshd[[email protected] ~]# [[email protected] expect]#
Set timeout
Add a line below the command you executed
Set Timeout-1 #永不超时
Set timeout 1 #1秒超时
Set Timeout 5 #5秒超时
For example:
Expect "]*"
Send "Vmstat 1" #这个命令会一直执行
Set Timeout-1
Expect "]*"
Send "Vmstat 1" #这个命令会5秒后自动停止
Set Timeout 5
Automatically synchronize files
Writing scripts
The contents are as follows:
[[email protected] expect]# vim 4.expect#!/usr/bin/expectset passwd "6811327"spawn rsync -av [email protected]:/tmp/12.txt /tmp/expect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect eof
Execution results
[[email protected] expect]# chmod a+x 4.expect ; ./4.expectspawn rsync -av [email protected]:/tmp/12.txt /tmp/[email protected]‘s password: receiving incremental file list12.txtsent 30 bytes received 84 bytes 228.00 bytes/sectotal size is 5 speedup is 0.04[[email protected] expect]# cat /tmp/12.txt1212
Specify host and files to synchronize
Scripting 5.expect
The contents are as follows:
[[email protected] expect]# vim 5.expect#!/usr/bin/expectset passwd "6811327"set host [lindex $argv 0]set file [lindex $argv 1]spawn rsync -av $file [email protected]$host:$fileexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect eof
Execution results
[[email protected] expect]# chmod a+x 5.expect ; ./5.expect 192.168.130.128 "/tmp/12.txt"spawn rsync -av /tmp/12.txt [email protected]:/tmp/12.txt[email protected]‘s password: sending incremental file listsent 31 bytes received 12 bytes 86.00 bytes/sectotal size is 5
Shell Project-Distribution system-expect