【问题标题】:How to use ssh tail command and write to local file如何使用 ssh tail 命令写入本地文件
【发布时间】:2015-07-01 13:17:10
【问题描述】:

我正在尝试编写一个脚本,从 3 台 linux 机器读取日志并将日志(带有某种前缀)写入一个文件。

我的问题是我可以访问该文件以查看它,但是当我尝试写入时它不起作用。

工作代码

expect -c "
spawn ssh user@x.x.x.x \" tail -f my_file\"
expect { 
   \"*assword\" {send \"PASS\r\";}
}
expect eof 
"

如果我添加

tail -f >> my_file.log

它不起作用。

【问题讨论】:

  • 你的意思是 tail -f filename 在这两种情况下我希望。
  • @meuh - 你是对的

标签: linux bash shell ssh


【解决方案1】:

首先,我强烈建议您学习如何通过密钥使用 SSH,这样您就不需要密码了。

那么,你需要做的就是:

ssh username@server 'tail -f filename' >> my_file.log

【讨论】:

  • 我知道我可以在机器之间创建密钥,但在这种情况下我不能。您还有其他解决方案吗?
  • @Ofer:使用expect设置密钥,然后使用我的解决方案。
【解决方案2】:

你的命令

tail -f filename >> my_file.log

正在遥控器上运行。所以 my_file.log 在远程。要将输出获取到本地文件,请将 >> my_file.log 移动到期望脚本的末尾:

 expect -c "..." >> my_file.log

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多