节省ls命令的结果,在本地机器上远程SFTP服务器 [英] save the result of ls command in the remote sftp server on local machine

查看:484
本文介绍了节省ls命令的结果,在本地机器上远程SFTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过<一个href=\"http://stackoverflow.com/questions/14150451/how-do-i-pipe-the-output-of-an-ls-on-remote-server-to-the-local-filesystem-via-s\">This问题但我不太满意的答案。我连接到远程SFTP服务器,我会尽LS那里,我想有我的本地机器上LS的结果。我不希望任何额外的东西要保存,ls命令仅结果。我可以将结果保存到一个变量访问本地计算机上?

 #!的/ usr / bin中/预期产卵SFTP myuser@myftp.mydomain.com
预计密码
发送输入mypassword \\ N的;
期望SFTP&gt;中
发送LS \\ n//保存在这里
相互作用


解决方案

下面是我会怎么做,有更多的传统的方法,需要打开一个文件写入所需的输出(我喜欢@kastelian的LOG_FILE理念,虽然):

 #!的/ usr / bin中/预期产卵SFTP myuser@myftp.mydomain.com
预计密码
发送输入mypassword \\ N的;
期望SFTP&gt;中设置文件[开放的/ tmp / LS-输出W];#打开进行写入并设置文件标识符期望*;#匹配缓冲任何以便清除它发送LS \\ R预计{
    SFTP&gt;中{
        把$文件$ expect_out(缓冲);#保存到文件,因为最后一场比赛(清)缓冲区内容,直到这场比赛(SFTP&GT;提示)
    }
    超时{;#几分优雅的方式,如果出现错误死
        把$文件错误:期待块超时
    }
}关闭$文件相互作用

生成的文件将持有相同的两个额外的行作为LOG_FILE提出的解决方案:ls命令在顶部和底部的SFTP>提示符下,但你应该可以,只要你喜欢处理那些<。 / p>

我测试了这一点,它的工作原理。

让我知道,如果它帮助!

I have seen This question but 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?

#!/usr/bin/expect

spawn sftp myuser@myftp.mydomain.com
expect "password:"
send "mypassword\n";
expect "sftp>"
send  "ls\n"  //save here
interact

解决方案

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):

#!/usr/bin/expect

spawn sftp myuser@myftp.mydomain.com
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.

I have tested this and it works.

Let me know if it helped!

这篇关于节省ls命令的结果,在本地机器上远程SFTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆