Shell脚本检测sshd服务状态

状态检测脚本,如果sshd不在运行中,重新启动:

#!/bin/bash
#jason.zhang@xxx.com
#DATE: 2020-10-22 10:42

SSHD_STATUS=`/etc/init.d/sshd status | grep pid | awk '{print $4}'`

if [ "$SSHD_STATUS" != "正在运行..." ]
        then
                echo `date`
                echo "sshd 服务已停止,正在重启..."
                /etc/init.d/sshd restart
fi

客户系统服务器(redhat)安全检测,显示使用的openssh存在漏洞,需要进行修复,升级到最新版本的openssh后,使用service sshd start无法启动sshd服务,使用/etc/init.d/sshd start可以启动,所以编写检测脚本,以便在查找解决方案时,解决sshd重启失败的问题(ssh远程登录失败时,需要联系客户登录到服务器管理后台进行重启)。

使用service sshd start 启动时会出现:

Starting sshd:sshd no hostkeys available -- existing

找到解决方案都是使用 ssh-keygen 生成 dsa_keyrsa_key

ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

或者

ssh-keygen -A

并检查文件权限为 600:

[root@JT-T-KEYAN-001 ~]# ll /etc/ssh/
总用量 608
-rw------- 1 root root 577834 10月 21 16:44 moduli
-rw-r--r-- 1 root root   1531 10月 21 16:44 ssh_config
-rw------- 1 root root   3179 10月 21 16:53 sshd_config
-rw------- 1 root root   1381 10月 21 19:02 ssh_host_dsa_key
-rw-r--r-- 1 root root    609 10月 21 19:02 ssh_host_dsa_key.pub
-rw------- 1 root root    513 10月 21 19:02 ssh_host_ecdsa_key
-rw-r--r-- 1 root root    181 10月 21 19:02 ssh_host_ecdsa_key.pub
-rw------- 1 root root    411 10月 21 19:02 ssh_host_ed25519_key
-rw-r--r-- 1 root root    101 10月 21 19:02 ssh_host_ed25519_key.pub
-rw------- 1 root root   2610 10月 21 19:02 ssh_host_rsa_key
-rw-r--r-- 1 root root    573 10月 21 19:02 ssh_host_rsa_key.pub
[root@JT-T-KEYAN-001 ~]#

尝试后并未解决问题。

发表评论

您的电子邮箱地址不会被公开。