本文介绍了将ssh -V保存到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动将无密码ssh的测试从72台远程服务器返回到中央服务器.我的中央服务器无密码ssh可以在72台服务器上工作,但是需要它从它们返回中央服务器.

72个服务器具有两个ssh版本之一.

OR

我遇到的问题是尝试将 ssh -V 保存到变量中,看来它无法打印到STDOUT.因此,我在下面的尝试失败了.

  ssh -V>someFile.txtssh_version = $(ssh -V) 

如何轻松保存 ssh -V 的输出,以便可以调用适当的ssh批处理选项?

下面是我用于远程测试的脚本.

 #!/bin/shssh -V>/tmp/ssh_version_check.txt猫/tmp/ssh_version_check.txt |grep"OpenSSH"rc = $?如果[[$ rc == 0]]然后ssh -o BatchMode = yes< central_server>测试-d/tmp"rc = $?如果[[$ rc!= 0]]然后echo"$(主机名)失败">>/tmp/failed_ssh_test.txt科幻别的ssh -B< central_server>测试-d/tmp"rc = $?如果[[$ rc!= 0]]然后echo"$(主机名)失败">>/tmp/failed_ssh_test.txt科幻科幻 
解决方案

ssh -V 输出到 STDERR ,而不是 STDOUT .>

不用说

  ssh -V>/tmp/ssh_version_check.txt 

  ssh -V&&/tmp/ssh_version_check.txt 

  ssh -V>/tmp/ssh_version_check.txt 2>& 1 


要保存到变量,请说:

  ssh_version = $(ssh -V 2>& 1) 

I am trying to automate the testing of passwordless ssh from 72 remote servers back to a central server. I have central server passwordless ssh working to the 72 servers, but need it working from them back the the central server.

The 72 servers have one of two ssh versions.

OR

The issue I am experience is trying to save ssh -V into a variable, it seems that it does not print to STDOUT. Thus my attempts below are failing.

ssh -V > someFile.txt
ssh_version=$(ssh -V)

How can I easily save output of ssh -V so that the appropriate ssh batch option can be called?

Below is the script I am using for remote testing.

#!/bin/sh
ssh -V > /tmp/ssh_version_check.txt

cat /tmp/ssh_version_check.txt | grep "OpenSSH"
rc=$?

if [[ $rc == 0 ]]
then
    ssh -o BatchMode=yes <central_server> "test -d /tmp"
    rc=$?
    if [[ $rc != 0 ]]
    then
            echo "$(hostname) failed" >> /tmp/failed_ssh_test.txt
    fi
else
    ssh -B <central_server> "test -d /tmp"
    rc=$?
    if [[ $rc != 0 ]]
    then
            echo "$(hostname) failed" >> /tmp/failed_ssh_test.txt
    fi
fi
解决方案

ssh -V outputs to STDERR, not STDOUT.

Instead of saying

ssh -V > /tmp/ssh_version_check.txt

say

ssh -V >& /tmp/ssh_version_check.txt

or

ssh -V > /tmp/ssh_version_check.txt 2>&1


In order to save to a variable, say:

ssh_version=$(ssh -V 2>&1)

这篇关于将ssh -V保存到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:30