• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

自动远程拷贝expect脚本

服务器 开心洋葱 3063次浏览 0个评论

自动远程拷贝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]  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\r"
                break;
        }

        "yes/no)?" {
                set yesnoflag 1
                send "yes\r"
                break;
        }

        "FATAL" {
                puts "\nCONNECTERROR: $ip occur FATAL ERROR!!!\n"
                exit 1
        }

        timeout {
                puts "\nCONNECTERROR: $ip Logon timeout!!!\n"
                exit 1
        }

        "No route to host" {
                puts "\nCONNECTERROR: $ip No route to host!!!\n"
                exit 1
        }

        "Connection Refused" {
                puts "\nCONNECTERROR: $ip Connection Refused!!!\n"
                exit 1
        }

        "Connection refused" {
                puts "\nCONNECTERROR: $ip Connection Refused!!!\n"
                exit 1
        }

        "Host key verification failed" {
                puts "\nCONNECTERROR: $ip Host key verification failed!!!\n"
                exit 1
        }

        "Illegal host key" {
                puts "\nCONNECTERROR: $ip Illegal host key!!!\n"
                exit 1
        }

        "Connection Timed Out" {
                puts "\nCONNECTERROR: $ip Logon timeout!!!\n"
                exit 1
        }

        "Interrupted system call" {
                puts "\n$ip Interrupted system call!!!\n"
        }
}

}

if { $yesnoflag == 1 } {
        expect {
                "assword:" {
                        send "$passwd\r"
                }

                "yes/no)?" {
                        set yesnoflag 2
                        send "yes\r"
                }
        }
}

if { $yesnoflag == 2 } {
        expect {
                "assword:" {
                        send "$passwd\r"
                }
        }
}

expect {
        "assword:" {
                send "$passwd\r"
                puts "\nPASSWORDERROR: $ip Password error!!!\n"
                exit 1
        }

        eof {
                puts "OK_SCP: $ip\n"
                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


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明自动远程拷贝expect脚本
喜欢 (0)

您必须 登录 才能发表评论!

加载中……