远程上传或获取文件,并打印出来

来源:互联网 发布:港币贬值 知乎 编辑:程序博客网 时间:2024/06/05 18:41
脚本remote_get_file_and_output.sh内容如下:
#!/usr/bin/expect
set HOSTIP [lindex $argv 0]
set LOGIN [lindex $argv 1]
set PASS [lindex $argv 2]
set DIR [lindex $argv 3]
set LOCALPATH [lindex $argv 4]
set FILE [lindex $argv 5]
set CMD [lindex $argv 6]
set timeout 10
set prompt "* >"

if {$argc<7} {
        puts stderr "Usage:$argv0 hostip login password destdir srcdir file cmd.\n"
        exit 1
}


spawn sftp $LOGIN@$HOSTIP
expect {
"*yes/no)" {send "yes\r";exp_continue}
}
expect {
"Password:*" {send "$PASS\r";exp_continue}
}
expect "sftp>"
send "cd $DIR\r"
expect "sftp>"
send "lcd $LOCALPATH\r"
expect "sftp>"
send "put $FILE\r"
expect "* Transfer complete."
send "exit\r"


spawn ssh $LOGIN@$HOSTIP
expect {
"*yes/no)" {send "yes\r";exp_continue}
}
expect {
"Password:*" {send "$PASS\r";exp_continue}
}
expect "$prompt"
send "$CMD\r"
expect "eof"
send "exit\r"


测试:
wad192:~ # ./test.sh 10.137.10.243 root password "/home" "/home" "hellos" "cat /home/hellos" 
spawn sftp root@192.168.9.243
Connecting to 192.168.9.243...
Password: 
sftp> cd /home
sftp> lcd /home
sftp> put hellos
Uploading hellos to /home/hellos
hellos                                                                                                                                                                  100%    0     0.0KB/s   00:00    
sftp> spawn ssh root@10.137.10.243
Password: 
Last login: Sat Oct 12 13:30:41 2013 from 10.137.10.192
wad243:~ # cat /home/hellos
Happy  QQ~~
原创粉丝点击