shell脚本--小工具:只允许特定IP-SSH当前服务器

开箱即食:
通过iptables,限制该机器只允许当前ssh机器访问,
-------该工具可选择加上限制或解除限制

#!/bin/bsah

get_sshPort(){
s_p=`cat /etc/ssh/sshd_config |grep "Port " | tail -1`
p=${s_p/"Port "/""}
s=${p/"#"/""}
echo $s
}

restricted(){
sshPort=`get_sshPort`
sshIP=`netstat -atn |grep -i :$sshPort  | awk '{print $5}' | awk -F: '{print $1}' | sort -nr | uniq -c | awk '{print $2}'`
#echo $sshIP
for i in $sshIP;do echo $i ;done
directorylist="$(for i in $sshIP;do echo $i; done)"

PS3='IP address of the current login? ' # 设置有帮助的选择提示
until [ "$directory" == "Finished" ]; do

    printf "%b" "\a\n\nSelect the IP address of the jumper:\n" >&2
    select directory in $directorylist; do
        if [ -n "$directory" ]; then
            echo "You chose number $REPLY, processing $directory..."
			ipt_strategy $directory $sshPort
            exit 
        else
            echo "your choice is error!"
        fi 

    done 
done
}


ipt_strategy(){
iptables-save > ~/iptables.bak
iptables -A INPUT -s $1 -p tcp --dport $2 -j ACCEPT
iptables -A OUTPUT -d $1 -p tcp --sport $2 -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
iptables-save
}
ipt_unfreeze(){
iptables-restore < ~/iptables.bak
}

read -p "Please input your choice 限制/解除限制 S/R:" -t 5 choice
case $choice in
	"S")
	echo "your choice is S!添加iptables限制"
	restricted
	;;
	"R")
	echo "your choice is R!解除iptables限制"
	ipt_unfreeze
	;;
	*)
	echo "your choice is error!"
	;;
esac
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值