公司有安装panabit行为管理器. 然后做了一些规则限制. 放开了几个域名.比如QQ的企业邮箱.
然后像QQ企业邮箱之类的有时经常换IP.这样如果不知道的话,同事可能就没办法收邮件了...
看了下panabit是基于freebsd的. 公司目前有两套,一个8.0的,一个9.0的.
话说bsd里面的 sed 真的是超难用. 还好有 awk. 而且是csh, 没有bash. 考虑到panabit这个是定制的环境.可能是不是你想安装就能安装的.就算安装了,说不定不稳定...
panabit里面没有nslookup和dig. 所有域名只能ssh到别的server去查询.
下面是具体代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#!/bin/csh
# create by bianlimit
set
sshhost =
"192.168.1.14"
if
( `
uname
-r` ==
"9.2-RELEASE"
)
then
#分部用的是新的版本, 这里面的 tid 和 dns 那个是去pa_conf这个文件查看的.
set
ipectrl =
"/usr/system/bin/ipectrl"
set
pa_conf =
"/conf/panabit.conf"
set
pa_tid =
"1"
set
pa_dns =
"1"
else
#老版本
set
ipectrl =
"/usr/panabit/bin/ipectrl"
set
pa_conf =
"/usr/panaetc/panabit.conf"
set
pa_tid =
"4"
set
pa_dns =
"2"
endif
set
pa_dir = `
dirname
$pa_conf`
set
ip_tmp=
"192.168.1.11 192.168.1.12"
set
dns_tmp=`
awk
-F
'='
-
v
var=$pa_dns
'/^dns/&&$2==var" dns"{print $3}'
$pa_conf`
foreach i ( $dns_tmp )
set
www = `
ssh
$sshhost
"nslookup $i | awk 'NR>4&&/^Add/'"
|
awk
'{print $2" "}'
`
if
( $i ==
"exmail.qq.com"
)
then
#公司用的QQ企业邮箱还得添加imap, smtp
set
imap = `
ssh
$sshhost
"nslookup imap.$i | awk 'NR>4&&/^Add/'"
|
awk
'{print $2" "}'
`
set
smtp = `
ssh
$sshhost
"nslookup smtp.$i | awk 'NR>4&&/^Add/'"
|
awk
'{print $2" "}'
`
set
ip_tmp =
"$ip_tmp $imap $smtp"
endif
set
ip_tmp =
"$ip_tmp $www"
end
set
newip = `
echo
"$ip_tmp"
|
awk
'{for(i=1;i<=NF;i++){a[$i]}}END{if(length(a)==0){print "EXIT"}else{for(i in a){print i}}}'
`
if
(
"$newip"
==
"EXIT"
)
then
exit
endif
#生成新的格式
awk
-F
'='
-
v
vtid=$pa_tid -
v
nip=
"$newip"
'BEGIN{split(nip,t," ")}$1~/iprng tid/&&$2==vtid" ip"{x=1;next}{if(x){c=c?c"\n"$0:$0}else{a=a?a"\n"$0:$0}}END{for(i in t){b=b?b"\niprng tid="vtid" ip="t[i]:"iprng tid="vtid" ip="t[i]};print a"\n"b"\n"c}'
$pa_conf >
"${pa_conf}.tmp"
cp
$pa_conf
"${pa_conf}.`date +%F`"
&&
mv
"${pa_conf}.tmp"
$pa_conf
find
$pa_dir -name
"panabit.conf*"
-a -mtime +15 -
exec
rm
-f {} \;
$ipectrl stop
sleep
5
$ipectrl start
|
以上代码已测试通过. 运行时请删除相关中文注释
ssh 请先做相关认证.
添加计划任务, 把下面的代码添加到 crontab 中, auto_dns.sh 为脚本名
1
2
3
|
shell=
/bin/sh
path=
/etc
:
/bin
:
/sbin
:
/usr/bin
:
/etc/rc
.d:
1 8 * * *
/root/auto_dns
.sh
|
添加完成后,重启一下
1
|
/etc/rc
.d
/cron
onerestart
|
本文转自 nonono11 51CTO博客,原文链接:http://blog.51cto.com/abian/1680187,如需转载请自行联系原作者