博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自动远程拷贝expect脚本
阅读量:4108 次
发布时间:2019-05-25

本文共 4578 字,大约阅读时间需要 15 分钟。

转自

http://www.75271.com/1266.html

自动远程拷贝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

转载地址:http://ampsi.baihongyu.com/

你可能感兴趣的文章
oracle11g安装教程
查看>>
oracle将时间加一天,加小时,加分,加秒
查看>>
idea整合svn
查看>>
idea设置类注释和方法注释
查看>>
BlockingQueue(阻塞队列)详解
查看>>
阻塞队列实现日志接口开发
查看>>
oracle 中 to_date 函数的用法
查看>>
解决maven创建web项目过慢的问题
查看>>
let definitions are not supported by current javascript
查看>>
通过idea将maven工程转为web项目
查看>>
聊一聊 SpringBoot 自动配置的原理
查看>>
将文字自动转为banner打印形式的工具
查看>>
分布式的cap原理
查看>>
单点登录的原理
查看>>
react使用moment进行日期格式化
查看>>
Java 中 PO 与 VO 的区别
查看>>
idea创建maven的web项目
查看>>
单例模式中,你不知道的事~~
查看>>
使用xfire搭建webservice服务
查看>>
idea常用快捷键
查看>>