Save ssh -V to variable

余生长醉 提交于 2021-02-05 06:00:20

问题


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.

OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008

OR

sshg3: SSH Tectia Client 6.1.8 on x86_64-unknown-linux-gnu
Build: 136
Product: SSH Tectia Client License
type: commercial

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

回答1:


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)


来源:https://stackoverflow.com/questions/19118724/save-ssh-v-to-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!