linux - 执行远程命令并将输入保存到文件

标签 linux shell ssh telnet remote-execution

我需要通过 SSH 访问多个主机,执行特定命令 ( show ms info ) 并将输出捕获到文件中。我需要将该文件复制回我的 Linux 机器

我想使用sshexpect提供密码

我的问题是将输出保存到文本文件并同时循环大约 100 台机器。

最佳答案

它比你想象的更简单:

host1 $ ssh user@host2 ls > remote-output.txt
Enter passphrase for key '/home/user/.ssh/id_rsa':
host1 $ ls
remote-output.txt
host1 $

要为多个主机执行此操作,我建议使用 ssh-agent并设置授权键:

$ ssh-agent bash
$ ssh-add
Enter passphrase for /home/user/.ssh/id_rsa:
$ for h in host1 host2;do ssh $h ls > $h.txt; done
$ ls
host1.txt host2.txt
$

关于linux - 执行远程命令并将输入保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10701365/

相关文章:

python - 无法从 Windows 使用 Paramiko 登录 Linux 服务器

c - 知道 "inode number"后如何获取inode结构

linux - 如何使用 awk、perl 或 sed 从 LiveHTTPHeaders 输出中删除响应?

c - malloc 将 errno 设置为 EAGAIN

linux - Centos命令行编辑CSV文件

mysql - shell脚本不执行

bash - 'lsb_release -a' 在 bash 脚本中使用它时打印额外的一行

linux - 使用linux shell脚本使用sed替换 "\"

git - 如何从已知主机列表中删除永久添加的主机?

android - 任何有助于使用 Jenkins 构建 Android 应用程序