#!/usr/bin/expect
# 脚本名称: sftp.sh
# 执行例程 expect sftp.sh '192.168.1.1' 'user' 'passwd' '/DXP/DATA/EDW/ROUT/BEIJING_2016-12-01.DAT' '/app/sftp/BCSS_FILE/BCSS/EC/201907'
if {$argc < 5} {
puts "Usage:cmd <host> <username> <password> <src_file> <dest_file>"
exit 1
}
set timeout 600
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set src_file [lindex $argv 3]
set dest_file [lindex $argv 4]
spawn sftp $username@$host
expect {
"yes/no" {send "yes\n"; exp_continue;}
"*password:" {send "$password\n"}
}
expect "sftp>" {send "cd $dest_file\n"}
expect {
"No such file or directory" {send "mkdir $dest_file\n";exp_continue;}
"sftp>" { send "put $src_file $dest_file\n"}
}
expect {
"No such file or directory" { send_user "No such file\n";send "quit\r";exit 2 }
"100%" { send_user "finish\n";send "quit\r";exit 0}
}
expect eof
支持原创
博客: https://blog.csdn.net/clark_xu
代码:https://github.com/clark99
微信公众号:clark_blog