分发系统:shell 上线脚本expect实现远程传输文件,执行命令,系统上线等功能
expect 脚本远程登录
vim 1.expect
#! /usr/bin/expect
set host "192.168.91.129"
set passwd "1q2w3e"
spawn ssh [email protected]$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact *停留在远程机器上,如果想登录到远程机器上退出来,使用expect或者eof,如果都不加,立马就退出来
登录到aminglinux02上
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"
[[email protected] ~]# chmod a+x 2.expect
expect传递参数
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "1q2w3e"
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"
[[email protected] ~]# chmod a+x 3.expect
只传递一个参数
传递多个参数
原文地址:http://blog.51cto.com/13528516/2107908