分发系统:shell 上线脚本expect实现远程传输文件,执行命令,系统上线等功能
expect 脚本远程登录
vim 1.expect
#! /usr/bin/expect
set host "192.168.91.129"
set passwd "1q2w3e"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact *停留在远程机器上,如果想登录到远程机器上退出来,使用expect或者eof,如果都不加,立马就退出来
分发系统expect远程登录,执行命令,传递参数_第1张图片

登录到aminglinux02上
分发系统expect远程登录,执行命令,传递参数

expect脚本远程执行命令
vim 2.expect
#!/usr/bin/expect
set user "root"
set passwd "1q2w3e"
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@aminglinux-001 ~]# chmod a+x 2.expect
分发系统expect远程登录,执行命令,传递参数_第2张图片

expect传递参数
#!/usr/bin/expect

set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "1q2w3e"
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@aminglinux-001 ~]# chmod a+x 3.expect
只传递一个参数
分发系统expect远程登录,执行命令,传递参数_第3张图片
传递多个参数
分发系统expect远程登录,执行命令,传递参数_第4张图片