bash 将 ls 命令的结果保存在本地机器上的远程 sftp 服务器中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21052944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
save the result of ls command in the remote sftp server on local machine
提问by Sara
I have seen This questionbut I am not satisfied with answer. I connect to remote sftp server and I will do ls there I want to have the result of ls on my local machine. I don't want any thing extra to be saved, only the result of ls command. Can I save the result to a variable accessible on the local machine?
我看过 这个问题,但我对答案不满意。我连接到远程 sftp 服务器,我会在那里做 ls 我想在我的本地机器上得到 ls 的结果。我不想保存任何额外的东西,只保存 ls 命令的结果。我可以将结果保存到本地机器上可访问的变量中吗?
#!/usr/bin/expect
spawn sftp [email protected]
expect "password:"
send "mypassword\n";
expect "sftp>"
send "ls\n" //save here
interact
回答by Mindaugas Kubilius
Try sending ls
output to the log file:
尝试将ls
输出发送到日志文件:
spawn sftp [email protected]
expect "password:"
send "mypassword\n";
expect "sftp>"
log_file -noappend ls.out
send "ls\n" //save here
expect "sftp>"
log_file
interact
log_file -noappend ls.out
triggers logging program output, and later log_file
without arguments turns it off. Need to expect another sftp prompt otherwise it won't log the output.
log_file -noappend ls.out
触发日志程序输出,然后在log_file
没有参数的情况下将其关闭。需要期待另一个 sftp 提示,否则它不会记录输出。
There will be two extra lines in log - first line is ls
command itself, last line is sftp>
prompt. You can filter them out with something like sed -n '$d;2,$p' log.out
. Then you can slurp the contents of the file into shell variable, remove temp file, etc. etc.
日志中会有两行额外的行——第一行是ls
命令本身,最后一行是sftp>
提示。你可以用类似的东西过滤掉它们sed -n '$d;2,$p' log.out
。然后你可以将文件的内容放入 shell 变量中,删除临时文件等。
回答by James
Here's how I would do it, with a more "traditional" approach that involves opening a file for writing the desired output (I like @kastelian 's log_file idea, though):
这是我将如何做到这一点,使用更“传统”的方法,包括打开一个文件来写入所需的输出(不过,我喜欢 @kastelian 的 log_file 想法):
#!/usr/bin/expect
spawn sftp [email protected]
expect "password:"
send "mypassword\n";
expect "sftp>"
set file [open /tmp/ls-output w] ;# open for writing and set file identifier
expect ".*" ;# match anything in buffer so as to clear it
send "ls\r"
expect {
"sftp>" {
puts $file $expect_out(buffer) ;# save to file buffer contents since last match (clear) until this match (sftp> prompt)
}
timeout { ;# somewhat elegant way to die if something goes wrong
puts $file "Error: expect block timed out"
}
}
close $file
interact
The resulting file will hold the same two extra lines as in the log_file proposed solution: the ls command at the top, and the sftp> prompt at the bottom, but you should be able to deal with those as you like.
生成的文件将包含与 log_file 建议的解决方案中相同的额外两行:顶部的 ls 命令和底部的 sftp> 提示符,但您应该能够随心所欲地处理它们。
I have tested this and it works.
我已经测试过这个并且它有效。
Let me know if it helped!
如果有帮助,请告诉我!
回答by Guntram Blohm supports Monica
You can use echo 'ls -1' | sftp <hostname> > files.txt
. Or, if you really want a shell variable (not recommended if you have a long file list), try varname=$(echo 'ls -1' | sftp <hostname>)
.
您可以使用echo 'ls -1' | sftp <hostname> > files.txt
. 或者,如果你真的想要一个 shell 变量(如果你有一个很长的文件列表,不推荐),试试varname=$(echo 'ls -1' | sftp <hostname>)
.