20.27 Distribution System Introduction
Remote interactive SSH login, remote execution command, configuration service.
20.28 Expect script Telnet
Yum Install-y expect
Automatic Telnet
#! /usr/bin/expect
Set Host "192.168.133.132"
Set passwd "123456"
Spawn ssh [email protected] $host
Expect {
"Yes/no" {send "yes\r"; Exp_continue}
"Password:" {send "$passwd \ r"}
}
Interact
Operation Process
[Email protected] sbin]# vim 1.expect
#!/usr/bin/expect
Set Host "192.168.106.165"
Set passwd "356666"
Spawn ssh [email protected] $host
Expect {
"Yes/no" {send "yes\r"; Exp_continue}
"Password:" {send "$passwd \ r"}
}
Interact
[Email protected] sbin]# chmod a+x 1.expect
[Email protected] sbin]#./1.expect
Spawn ssh [email protected]
The authenticity of host ' 192.168.106.165 (192.168.106.165) ' can ' t be established.
ECDSA key fingerprint is SHA256:G28ZF++YHMMWZQB9YLYBYPJGGZIEHU/T062FZHJTRHK.
ECDSA key fingerprint is md5:2e:db:58:71:93:d8:09:a1:d9:bf:ad:2a:60:ba:b2:a6.
Is you sure want to continue connecting (yes/no)? Yes
warning:permanently added ' 192.168.106.165 ' (ECDSA) to the list of known hosts.
[email protected] ' s password:
Last Login:tue Apr 15:57:54 2018 from 192.168.106.1
[Email protected] ~]#
20.29 Expect script remote execution command
After automatic telnet, execute the command and exit
#!/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"
Operation Process
[Email protected] sbin]# vim 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"
[[email protected] sbin]# chmod a+x 2.expect [[email protected] sbin]# ./2.expect spawn ssh [email protected][email protected]‘s password: Last login: Tue Apr 24 16:13:17 2018 from 192.168.106.160[[email protected] ~]# touch /tmp/12.txt[[email protected] ~]# echo 1212 > /tmp/12.txt[[email protected] ~]# [[email protected] sbin]#
20.30 Expect script pass parameters
Passing parameters
#!/usr/bin/expect
Set user [lindex $argv 0]
Set host [lindex $ARGV 1]
Set passwd "123456"
Set cm [lindex $argv 2]
Spawn ssh [email protected] $host
Expect {
"Yes/no" {send "yes\r"}
"Password:" {send "$passwd \ r"}
}
Expect "]"
Send "$cm \ r"
Expect "]"
Send "exit\r"
Operation Process
[[email protected] sbin]# vim 3.expect写入上面的传递参数 内容[[email protected] sbin]# chmod a+x 3.expect [[email protected] sbin]# ./3.expect root 192.168.106.165 lsspawn ssh [email protected][email protected]‘s password: Last login: Tue Apr 24 16:24:52 2018 from 192.168.106.160[[email protected] ~]# lsanaconda-ks.cfg mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz
2018-06-06 Linux Learning