#! /bin/bash
N=2
[email protected]
while :; do
ipfile=/usr/local/sbin/serverip.txt
for ip in `cat $ipfile`; do
mailfile=/tmp/pkgloss.txt
pkgloss=`ping -c4 $ip |grep 'transm' |awk -F',' '{print $2}' |awk '{print $1}'`
if [ $pkgloss -lt $N ]; then
echo $ip >$mailfile
ping -c4 $ip |grep 'transm' >>$mailfile
mail -s "$ip loss packets" $rmail<$mailfile
fi
done
sleep 100
done
none
#! /bin/bash
w=`date +%w`
rs=/usr/bin/rsync
rsou=ypl-web:/data/wwwroot
rde=/backup/webatt
d1=`date +%y%m%d`
d2=`date -d "-1 day" +%y%m%d`
m1=`date -d "-1 day" +%y%m`
m2=`date +%y%m`
sou=$rsou/ypl/attachments
des=$rde/ypl-att
exec 1>/var/log/att-bak.log 2>&1
echo attbak start `date`
if [ "$w" = "0" ]; then
$rs -aur --delete $rsou/ypl/ $des/attachments_$w
else
$rs -aur --delete --exclude=attachments $rsou/ypl/ $des/attachments_$w
[ -d $des/attachments_$w/attachments/month_$m2 ] || mkdir -p $des/attachments_$w/attachments/month_$m2
$rs -aur $sou/month_$m1/$d2* $des/attachments_$w/attachments/month_$m1/
$rs -aur $sou/month_$m2/$d1* $des/attachments_$w/attachments/month_$m2/
$rs -aur $sou/swfupload/$d2* $des/attachments_$w/attachments/swfupload/
$rs -aur $sou/swfupload/$d1* $des/attachments_$w/attachments/swfupload/
/usr/bin/find $des/attachments_$w/attachments/ -type f -mmin +1600 |xargs rm
fi
echo attbak end `date`
none
while :;
do
nmap -sS -vv 121.10.118.111 |grep 80 |tail -n1>/tmp/nmap.txt
rr80=`cat /tmp/nmap.txt|awk '{print $2}'`
[ "$rr80" = "open" ] && mail -s rr_port_80 [email][email protected][/email] </tmp/nmap.txt
sleep 10
done当然可以尝试修改脚本使其监控80端口是否正常。
none
#!/bin/sh
grep HISTTIMEFORMAT /etc/bashrc || echo 'export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "' >>/etc/bashrc
for U in `grep -v shutdown /etc/passwd|awk -F: '$NF~/sh/&&$NF!~/no/{print $1}'`
do
UHOME=`cat /etc/passwd|grep "^$U"|cut -d: -f6`
[ ! -f $UHOME/.bash_history ] && touch $UHOME/.bash_history
chattr +a $UHOME/.bash_history
done
none
#! /bin/bash
STORE_NAME=`date +%m%d`
STORE_DIR='/home/backup/mysql/'
exec 1>/var/log/mysqlbak.log 2>&1
echo mysqlbak begin at `date`
[ ! -d $STORE_DIR ] && mkdir -p $STORE_DIR
/usr/local/mysql/bin/mysqldump -uroot -p'password' databasename > $STORE_DIR/mysql-$STORE_NAME.sql
gzip $STORE_DIR/mysql-$STORE_NAME.sql
find $STORE_DIR -mtime +7 -name '*.sql.gz' |xargs rm -f
echo mysqlbak finish at `date`
none
#! /bin/bash
dir=/home/mysql
for file in `ls $dir`; do
for file2 in `ls $dir/$file` ; do
own=`ls -ld $dir/$file/$file2 |awk '{print $3}'`
if [ "$own" = "root" ] ; then
chown -R mysql:mysql $dir/$file/$file2
echo "$dir/$file/$file2" >>/tmp/checkown.log
fi
done
done
none
#! /bin/bash
exec >>/tmp/addcnip.log 2>> /tmp/addcnip.log
echo " "
echo begin at `date`
d=`date +%y%m%d --date "-1 hour"`
d1=`date +%d --date "-1 hour"`
d2=`date +y%m%d%H --date "-1 hour"`
d3=`date +%H --date "-1 hour"`
svnpath=http://lishiming.5d6d.com/svn/cnip
logfile=/home/logs/access-$d2\_log
count1=`wc -l /usr/local/sbin/newcnip.txt|awk '{print $1}'`
accipf=/usr/local/sbin/nocnip.txt
newcnipf=/usr/local/sbin/newcnip.txt
dip=192.168.1.102
ipt=/sbin/iptables
awk '{print $1}' $logfile |sort -n|uniq |grep -v '^$'|grep -v '_'>/tmp/initip.txt
rm -f /tmp/cnip.txt
rm -f /tmp/initip2.txt
rm -f /tmp/nocnip.txt
for ip in `cat /tmp/initip.txt`; do
n=`grep $ip $accipf|wc -l`
if [ "$n" = "0" ] ; then
echo $ip >>/tmp/initip2.txt
fi
done
killall whois
for ip in `cat /tmp/initip2.txt|grep -v '^$'`; do
[ -d /tmp/whois ] || mkdir /tmp/whois
whois $ip >/tmp/whois/$ip
coun=`cat /tmp/whois/$ip |grep 'ountry'|awk '{print $2}'|head -n1`
if [ "$coun" = "CN" ] || [ "$coun" = "cn" ]; then
ippool=`cat /tmp/whois/$ip |grep 'netnum' |awk '{print $2"-"$4}'`
echo $ippool >>/tmp/cnip.txt
else
echo $ip >>/tmp/nocnip.txt
fi
rm -f /tmp/whois/$ip
done
cat $accipf >>/tmp/nocnip.txt
grep -v '^$' /tmp/nocnip.txt|sort -n |uniq >$accipf
cat $newcnipf |grep -v '^$'>>/tmp/cnip.txt
count2=`grep -v '^$' /tmp/cnip.txt|sort -n |uniq |wc -l |awk '{print $1}'`
if [ "$count1" -lt "$count2" ]; then
sort -n /tmp/cnip.txt |uniq |grep -v '^$'>$newcnipf
/bin/cp -f $newcnipf /tmp/cnip/$d1-cnip.txt
$ipt -F
for ip in `cat $newcnipf|grep -v '^$'`; do
$ipt -I INPUT -m iprange --src-range $ip -d $dip -j DROP
done
for ip2 in `cat /usr/local/sbin/goodip.txt`; do
$ipt -I INPUT -s $ip2 -d $dip -j ACCEPT
done
if [ "$d3" != "00" ] ; then
/usr/bin/svn remove $svnpath\/$d.txt -m 'if exist'
fi
/usr/bin/svn import /tmp/cnip/$d1-cnip.txt $svnpath\/$d.txt -m 'add cnip'
fi
echo end `date`
none
#! /bin/bash
exec 1>/var/log/mysqlbak.log 2>&1
data_source=/home/mysql/
backup_dir=/mnt/mysqlbak
remote_dir=1.2.3.4:/data/mysqlbak
n=`date +%d`
w=`date +%w`
echo backup of mysql begins at `date`
/etc/init.d/mysqld stop
p=/var/run/mysqld/mysqld.pid
while ps -p $p >/dev/null;
do
sleep 1 && echo -n .
done
sync;sync;sync;sleep 2;
rsync -aru --delete $data_source/* $backup_dir/mysql_$w/
/etc/init.d/mysqld start
echo backup of mysql finished at `date`
rsync -aru --delete $backup_dir/mysql_$w/* $remote_dir/mysql_$n/
echo remote backup of mysql finished at `date`
none
df -h|awk '$5~/[8 9][0-9]%|100%/ ' >/tmp/checkdisk.log
none
sed 's/\(^a.*\)/\1 12/' test
#cat test
askdj
aslkd aslkdjf3e
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj
#sed 's/\(^a.*\)/\1 12/' test
askdj 12
aslkd aslkdjf3e 12
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj 12
none
#! /bin/bash
chd='www.5dforum.com/bbs.php'
mailer='[email protected]'
maxrp=3
while :; do
n1=0
c=`ping -c 10 www.5dforum.com |grep received |awk '{print $4}' `
if [ $c == "0" ] ; then
for i in `seq 1 $maxrp`; do
c=`ping -c 10 www.5dforum.com |grep received |awk '{print $4}' `
if [ $c == "0" ] ; then
n1=$i
fi
sleep 10
done
if [ $n1 == $maxrp ]; then
echo "The 5dforum\'s network is down!"|mail -s 5dforum-down $mailer
exit
fi
else
if $(curl $chd|grep 'Discuz!'>/dev/null) ; then
sleep 30
else
n2=0
for i in `seq 1 $maxrp`; do
if $(curl $chd|grep 'Discuz!'>/dev/null) ; then
sleep 30
else
n2=$i
fi
done
if [ $n2 == $maxrp ] ; then
echo "The 5dforum is something wrong, please cheack!"|mail -s 5dforum-down $mailer
exit
fi
fi
fi
sleep 30
done
none
grep -E '123|abc' filename // 找出文件(filename)中包含123或者包含abc的行
egrep '123|abc' filename //用egrep同样可以实现
awk '/123|abc/' filename // awk 的实现方式
其他awk相关文章
none
前提是,文件中只有一列数字。计算脚本为:
let sum=0 ; for i in `cat file `; do let j=$i; let sum=$j+$sum; done; echo $sum
none
sort -t : -k 5b,5 -k 3,3n /etc/passwd
sort -t : -n -k 5b,5 -k 3,3 /etc/passwd
sort -t : -b -k 5,5 -k 3,3n /etc/passwd
通过上边三个例子,我只想说明sort中的 -t 和 -k 选项的意义。
其中-t选项用来定义分割符,在上边三个例子中,使用:作为分隔符
-k 后边跟域号,-k 5 代表按第五列进行排序, -k 5,5 代表从第5列开始到第5列结束
其中-b 选项代表忽略头行出现的空行, -n选项代表按数值大小排序
另外还有没有用到的一些选项
-c 测试文件是否已经分类。
-m 合并两个分类文件。
-u 删除所有复制行。
-o 存储sort 结果的输出文件名。
-r 对分类次序或比较求逆。
none
今天突然发现grep -q 用于if 逻辑判断很好用。
-q 参数,本意是 Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. 中文意思为,安静模式,不打印任何标准输出。如果有匹配的内容则立即返回状态值0。
小应用
# cat a.txt
nihao
nihaooo
hello
# if grep -q hello a.txt ; then echo yes;else echo no; fi
yes
# if grep -q word a.txt; then echo yes; else echo no; fi
no
none
- «
- 1
- ...
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- ...
- 31
- »