[英]Linux Expect SFTP + put file
I just got some help in automating my password input during an SFTP connection by using Expect .我刚刚在SFTP连接期间使用Expect自动输入密码方面获得了一些帮助。
I then tried to issue a put command and things got complicated again.然后我尝试发出一个 put 命令,事情又变得复杂了。 I'm using the following code:
我正在使用以下代码:
#!/usr/bin/expect
#!/bin/sh
expect -c "
spawn sftp remoteuser@*.*.*.*
expect \"remoteuser@*.*.*.*'s password:\"
send \"passwrd\r\"
interact "
echo "put output/data.xml\r"
echo "quit\r"
My password input and connection to the remote server works just fine but I am struggling to get the put output/data.xml command to display and execute at the SFTP prompt.我的密码输入和与远程服务器的连接工作正常,但我正在努力让put output/data.xml命令在 SFTP 提示符下显示和执行。
My echo "put output/data.xml\r" and echo "quit\r" lines just print as text which is not entirely surprising but I really don't know what else might work.我的echo "put output/data.xml\r"和echo "quit\r"行只是打印为文本,这并不完全令人惊讶,但我真的不知道还有什么可能有效。
I understand you need some help, but you should at least try doing some reading.我知道你需要一些帮助,但你至少应该尝试做一些阅读。 When you say
expect -c 'some stuff...'
, you are specifying the entire expect script as the parameter to the -c
option.当您说
expect -c 'some stuff...'
时,您将整个expect 脚本指定为-c
选项的参数。 The Expect session will not exist afterward. Expect session 之后将不存在。
Also, try to understand what the interact
command does instead of blindly applying what someone advises.另外,尝试理解
interact
命令的作用,而不是盲目地应用别人的建议。
Anyway, the solution:无论如何,解决方案:
expect <<'END_EXPECT'
set timeout -1
spawn sftp remoteuser@1.2.3.4
expect "[Pp]assword:"
send "passwrd\r"
expect "whatever the sftp prompt looks like"
send "put output/data.xml\r"
expect "whatever the sftp prompt looks like"
send "quit\r"
expect eof
END_EXPECT
Note that the here-doc delimiter is quoted when you first see it.请注意,当您第一次看到 here-doc 分隔符时,它会被引用。 That way, you will not be subjected to any shell expansions.
这样,您将不会受到任何 shell 扩展的影响。 It's like the entire here-doc is single-quoted.
就像整个 here-doc 都是单引号一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.