zoukankan      html  css  js  c++  java
  • expect自动远程拷贝脚本

    expect自动远程拷贝脚本,利用rsync命令,脚本内容如下:

    #!/usr/bin/expect --
    
    proc Usage_Exit {self} {
            puts ""
            puts "Usage: $self ip user passwd port sourcefile destdir direction bwlimit timeout"
            puts ""
            puts "       sourcefile: a file or directory to be transferred"
            puts "                   需要拷贝目录时目录名后不要带 /, 否则会拷贝该目录下的所有文件"
            puts "       destdir:    the location that the sourcefile to be put into"
            puts "       direction:  pull or push"
            puts "                   pull: remote -> local"
            puts "                   push: local -> remote"
            puts "       bwlimit:    bandwidth limit, kbit/s, 0 means no limit"
            puts "       timeout:    timeout of expect, s, -1 means no timeout"
            puts ""
            exit 1
    }
    
    if { [llength $argv] < 9 } {
            Usage_Exit $argv0
    }
    
    set ip [lindex $argv 0]
    set user [lindex $argv 1]
    set passwd [lindex $argv 2]
    set port [lindex $argv 3]
    set sourcefile [lindex $argv 4]
    set destdir [lindex $argv 5]
    set direction [lindex $argv 6]
    set bwlimit [lindex $argv 7]
    set timeoutflag [lindex $argv 8]
    
    set yesnoflag 0
    set timeout $timeoutflag
    
    for {} {1} {} {
    # for is only used to retry when "Interrupted system call" occured
    
    if { $direction == "pull" } {
    
            if { $bwlimit > 0 } {
                    spawn rsync -crazP --bwlimit=$bwlimit -e "/usr/bin/ssh -q -l$user -p$port" $ip:$sourcefile $destdir
            } elseif { $bwlimit == 0 } {
                    spawn rsync -crazP -e "/usr/bin/ssh -q -l$user -p$port" $ip:$sourcefile $destdir
            } else {
                    Usage_Exit $argv0
            }
    
    } elseif { $direction == "push" } {
    
            if { $bwlimit > 0 } {
                    spawn rsync -crazP --bwlimit=$bwlimit -e "/usr/bin/ssh -q -l$user -p$port" $sourcefile $ip:$destdir
            } elseif { $bwlimit == 0 } {
                    spawn rsync -crazP -e "/usr/bin/ssh -q -l$user -p$port" $sourcefile $ip:$destdir
            } else {
                    Usage_Exit $argv0
            }
    
    } else {
            Usage_Exit $argv0
    }
    
    expect  {
    
            "assword:" {
                    send "$passwd
    "
                    break;
            }
    
            "yes/no)?" {
                    set yesnoflag 1
                    send "yes
    "
                    break;
            }
    
            "FATAL" {
                    puts "
    CONNECTERROR: $ip occur FATAL ERROR!!!
    "
                    exit 1
            }
    
            timeout {
                    puts "
    CONNECTERROR: $ip Logon timeout!!!
    "
                    exit 1
            }
    
            "No route to host" {
                    puts "
    CONNECTERROR: $ip No route to host!!!
    "
                    exit 1
            }
    
            "Connection Refused" {
                    puts "
    CONNECTERROR: $ip Connection Refused!!!
    "
                    exit 1
            }
    
            "Connection refused" {
                    puts "
    CONNECTERROR: $ip Connection Refused!!!
    "
                    exit 1
            }
    
            "Host key verification failed" {
                    puts "
    CONNECTERROR: $ip Host key verification failed!!!
    "
                    exit 1
            }
    
            "Illegal host key" {
                    puts "
    CONNECTERROR: $ip Illegal host key!!!
    "
                    exit 1
            }
    
            "Connection Timed Out" {
                    puts "
    CONNECTERROR: $ip Logon timeout!!!
    "
                    exit 1
            }
    
            "Interrupted system call" {
                    puts "
    $ip Interrupted system call!!!
    "
            }
    }
    
    }
    
    if { $yesnoflag == 1 } {
            expect {
                    "assword:" {
                            send "$passwd
    "
                    }
    
                    "yes/no)?" {
                            set yesnoflag 2
                            send "yes
    "
                    }
            }
    }
    
    if { $yesnoflag == 2 } {
            expect {
                    "assword:" {
                            send "$passwd
    "
                    }
            }
    }
    
    expect {
            "assword:" {
                    send "$passwd
    "
                    puts "
    PASSWORDERROR: $ip Password error!!!
    "
                    exit 1
            }
    
            eof {
                    puts "OK_SCP: $ip
    "
                    exit 0;
            }
    }

    用法:

    ./scp.exp
    
    Usage: ./scp.exp ip user passwd port sourcefile destdir direction bwlimit timeout
    
           sourcefile: a file or directory to be transferred
                       需要拷贝目录时目录名后不要带 /, 否则会拷贝该目录下的所有文件
           destdir:    the location that the sourcefile to be put into
           direction:  pull or push
                       pull: remote -> local
                       push: local -> remote
           bwlimit:    bandwidth limit, kbit/s, 0 means no limit
           timeout:    timeout of expect, s, -1 means no timeout

     

  • 相关阅读:
    排序算法分析
    图论算法小结
    A*算法
    分支界限法的应用
    图的搜索策略
    最大二分匹配
    C++学习笔记(1)
    vscode简单c语言多文件编译
    c语言变量大小
    十大排序算法总结
  • 原文地址:https://www.cnblogs.com/L-H-R-X-hehe/p/3825321.html
Copyright © 2011-2022 走看看