我看过这个问题,但对答案不满意。我连接到远程sftp服务器,然后在这里执行ls,我希望在本地计算机上获得ls的结果。我不想保存任何额外的东西,只希望保存ls命令的结果。我可以将结果保存到本地计算机上可访问的变量中吗?
#!/usr/bin/expect
spawn sftp [email protected]
expect "password:"
send "mypassword\n";
expect "sftp>"
send "ls\n" //save here
interact
使用更传统的方法(包括打开文件以写入所需的输出),这就是我的方法(不过,我喜欢@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
生成的文件将保留与建议的log_file解决方案相同的两行:顶部的ls命令和底部的sftp>提示符,但是您应该可以根据需要进行处理。
我已经对此进行了测试,并且有效。
让我知道是否有帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句