shell方式获取SSH版本

ssh -V的输出不是标准输出,通过shell、awk、cut等方式获取版本号,无法得到想要的结果,要加上2>&1。

不加2>&1 使用awk命令:

[root@localhost ~]# ssh -V  | awk -F "," '{print $1}'
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
[root@localhost ~]#

 

加2>&1 使用awk命令:

[root@localhost ~]# ssh -V 2>&1 | awk -F "," '{print $1}' 
OpenSSH_7.4p1
[root@localhost ~]#

 

============================================================================

 

不加2>&1 ,shell脚本:

[root@localhost ~]# vim ssh.sh
#!/usr/bin/env bash

ssh_version=$(ssh -V | awk -F "," '{print $1}')
echo "SSH Version: $ssh_version"

运行结果:

[root@localhost ~]# bash ssh.sh 
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
SSH Version: 
[root@localhost ~]# 

 

加2>&1 ,shell脚本:

#!/usr/bin/env bash

ssh_version=$(ssh -V 2>&1 | awk -F "," '{print $1}')
echo "SSH Version: $ssh_version"

运行结果:

[root@localhost ~]# bash ssh.sh 
SSH Version: OpenSSH_7.4p1
[root@localhost ~]#

 

 

参考:

https://blog.csdn.net/u013107656/article/details/84567103

posted @ 2021-01-23 10:10  idongqing  阅读(823)  评论(0编辑  收藏  举报