脚本:
#!/usr/bin/expect
#参数数量判断
if {$argc < 2} {
puts "输入格式:./scp.sh <主机Ip列表文件> <待上传的文件>"
exit 1
}
#超时时间
set timeout 10
set list_file [ lindex $argv 0 ]
set waitUpFile [ lindex $argv 1 ]
send_user $list_file
send_user "\r\n"
send_user $waitUpFile
send_user "\r\n"
#轮询索引
set index 0
#读取文件
set fp [open $list_file r]
puts "============== start ==============="
#一行一行读取
while {[gets $fp line] != -1} {
incr index
#解析单行内容
set arr [split $line ","]
set name [lindex $arr 0]
set Ip [lindex $arr 1]
set PassWd [lindex $arr 2]
set targetDir [lindex $arr 3]
puts "\n------------------- 处理第: $index 条:$name $Ip"
#上传文件
spawn scp -o ConnectTimeout=$timeout $waitUpFile root@$Ip:$targetDir
expect {
"yes/no" {
send "yes\n";exp_continue
}
"password" {
send "$PassWd\n"
}
"lost connection" {
send_user "------------------- 链接失败:"
}
}
expect eof
sleep 1
}
close $fp
puts "============== end ==============="
上传结果图:
上传 文件配置:
分别是:设备名,Ip,密码,远端目录。
用逗号隔开。