[ 登录注册 ]

云计算

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

2016-08-25 21:31:50 admin 返回上一页

脚本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~~
文章来源:http://www.bozhiyue.com/cloud/2016/0825/432551.html
返回上一页    返回分类 上一篇:   下一篇:
相关