[自动化] 命令行上传文件夹(SCP,SSH)

2018-08-23  本文已影响48人  Simline

因项目需要,一番搜索后得知可由expect命令进行自动化操作,感觉倍儿爽。所以写此案例,仅供大家参考。

#!/usr/bin/expect
# filename: upload.exp
# author: simline@163.com
set user        [lindex $argv 0 ]
set host        [lindex $argv 1 ]
set passwd      [lindex $argv 2 ]
set srcPath     [lindex $argv 3 ]
set desPath     [lindex $argv 4 ]
set dir         [lindex [split $srcPath /] end ]
set timeout     6000

spawn   echo $dir
spawn   scp -r $srcPath $user@$host:/tmp

expect  "password:"
send    "$passwd\r"
interact

spawn   ssh $user@$host
expect  "password:"
send    "$passwd\r"
expect  "$user"
send    "sudo su\r"
if {$expect_out(buffer) == "*password:"} {
    send    "$passwd\r"
}
expect  "root*"
send    "rm -rf $desPath && mv /tmp/$dir $desPath\r"
expect  "root"

macOS下执行,就是传参而已了

iMac:~ user$ ./upload.exp  远程主机用户名  远程主机IP 远程主机密码  本地路径  服务器端目标路径
上一篇下一篇

猜你喜欢

热点阅读