Why does automating SFTP with Expect hang after sending the password?

痴心易碎 提交于 2019-12-23 02:52:53

问题


I am attempting to automate uploading some files from my Linux server to an FTP-enabled Windows server. I am successfully doing so manually using SFTP and then issuing the put command. However, when called from cron, my script keeps stopping for a password.

Below is the code I am attempting to use:

#!/usr/bin/expect
#!/bin/sh
clear
spawn sftp remoteuser@43.123.0.10
expect "password"
send "world"
expect eof

As it stands, it stops each time to request a password. Why doesn't send "world" complete the password dialog?

UPDATE:

#!/usr/bin/expect
#!/bin/sh
clear
spawn sftp remoteuser@43.123.0.10
expect "remoteuser@43.123.0.10's password:"
send "world"
expect eof

Now I get the following error:

xml_reports.sh: line 5: spawn: command not found
couldn't read file "remoteuser@43.123.0.10's password:": no such file or directory
xml_reports.sh: line 7: send: command not found
couldn't read file "eof": no such file or directory

回答1:


Try like this:

#!/bin/bash
expect -c "
spawn sftp remoteuser@XX.XX.XX.XX
expect \"password\"
send \"PASSWORD\r\"
interact "

Example : http://www.techtrunch.com/scripting/lazy-admins-part-2




回答2:


You need to use arguments when you run it. Read this article. It explains how to do this properly.




回答3:


What is happening is Expect is still waiting for the pattern to arrive (until timeout), it could be that "password" is not in the prompt of sftp and that it could be "Password" or something else.



来源:https://stackoverflow.com/questions/14159935/why-does-automating-sftp-with-expect-hang-after-sending-the-password

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!