简体   繁体   English

通过 ssh 运行多个命令并将输出存储在不同的文件中

[英]running multiple commands through ssh and storing the outputs in different files

i've set up my public and private keys and have automated ssh login.我已经设置了我的公钥和私钥并自动进行了 ssh 登录。 I want to execute two commands say command1 and command2 in one login session and store them in files command1.txt and command2.txt on the local machine.我想在一个登录会话中执行两个命令,比如 command1 和 command2,并将它们存储在本地机器上的文件 command1.txt 和 command2.txt 中

i'm using this code我正在使用此代码

ssh -i my_key user@ip 'command1 command2' and the two commands get executed in one login but i have no clue as to how to store them in 2 different files. ssh -i my_key user@ip 'command1 command2'并且这两个命令在一次登录中执行,但我不知道如何将它们存储在 2 个不同的文件中。

I want to do so because i dont want to repeatedly ssh into my remote host.我想这样做是因为我不想重复 ssh 到我的远程主机。

Unless you can parse the actual outputs of the two commands and distinguish which is which, you can't.除非您可以解析两个命令的实际输出并区分哪个是哪个,否则您不能。 You will need two separate ssh sessions:您将需要两个单独的 ssh 会话:

ssh -i my_key user@ip command1 > command1.txt
ssh -i my_key user@ip command2 > command2.txt

You could also redirect the outputs to files on the remote machine and then copy them to your local machine:您还可以将输出重定向到远程机器上的文件,然后将它们复制到您的本地机器上:

ssh -i my_key user@ip 'command1 > command1.txt; command2 > command2.txt'
scp -i my_key user@ip:'command*.txt' .

NO, you will have to do it separately in separate command (multiple login) as already mentioned by @lanzz.不,正如@lanzz 已经提到的那样,您必须在单独的命令(多次登录)中单独执行此操作。 To save the output in local, do like要将输出保存在本地,请执行以下操作

ssh -i my_key user@ip "command1" > .\file_on_local_host.txt

In case, you want to run multiple command in a single login, then jot all your command in a script and then run that script through SSH, instead running multiple command.如果您想在一次登录中运行多个命令,然后将所有命令记在一个脚本中,然后通过 SSH 运行该脚本,而不是运行多个命令。

It's possible, but probably more trouble than it's worth.这是可能的,但可能比它的价值更麻烦。 If you can generate a unique string that is guaranteed not to be in the output of command1, you can do:如果可以生成保证不在 command1 输出中的唯一字符串,则可以执行以下操作:

$ ssh remote 'cmd1; echo unique string; cmd2' |
  awk '/^unique string$/ { output="cmd2"; next } { print > output }' output=cmd1

This simply starts printing to the file cmd1 , and then changes output to the file cmd2 when it sees the unique string.这只是开始打印到文件cmd1 ,然后在看到唯一字符串时将输出更改为文件cmd2 You'll probably want to handle stderr as well.您可能还想处理 stderr。 That's left as an exercise for the reader.这留给读者作为练习。

You can do this.你可以这样做。 Assuming you can set up authentication from the remote machine back to the local machine, you can use ssh to pipe the output of the commands back.假设您可以设置从远程机器返回到本地机器的身份验证,您可以使用 ssh 将命令的输出通过管道传回。 The trick is getting the backslashes right.诀窍是正确使用反斜杠。

ssh remotehost command1 \| ssh localhost cat \\\> command1.txt \; command2 \| ssh localhost cat \\\> command2.txt 

Or if you aren't so into backslashes...或者如果你不那么喜欢反斜杠......

ssh remotehost 'command1 | ssh localhost cat \> command1.txt ; command2 | ssh localhost cat \> command2.txt' 

option 1. Tell your boss he's being silly.选项 1. 告诉你的老板他很傻。 Unless, of course, he isn't and there is critical reason of needing it all in one session.除非,当然,他不是,并且有一个关键的原因需要在一个会话中全部完成。 For some reason such a case escapes my imagination.出于某种原因,这种情况超出了我的想象。

option 2. why not tar?选项 2. 为什么不使用 tar?

ssh -i my_key user@ip 'command1 > out1; command2 > out2; tar cf - out*' | tar xf -

join them using && so you can have it like this使用 && 加入他们,这样你就可以像这样拥有它

ssh -i my_key user@ip "command1 > command1.txt && command2 > command2.txt && command3 > command3.txt"

Hope this helps希望这可以帮助

I was able to, here's exactly what I did:我能够做到,这正是我所做的:

ssh root@your_host "netstat -an;hostname;uname -a"

This performs the commands in order and cat'd them onto my screen perfectly.这会按顺序执行命令并将它们完美地显示在我的屏幕上。

Make sure you start and finish with the quotation marks, else it'll run the first command remotely then run the remainder of the commands against your local machine.确保以引号开始和结束,否则它将远程运行第一个命令,然后针对本地计算机运行其余命令。

I have an rsa key pair to my server, so if you want to avoid credential check then obviously you have to make that pair.我的服务器有一个 rsa 密钥对,所以如果你想避免凭据检查,那么显然你必须制作那对。

I think this is what you need: At first you need to install sshpass on your machine.我认为这就是你所需要的:首先你需要在你的机器上安装sshpass then you can write your own script:然后你可以编写自己的脚本:

while read pass port user ip; do
sshpass -p$pass ssh -p $port $user@$ip <<ENDSSH1
    COMMAND 1  > file1
    .
    .
    .
    COMMAND n  > file2
ENDSSH1
done <<____HERE
    PASS    PORT    USER    IP
      .      .       .       .
      .      .       .       .
      .      .       .       .
    PASS    PORT    USER    IP    
____HERE

How to run multiple command on remote server using single ssh conection.如何使用单个 ssh 连接在远程服务器上运行多个命令。

[root@nismaster ~]# ssh 192.168.122.169 "uname -a;hostname" root@192.168.122.169's password: Linux nisclient2 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux nisclient2 [root@nismaster ~]# ssh 192.168.122.169 "uname -a;hostname" root@192.168.122.169 的密码:Linux nisclient2 2.6.18-164.el5 #1 SMP Tue Aug 18 15:52064 EDT i686 i386 GNU/Linux nisclient2

    OR

[root@nismaster ~]# ssh 192.168.122.169 "uname -a && hostname" root@192.168.122.169's password: Linux nisclient2 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux nisclient2 [root@nismaster ~]# ssh 192.168.122.169 "uname -a && hostname" root@192.168.122.169 的密码:Linux nisclient2 2.6.18-164.el5 #1 SMP Tue Aug 18 15:52004 EDT i686 i386 GNU/Linux nisclient2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM