一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Linux系统退出时限制ip登录shell脚本

时间:2016-04-26 编辑:简简单单 来源:一聚教程网


生产环境中,有可能会出现被其他管理员或使用者修改hosts.allow允许更多的ip登陆机器;为了方便统一管理,我们可以在用户退出的时候统一修改allow和deny文件成默认设置。同时也能自动获取机器所配ip的整个ip段为允许登陆


test -f /root/.bash_logout && chattr -i /root/.bash_logout
cat >/root/.bash_logout < export TERM=xterm
/usr/bin/chattr -i /etc/hosts.allow /etc/hosts.deny
 
#for hosts.deny
/bin/awk 'BEGIN{FS="[=.]";RS="\n"};/^IPADDR/{print "sshd:all EXCEPT "\$2"."\$3"."\$4".0/255.255.255.0"}' /etc/sysconfig/network-scripts/ifcfg-* | grep -v 127.0.0 | uniq 1>/etc/hosts.deny
 
/bin/cat >>/etc/hosts.deny << iEOF
sshd:all EXCEPT ip
iEOF
 
#for hosts.allow
/bin/awk 'BEGIN{FS="[=.]";RS="\n"};/^IPADDR/{print "sshd:"\$2"."\$3"."\$4".0/255.255.255.0"}' /etc/sysconfig/network-scripts/ifcfg-* | grep -v 127.0.0 | uniq 1>/etc/hosts.allow
 
/bin/cat >>/etc/hosts.allow << iEOF
sshd:ip
iEOF
/usr/bin/chattr +i /etc/hosts.allow /etc/hosts.deny
 
clear
EOF
 
sh /root/.bash_logout 2>&1 >/dev/null

例子

这篇文章只是检测有非法ip登录到服务器上就自动给运维报警,当然也可以改成短信报警,前提是你有短信网关.

#!/bin/bash
#该脚本作用是检测是否有恶意IP登陆服务器并邮件报警
#可以结合139邮箱以达到短信及时通知到手机的功能
#适用系统centos5
 
Ldate=`which date`
Lawk=`which awk`
Llast=`which last`
Lgrep=`which grep`
Lsendmail=`which sendmail`
Lifconfig=`which ifconfig`
serverip=`$Lifconfig eth0|$Lgrep inet|$Lawk -F : '{print $2}'|$Lawk '{print $1}'`
cutdate=`$Ldate |$Lawk '{print $1" "$2" "$3}'`
 
hackerip=`$Llast|$Lgrep "$cutdate"|$Lawk '{print $3}'|$Lgrep -v 192.168.1x.xx`
 
if [ -z $hackerip ]
then
exit
else
 
for logip in $hackerip
do
echo "hacker ip is  $logip already login  $serverip"|mail -s "SOS" rocdk890@139.com
 
done
fi

热门栏目