How can I ssh inside a Perl script?

后端 未结 9 1037
猫巷女王i
猫巷女王i 2020-12-09 12:14

I want to SSH to a server and execute a simple command like \"id\" and get the output of it and store it to a file on my primary server. I do not have privileges to install

9条回答
  •  自闭症患者
    2020-12-09 12:51

    I had a similar issue, and after much searching I've found a simple option. I used qx() and ran the ssh commands like I normally would. The catch is I had to capture both stderr and stdout.

    The following is an example of what I used:

    my $output = qx(ssh root\@$curIP python -V 2>&1);
    

    It runs the python -V command, which outputs the version info to stderr. In this example, my ip address was stored in the $curIP variable. Lastly, the 2>&1 helps capture both stdout and stderr. I did not specify a password, as I have key exchanges setup. Hope this helps.

提交回复
热议问题