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

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

作者: Simline | 来源:发表于2018-08-23 01:35 被阅读48次

    因项目需要,一番搜索后得知可由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 远程主机密码  本地路径  服务器端目标路径
    

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/znguiftx.html