Skip to content

Instantly share code, notes, and snippets.

@Fang-Li
Created October 25, 2018 09:47
Show Gist options
  • Save Fang-Li/1828241cdc197f9559a5de0cdd206456 to your computer and use it in GitHub Desktop.
Save Fang-Li/1828241cdc197f9559a5de0cdd206456 to your computer and use it in GitHub Desktop.
expect tcl 免密上传脚本
#!/usr/bin/expect -f
##########################################################
# 1.service ip
# 2.User
# 3.userPassword
# 4.serverPath [server端路径]
# 5.localPath [本地路径] ... 多个值
#返回值:
# 0 成功
# 1 参数个数不正确
###########################################################
proc usage {} {
regsub ".*/" $::argv0 "" name
send_user "Usage:\n"
send_user "$name serviceip User userPassword serverPath localPath...\n"
exit 1
}
## 判断参数个数
if {[llength $argv] < 5} {
usage
}
#设置变量值
set severip [lindex $argv 0]
set User [lindex $argv 1]
set userPassword [lindex $argv 2]
set serverPath [lindex $argv 3]
set localPath [lindex $argv 4 end]
send ${userPassword}
#定义变量标记rsync连接时是否输入yes确认
set inputYes 0
#rsync -avz /etc/ 192.168.15.234:/home/7_8
spawn rsync -avz "-e ssh -p 36688" {*}[lrange $argv 4 end] ${User}@${severip}:${serverPath}
#spawn rsync -avz "-e ssh -p 36688" {*}[lrange $localPath 1 end] ${User}@${severip}:${serverPath}
#spawn rsync -avz "-e ssh -p 36688" {*}${localPath} ${User}@${severip}:${serverPath}
expect {
-nocase -re "yes/no" {
send -- "yes\r"
set inputYes 1
}
-nocase -re "assword: " {
send -- "${userPassword}\r"
interact
}
-nocase -re "Connection refused" {
send_error "Sftp services at ${ftpServerIp} is not active.\n"
exit 2
}
timeout {
send_error "Connect to sftp server ${ftpUser}@${ftpServerIp} timeout(10s).\n"
exit 8
}
}
#如果输入了yes确认,输入密码
if {$inputYes==1} {
expect {
-nocase -re "assword: " {
send -- "${userPassword}\r"
interact
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment