首页 > 解决方案 > Account locked due to 6 failed logins occurred when using expect script to call sftp to upload file

问题描述

I have written an expect script in which i call sftp to upload file and i pass sftpuser sftppass sftpip to the script. In shell, i call this script with same right parameter and it will upload file successfully five times. And in the sixth turn, it reports the following error: Account locked due to 6 failed logins

I doubt that i have passed the right password every time, why system treat it as an failure login?

The expect script looks like this:

#!/usr/bin/expect -f

set loginuser [lindex $argv 0]
set loginpass [lindex $argv 1]
set ipaddr [lindex $argv 2]
set remote_dir [lindex $argv 3]
set put_file [lindex $argv 4]
    
set timeout 300
set cmd_prompt "sftp>"


#-------------------------------------------------- login by ssh
spawn sftp $loginuser@$ipaddr
set timeout 300
expect {
-re "Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
} -re "assword:" {
send "$loginpass\r"
} -re "Permission denied, please try again." {
exit
} -re "Connection refused" {
exit
} timeout {
exit
} eof {
exit
}
}

expect {
-re "assword:" {
send "$loginpass\r"
}
-re $cmd_prompt {
send "\r"
}
}

exec sleep 1
expect {
-re $cmd_prompt {
send "cd $remote_dir\r"
}
}

#exec sleep 1
#expect {
#-re $cmd_prompt {
#send "ls\r"
#}
#}

exec sleep 1
expect {
-re $cmd_prompt {
send "put $put_file\r"
}
}

expect {
-re $cmd_prompt {
send "exit\r"
}
}

expect eof
#interact

The command calling this script in shell looks like this: ./autoftp.sh sftpuser sftpuserpass 172.11.11.11 upload ./test.dat

标签: uploadsftpexpectaccountlocked

解决方案


要查看到底发生了什么,请使用-d调试标志运行 expect 并仔细研究结果输出。

其他一些观察:

  • 您所有的期望模式都有-re标志,这意味着将它们视为正则表达式,但它们实际上不是正则表达式,除了第一个,问号会被误解。您应该使用该-ex标志进行精确匹配。
  • 除非您安装了非常旧的 Expect/Tcl,否则五行 [lindex $argv *] 可以压缩为lassign $argv loginuser loginpass ipaddr remote_dir put_file.
  • Expect 有自己的功能sleep,所以你不需要.exec sleep 1sleep 1
  • 而不是复制密码处理,当您匹配时"Are you sure you want to continue connecting (yes/no)?"将处理程序更改为
{
    send "yes\r"
    exp_continue
}

重新启动相同的期望命令。


推荐阅读