我是在Ubuntu下用的, 需要 apt-get install expect .
#!/usr/bin/expect
#@Filename: sendfile.expect
set timeout 10
set username root
set password yourpasswd
set hostname 192.168.1.2
set filename [lindex $argv 0]
spawn scp -r $filename $username@$hostname:/usr/local/
expect "password:"
send "$password\n"
expect eof
这么用:
./sendfile.expect 1.txt
expect 不同于shell的$1 $2方式传参数, set key value的方式, 参数不够的话可以参考这种方式增加:
set filename [lindex $argv 0]