centos6/7自动修改sshd端口

源代码来自于博客https://blog.csdn.net/matchbox1234/article/details/110930656
修改获取进程部分,增加防火墙开放端口,增加注释原来端口。

#!/bin/bash
#说明:SSHD服务默认监听端口是22,如果你不强制说明别的端口,”Port 22”注不注释都是开放22访问端口

portset=$1

if [ ! -z "$portset" ];then
	inputportlen=`echo "$portset"|sed 's/[0-9]//g'`
	#$portlen为空,说明输入的是一个整数
	if [ "$inputportlen" == "" ] && [ "$portset" -gt "1" ] && [ "$portset" -lt "65535" ];then #判断用户输入是否是1-65535之间个一个整数
		echo "--> 端口号输入正确"
		backup_sshd_config ()
		{
			#获取当前日期和时间
			dateAndTime=`date +"%Y%m%d%H%M%S"`
			echo "--> 开始备份/etc/ssh/sshd_config文件"
			/bin/cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$dateAndTime && echo "--> /etc/ssh/sshd_config文件备份成功" || { echo "--> /etc/ssh/sshd_config文件备份失败"; ExitCode=1; }
			bakfile=`/bin/ls /etc/ssh | grep $dateAndTime`
			echo "--> /etc/ssh/sshd_config文件备份结束,文件名为:$bakfile"
		}
		sshd_service_restart ()
		{
			systemver=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
			if [[ $systemver = "6" ]];then
				/sbin/iptables -I INPUT -p tcp --dport $portset -j ACCEPT  && /etc/init.d/iptables save  && service iptables restart
				service sshd restart && echo "--> sshd服务重启完成" || { echo "--> sshd服务重启失败"; ExitCode=1; }
			else
				firewall-cmd --permanent --zone=public --add-port=$portset/tcp  && firewall-cmd --reload
				systemctl restart sshd.service  && echo "--> sshd服务重启完成" || { echo "--> sshd服务重启失败"; ExitCode=1; }
			fi
		}
		#获取sshd运行进程ID
		cent=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
		if [[ $cent = "6" ]];then
			#getSSHProcessID=`ps -ef | grep sshd | awk '{if($3=="1" && $8=="/usr/sbin/sshd")print $2}'`
			getSSHProcessID=`ps aux|grep sshd|grep sbin|awk '{print $2}'`
		else
			#getSSHProcessID=`ps -ef | grep sshd | awk '{if($3=="1" && $9=="/usr/sbin/sshd")print $2}'`
			getSSHProcessID=`ps aux|grep sshd|grep sbin|awk '{print $2}'`
		fi
		if [ "$getSSHProcessID" != "" ];then #$getSSHProcessID不为空说明sshd服务启动正常
			#获取sshd打开的端口列表
			getSSHOpenPortList=`netstat -anop | grep $getSSHProcessID | grep ^tcp | grep LISTEN | grep -v ::: | grep sshd | awk '{print $4}' | awk -F ":" '{print $2}' | uniq | xargs echo`
			#计算sshd打开的端口数量
			getSSHOpenPortCount=`netstat -anop | grep $getSSHProcessID | grep -v ::: | grep sshd | grep LISTEN | awk '{print $4}' | awk -F ":" '{print $2}' | uniq | wc -l`
			if [ "$getSSHOpenPortCount" == "1" ] && [ "$getSSHOpenPortList" == "$portset" ];then #如果当前只打开了一个端口,且与希望设置的端口相同,无需做任何配置
				echo "sshd服务运行端口为$portset,无需修改!!!"
				exit 0
			elif [ "$getSSHOpenPortCount" == "1" ] && [ "$getSSHOpenPortList" == "22" ];then #如果端口为22说明使用的是默认的#Port 22设置,则增加Port设置
				listenportlent=`netstat -ano | grep -w LISTEN | grep -w $portset`
				if [ "$listenportlent" == "" ];then #判断端口是否被占用
					#备份配置文件
					backup_sshd_config
					sed -i "s@Port @#Port @g" /etc/ssh/sshd_config && echo "Port $portset" >> /etc/ssh/sshd_config  && echo "--> 修改sshd运行端口为$runport成功" || { echo "--> 修改sshd运行端口为$runport失败"; ExitCode=1; }
					#重启sshd服务
					sshd_service_restart
				else
					echo "端口已经被占用,请重新输入"
					exit 1
				fi		
			else #当打开了一个或多个非22,且与设置的端口不同时
				listenportlent=`netstat -ano | grep -w LISTEN | grep -w $portset`
				if [ "$listenportlent" == "" ];then #判断端口是否被占用
					#备份sshd配置文件
					backup_sshd_config
					for sshport in $getSSHOpenPortList
					do
						/bin/sed -i "s/$sshport/$portset/g" /etc/ssh/sshd_config
					done
					#重启sshd服务
					sshd_service_restart
				else
					echo "端口已经被占用,请重新输入"
					exit 1
				fi
			fi
		else
			echo "--> sshd服务未启动"
			exit 1
			#尝试重启sshd服务
			sshd_service_restart
		fi
	else
		echo "--> 请输入1-65535之间的一个整数"
		exit 1
	fi
else
	echo "--> 请输入端口号"
	exit 1
fi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值