#! /usr/冷酷的柠檬/expect
#文件名: executeSshCommand.sh
#作者: Lotomer
时间: 2017-08-26
#功能:执行远程命令ssh
#版本: 1.1
#参数:
# 1) remote_ip :必需,远程服务器IP
# 2) remote_user :必需,远程服务器用户
# 3) remote_password :必填字段,远程服务器用户密码
# 4) remote_command :必需在远程服务器上执行的命令
# 5) remote_password2:必需,远程服务器用户的备用密码
# 6) timeout )选项、连接超时、单位:秒,默认不超时
#例如:/executesshcommand.sh 192.168.1.3 root 123456 ' cat/proc/CPU info ' 65432110
--------------------
#版本号更新时间的说明
# 1.0 2017-08-26初版
# 1.1 2019-11-16增加返回值
--------------------
#set timeout 5
set host [lindex $argv 0]
set user [lindex $argv 1]
set pwd [lindex $argv 2]
set sshCommand [lindex $argv 3]
set pwd2 [lindex $argv 4]
set timeout [lindex $argv 5]
if {$timeout==''} {
#puts 'timeout is empty,use -1 '
set timeout -1
} else {
#puts 'timeout is $timeout '
设置时间输出$时间输出
}
spawn ssh $ user @ $ host ' $ ssh command '
expect { (
' *yes/no*' {send 'yesr '; exp_continue}
' *assword' {send '$pwdr '; set pwd [exec echo $pwd2]; exp_continue}
' ]#' { send '$sshCommandr '; exp_continue }
eof
}
#puts$expect_out(buffer ) )。
las sign [ wait ] pidspawnidos _ error _ flag value
#系统错误
if {$os_error_flag==-1} {
puts 'os errno: $value '
} else {
返回CMD的执行结果
exit $value
}