由于業務需要,需要限制一下伺服器的出網下載下傳,隻能在00-17點這個時間段才能出網下載下傳資源,其它
時間不運作出網下載下傳。是以決定用iptables做一下限制,這樣是最簡單時效的,廢話不多說,看腳本。
#!/bin/bash
re_log(){
Time=$(date "+%Y%m%d %T")
echo -e "[$Time] $1" >>/tmp/change_iptables.log
}
change_iptables(){
from=$1
to=$2
echo "copy $from to $to.."
cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak
cp -rf $from $to
if [ $? -ge 1 ];then
re_log "copy $from to $to.. failed.."
else
re_log "copy $from to $to.. success.."
fi
# service iptables status || service iptables start
service iptables restart
}
limit(){
change_iptables /root/workspace/iptables_drop /etc/sysconfig/iptables
}
open(){
change_iptables /root/workspace/iptables /etc/sysconfig/iptables
}
case $1 in
--limit|limit)
limit
;;
--open|open)
open
;;
*)
echo "Usage: $0 limit|open"
echo "Ex: $0 open"
exit
;;
esac
add_crond(){
sed -i '/\/root\/workspace\/iptables.sh/d' /etc/crontab
echo -e "*/5 18-23 * * * root /root/workspace/iptables.sh limit &>/dev/null" >>/etc/crontab
echo -e "*/5 00-17 * * * root /root/workspace/iptables.sh open &>/dev/null" >>/etc/crontab
}
add_crond