免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2532 | 回复: 2
打印 上一主题 下一主题

使用shell script 实现mail服务器IP屏蔽的自动监控和防范 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-31 13:53 |只看该作者 |倒序浏览
公司邮件提供商为了防治垃圾邮件,采取了较为严格的防范策略包括同一IP在一定的时间内,发送email多少封就会被认为是垃圾邮件行为,而将该IP屏蔽2个小时,表现在连接端口时出现 “451 Audit system block IP”错误,影响正常的邮件发送。

如何避免这样的问题,一直是很头痛的一个问题。除非修改IP地址,但是IP地址有限,于是想到了使用ADSL备份线路,每次拨号都会从DHCP分配一个新的IP地址。

被屏蔽的时候手工调整一下路由,走adsl,不错,能不能自动去检测,然后根据服务器状态自动进行路由切换和重新拨号呢?
这里面有个关键点,要能够和ADSL路由器进行自动会话,实现PPPOE的拨号(SpeedTocuh515)。
第二点,要在故障检测到2个小时以后,能够从DSL切换回光纤线路。

因为一般的shell像使用cat <<EOL 的方式是无法处理连接到主机上以后的操作,要是能有像windows下面的自动回拨系统的脚本支持就好了。
unix不愧是unix,expect is what I‘m expecting。
在加上autoexpect简直帅呆了,
对于第二个问题,linux下的date提供了很好的参数 +%s
后面的就简单了。
先检查一下服务器状态,使用getstatus(expect脚本),得到服务器状态(220,451或者无法连接)如果正常退出,否则,看下是不是第一次出现被屏蔽,如果是,把当前时间时间写入临时文件,如果不是第一次检测到故障就对比一下首故障时间,如果大于2小时,就切换到光纤链路,如果不到两小时,而且还是DSL线路,就进行一次新的拨号,得到一个新IP地址。每10分钟检测一次状态。
呵呵,看来还不错。
结果:
Mon Jul 31 12:59:47 CST 2006 :[checkstat( )]: Running checkstat( )
Mon Jul 31 12:59:50 CST 2006 :[checkstat( )]: Returen $stat is FAILED
Mon Jul 31 12:59:50 CST 2006: [main( ) ]: File Errtime existing
Mon Jul 31 12:59:50 CST 2006 :[calctime( ) ]: Running calctime( )
Mon Jul 31 12:59:50 CST 2006 :[calctime( ) ]: Retuen $secs 2172
Mon Jul 31 12:59:50 CST 2006 :[checkrt( ) ]: Running checkrt( )
Mon Jul 31 12:59:50 CST 2006 :[checkrt( ) ]: Return $rt is dsl
Mon Jul 31 12:59:50 CST 2006 :[main( ) ]: Redial DSL
Mon Jul 31 13:00:21 CST 2006 :[getdslip( ) ]: Running getdslip( )
Mon Jul 31 13:00:21 CST 2006 :[getdslip( ) ]: Return $dslip :219.145.57.1
------------------------------------------------------------------------
Mon Jul 31 13:00:21 CST 2006 :[checkstat( )]: Running checkstat( )
Mon Jul 31 13:00:24 CST 2006 :[checkstat( )]: Returen $stat is FAILED
Mon Jul 31 13:00:24 CST 2006: [main( ) ]: File Errtime existing
Mon Jul 31 13:00:24 CST 2006 :[calctime( ) ]: Running calctime( )
Mon Jul 31 13:00:24 CST 2006 :[calctime( ) ]: Retuen $secs 2206
Mon Jul 31 13:00:24 CST 2006 :[checkrt( ) ]: Running checkrt( )
Mon Jul 31 13:00:24 CST 2006 :[checkrt( ) ]: Return $rt is dsl
Mon Jul 31 13:00:24 CST 2006 :[main( ) ]: Redial DSL
Mon Jul 31 13:00:55 CST 2006 :[getdslip( ) ]: Running getdslip( )
Mon Jul 31 13:00:55 CST 2006 :[getdslip( ) ]: Return $dslip :222.91.80.1
------------------------------------------------------------------------
Mon Jul 31 13:00:55 CST 2006 :[checkstat( )]: Running checkstat( )
Mon Jul 31 13:01:00 CST 2006 :[checkstat( )]: Returen $stat is OK
Mon Jul 31 13:01:00 CST 2006: [main( ) ]: File Errtime Removed
Mon Jul 31 13:01:00 CST 2006 :[checkrt( ) ]: Running checkrt( )
Mon Jul 31 13:01:00 CST 2006 :[checkrt( ) ]: Return $rt is dsl
Mon Jul 31 13:01:00 CST 2006 :[getdslip( ) ]: Running getdslip( )
Mon Jul 31 13:01:00 CST 2006 :[getdslip( ) ]: Return $dslip :222.91.80.1
------------------------------------------------------------------------


心得,execpt好东西,http://expect.nist.gov,对实现交互式的自动化脚本非常有用。我想除了tcl可能只有expect了。


主要脚本:
::::::::::::::
chip4mail
::::::::::::::
#!/bin/sh

home=/usr/local/chip4mail
log=$home/logs/`date +%F`.log

#################################
#Check current route of mail svr
#Return $rt=[fib|dsl]
#################################
function checkrt( )
{
rt=fib
echo "`date` :[checkrt( ) ]: Running checkrt( )" | tee -a $log
netstat -rn | grep 2xx.xx.xx.12 >/dev/null
a=`echo $?`

if [ $a -eq "0" ]; then
rt=dsl
else
rt=fib
fi
echo "`date` :[checkrt( ) ]: Return \$rt is $rt" | tee -a $log
}

##########################
#Check mail server status
#Return $stat[err|ok]
##########################
function checkstat( )
{
num="0"
stat="err"
echo "`date` :[checkstat( )]: Running checkstat( )" | tee -a $log
$home/getstatus >/tmp/mailsvrstatus
num=`tail -n1 /tmp/mailsvrstatus | cut -c1-3`
echo $num
if [ $num != "220" ]; then
stat=err
else
stat=ok
fi
echo "`date` :[checkstat( )]: Returen \$stat is $stat" | tee -a $log
}


#################################
# Caculate seconds from last err
# Return $secs
#################################
function calctime( )
{
secs=0
echo "`date` :[calctime( ) ]: Running calctime( )" | tee -a $log
begin=`cat /tmp/errtime`
end=`date +%s`
((secs=$end-$begin))
export secs
echo "`date` :[calctime( ) ]: Retuen \$secs $secs" | tee -a $log
}

###########################
#Get DSL ip
###########################
function getdslip( )
{
echo "`date` :[getdslip( ) ]: Running getdslip( )" | tee -a $log
dslip=`$home/getdslstat | grep "Remote IP" | awk '{ print $3 }'`
echo "`date` :[getdslip( ) ]: Return \$dslip dslip" | tee -a $log
}

##########################
# Main
#  Last modi:2006/7/28
##########################

stat="err"
while [ $stat = "err" ]
do
echo '------------------------------------------------------------------------'| tee -a $log
checkstat
if [ $stat = "err" ]; then
if [ -e /tmp/errtime ]; then
echo "`date`: [main( ) ]: File Errtime existing" | tee -a $log
calctime
else
echo `date +%s` >/tmp/errtime
secs=0
echo "`date` :[main( ) ]: File Errtime Created" | tee -a $log
fi
else
if [ -e /tmp/errtime ]; then
rm /tmp/errtime
echo "`date`: [main( ) ]: File Errtime Removed" | tee -a $log
fi
checkrt
if [ $rt = "dsl" ]; then
getdslip
fi
sleep 600
exec $home/chip4mail
fi

checkrt
if [ $rt = "dsl" ]; then
if [ $secs -gt "7200" ]; then
rm /tmp/errtime
$home/tofib
echo " `date` :[main( ) ]: Changed to fiber" | tee -a $log
else
echo "`date` :[main( ) ]: Redial DSL" | tee -a $log
$home/redial >/dev/null
sleep 30
getdslip
fi
else
$home/todsl
echo "`date` :[main( ) ]: Changed to DSL" | tee -a $log
getdslip
fi
done



::::::::::::::
redial
::::::::::::::
#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Fri Jul 28 14:32:51 2006
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script. It
# necessarily has to guess about certain things. Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts. If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character. This
# pacifies every program I know of. The -c flag makes the script do
# this in the first place. The -C flag allows you to define a
# character to toggle this mode off and on.

set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}

#
# 2) differing output - Some programs produce different output each time
# they run. The "date" command is an obvious example. Another is
# ftp, if it produces throughput statistics at the end of a file
# transfer. If this causes a problem, delete these patterns or replace
# them with wildcards. An alternative is to use the -p flag (for
# "prompt" which makes Expect only look for the last line of output
# (i.e., the prompt). The -P flag allows you to define a character to
# toggle this mode off and on.
#
# Read the man page for more info.
#
# -Don


set timeout -1
spawn $env(SHELL)
match_max 100000
expect -exact "\]0;root/usr/local/chip4mail \[root@host chip4mail\]# "
send -- "telnet 192.168.x.x\r"
expect -exact "telnet 192.168.xx.xx\r
Trying 192.168.x.x...\r\r
Connected to 192.168.xx.xx.\r\r
Escape character is '^\]'.\r\r
\r
\r
Login: "
send -- "username\r"
expect -exact "username\r\r
Password: "
send -- "x"
expect -exact "*"
send -- "x"
expect -exact "*"
send -- "x"
expect -exact "*"
send -- "x"
expect -exact "*"
send -- "x"
expect -exact "*"
send -- "\r"
expect -exact "\r\r
\r\r
Login successful\r\r
\r\r
--> "
send -- "pppoe set transport 1 disable\r"
expect -exact "pppoe set transport 1 disable\r\r
\r\r
--> "
send -- "pppoe set transport 1 enable\r"
expect -exact "pppoe set transport 1 enable\r\r
\r\r
--> "
send -- "user logout\r"
expect -exact "user logout\r\r
\r
Logging out.\r
\r
Connection closed by foreign host.\r\r
\]0;root:/usr/local/chip4mail \[root@ chip4mail\]# "
send -- "exit\r"
expect eof

[ 本帖最后由 xaliyan 于 2006-7-31 14:00 编辑 ]

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
2 [报告]
发表于 2006-07-31 15:36 |只看该作者
题外话:客户的地位咋这么低啊,直接让提供商把你公司的ip地址放开限制就可以了。

论坛徽章:
0
3 [报告]
发表于 2006-08-01 18:11 |只看该作者
mail服务商的系统太弱智了(也许是人),唉,郁闷ing。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP