工作中编写的一些小脚本
Posted Louis He
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了工作中编写的一些小脚本相关的知识,希望对你有一定的参考价值。
#批量处理域名访问时间 while read line do ping -c 3 $line|tail -1 |awk -F "/" ‘{print $5}‘>> result/"$line"_result.txt& done < wenzhou_domain.txt sleep 1m while read domain do a=`cat result/"$domain"_result.txt` if [ "$a" = "" ];then echo unknow >>result.txt else echo $a >>result.txt fi done <wenzhou_domain.txt #批量处理域名状态码 #!/bin/bash Thread=100 CurFileName="domain.txt" FifoFile="$.fifo" mkfifo $FifoFile exec 6<>$FifoFile rm $FifoFile for ((i=0;i<=$Thread;i++));do echo;done >&6 exec 5<$CurFileName trap ‘kill -9 0;exit 1‘ 1 2 3 15 while read -u5 line do read -u6 { STATUS_CODE=$(curl -I -o /dev/null -s -w %{http_code} "$line") echo -e "$STATUS_CODE" |tee -a code.txt echo >&6 } & done wait #批量dig取ip #!/bin/bash Thread=100 CurFileName="domain.txt" FifoFile="$.fifo" mkfifo $FifoFile exec 6<>$FifoFile rm $FifoFile for ((i=0;i<=$Thread;i++));do echo;done >&6 exec 5<$CurFileName trap ‘kill -9 0;exit 1‘ 1 2 3 15 while read -u5 line do read -u6 { STATUS_CODE=$(dig "$line" A +noall +answer |tail -1|awk ‘{print $5}‘ ) echo -e "$line:\t$STATUS_CODE" |tee -a code.txt echo >&6 } & done wait #批量查询端口 1) #!/bin/bash Thread=100 CurFileName="domain.txt" FifoFile="$.fifo" mkfifo $FifoFile exec 6<>$FifoFile rm $FifoFile for ((i=0;i<=$Thread;i++));do echo;done >&6 exec 5<$CurFileName trap ‘kill -9 0;exit 1‘ 1 2 3 15 while read -u5 line do read -u6 { STATUS_CODE=$(nmap -p80,443 "$line" | grep open ) echo -e "$line\n$STATUS_CODE"|cut -d "/" -f1 |tee -a code.txt echo >&6 } & done wait 2) while read domain do nmap -sT $domain | grep open >> result/"$domain"_result.txt & done < domain.txt sleep 5m while read Domain do echo =========$Domain============ >>result.txt cat result/"$Domain"_result.txt >>result.txt done < domain.txt #监控web服务状态 #!/bin/sh CheckUrl(){ timeout=5 fails=0 success=0 while true do wget --timeout=$timeout --tries=1 http://blog.chinaunix.net/uid-23929712-id-2650421.html -q -O /dev/null if [ $? -ne 0 ] then let fails=fails+1 else let success=success+1 fi if [ $success -ge 1 ] then echo success Critical="sys is up." echo $Critical|tee|mail -s "$Critical" [email protected] exit 0 fi if [ $fails -ge 2 ] then Critical="sys is down." echo $Critical|tee|mail -s "$Critical" [email protected] exit 2 fi done } CheckUrl #判断系统内存大小 #/bin/sh FreeMem=`free -m|awk ‘NR==3 {print $NF}‘` CHARS="Current memory is $FreeMem" if [ $FreeMem -lt 100 ] then echo $CHARS|tee /tmp/messages.txt mail -s "`date +%F-%T`$CHARS" [email protected] </tmp/messages.txt fi #DDos攻击 #!/bin/sh file=$1 while true do awk ‘{print $1}‘ $1|grep -v "^$"|sort|uniq -c >/tmp/tmp.log exec </tmp/tmp.log while read line do ip=`echo $line|awk ‘{print $2}‘` count=`echo $line|awk ‘{print $1}‘` if [ $count -gt 500 ] && [ `iptables -L -n|grep "$ip"|wc -l` -lt 1 ] then iptables -I INPUT -s $ip -j DROP echo "$line is dropped" >>/tmp/droplist_$(date +%F).log fi done sleep 1m done #去重输出到原文件 #! /bin/bash while read gsa do { while read yijiazai do if [ $gsa = $yijiazai ]; then echo $gsa >>chongfu_domain.txt sed -i "/$gsa/d" vaas.txt #sed -i "/$gsa/d" yijiazai_domain.txt fi done < bukejia_domain.txt } done < vaas.txt #去重输出到新文件 #! /bin/bash while read https_d do { while read diyipi_d do if [ $https_d = $diyipi_d ]; then echo $https_d >>quchong_and_newfile.txt sed -i "/$https_d/d" diyipi_domain.txt fi done < diyipi_domain.txt } done < https.txt #过滤相同 #! /bin/bash while read zuihouyipi do { while read wangsu666_d do if [ $wangsu666_d = $zuihouyipi ]; then echo $wangsu666_d >> qieheidomain.txt fi done < wangsu666domain.txt } done < 161domain.txt #批量取出对应域名url #!/bin/sh while read line do cat access.log*|awk ‘{print $5,$8}‘|grep 200|awk ‘{print $2}‘|grep $line|sort -R|head -2 >>rulse.txt done < domain.txt 判断mysql服务是否开启的方法: #!/bin/sh echo method1------------------- if [ `netstat -lnt|grep 3306|awk -F "[ :]+" ‘{print $5}‘` -eq 3306 ] then echo "MySQL is Running." else echo "MySQL is Stopped." /etc/init.d/mysqld start fi echo method2------------------- if [ "`netstat -lnt|grep 3306|awk -F "[ :]+" ‘{print $5}‘`" = "3306" ] then echo "MySQL is Running." else echo "MySQL is Stopped." /etc/init.d/mysqld start fi echo method3------------------- if [ `netstat -lntup|grep mysqld|wc -l` -gt 0 ] then echo "MySQL is Running." else echo "MySQL is Stopped." /etc/init.d/mysqld start fi echo method4------------------- if [ `lsof -i tcp:3306|wc -l` -gt 0 ] then echo "MySQL is Running." else echo "MySQL is Stopped." /etc/init.d/mysqld start fi echo method5------------------- [ `rpm -qa nmap|wc -l` -lt 1 ] && yum install nmap -y &>/dev/null if [ `nmap 127.0.0.1 -p 3306 2>/dev/null|grep open|wc -l` -gt 0 ] then echo "MySQL is Running." else echo "MySQL is Stopped." /etc/init.d/mysqld start fi echo method6------------------- [ `rpm -qa nc|wc -l` -lt 1 ] && yum install nc -y &>/dev/null if [ `nc -w 2 127.0.0.1 3306 &>/dev/null&&echo ok|grep ok|wc -l` -gt 0 ] then echo "MySQL is Running." else echo "MySQL is Stopped." /etc/init.d/mysqld start fi echo method7------------------- if [ `ps -ef|grep -v grep|grep mysql|wc -l` -ge 1 ] then echo "MySQL is Running." else echo "MySQL is Stopped." /etc/init.d/mysqld start fi 判断http服务是否开启的方法: #!/bin/sh echo http method1------------------- if [ `netstat -lnt|grep 80|awk -F "[ :]+" ‘{print $5}‘` -eq 80 ] then echo "nginx is Running." else echo "Nginx is Stopped." /etc/init.d/nginx start fi echo http method2------------------- if [ "`netstat -lnt|grep 80|awk -F "[ :]+" ‘{print $5}‘`" = "80" ] then echo "Nginx is Running." else echo "Nginx is Stopped." /etc/init.d/nginx start fi echo http method3------------------- if [ `netstat -lntup|grep nginx|wc -l` -gt 0 ] then echo "Nginx is Running." else echo "Nginx is Stopped." /etc/init.d/nginx start fi echo http method4------------------- if [ `lsof -i tcp:80|wc -l` -gt 0 ] then echo "Nginx is Running." else echo "Nginx is Stopped." /etc/init.d/nginx start fi echo http method5------------------- [ `rpm -qa nmap|wc -l` -lt 1 ] && yum install nmap -y &>/dev/null if [ `nmap 127.0.0.1 -p 80 2>/dev/null|grep open|wc -l` -gt 0 ] then echo "Nginx is Running." else echo "Nginx is Stopped." /etc/init.d/nginx start fi echo http method6------------------- [ `rpm -qa nc|wc -l` -lt 1 ] && yum install nc -y &>/dev/null if [ `nc -w 2 127.0.0.1 80 &>/dev/null&&echo ok|grep ok|wc -l` -gt 0 ] then echo "Nginx is Running." else echo "Nginx is Stopped." /etc/init.d/nginx start fi echo http method7------------------- if [ `ps -ef|grep -v grep|grep nginx|wc -l` -ge 1 ] then echo "Nginx is Running." else echo "Nginx is Stopped." /etc/init.d/nginx start fi echo http method8------------------- if [[ `curl -I -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1` =~ [23]0[012] ]] then echo "Nginx is Running." else echo "Nginx is Stopped." /etc/init.d/nginx start fi echo http method9------------------- if [ `curl -I http://127.0.0.1 2>/dev/null|head -1|egrep "200|302|301"|wc -l` -eq 1 ] then echo "Nginx is Running." else echo "Nginx is Stopped." /etc/init.d/nginx start fi echo http method10------------------- if [ "`curl -s http://127.0.0.1`" = "oldboy" ] then echo "Nginx is Running." else echo "Nginx is Stopped." /etc/init.d/nginx start fi ssh服务开启,关闭脚本 #!/bin/sh path=/etc/init.d/sshd if [ $# -ne 1 ] then echo $"usage:$0{start|stop|resatrt}" exit 1 fi if [ "$1" = "start" ] then $path start if [ `netstat -lntup|grep sshd|wc -l` -ge 1 ] then echo "sshd is started" exit 0 fi elif [ "$1" = "stop" ] then $path stop if [ `netstat -lntup|grep sshd|wc -l` -eq 0 ] then echo "sshd is stop" exit 0 fi elif [ "$1" = "restart" ] then $path stop sleep 2 $path start if [ `netstat -lntup|grep sshd|wc -l` -ge 1 ] then echo "sshd is restarted" else echo echo $"usage:$0{start|stop|resatrt}" exit 1 fi fi 安装lamp,lnmp脚本 #!/bin/sh path=/root/scripts [ ! -d "$path" ]&& mkdir $path cat <<EOF 1.[install lamp] 2.[install lnmp] 3.[exit] pls input the num you want: EOF read num [ $num -eq 1 ]&&{ echo "start installing lamp." sleep 2 [ -x "$path/lamp.sh" ]||{ echo "$path/lamp.sh does not exist or can be exec" exit 1 } $path/lamp.sh exit $? } [ $num -eq 2 ]&&{ echo "start installing lnmp." sleep 2 [ -x "$path/lnmp.sh" ]||{ echo "$path/lnmp.sh does not exist or can be exec" exit 1 } $path/lnmp.sh exit $? } [ $num -eq 3 ]&&{ echo bye exit 3 } [ ! $num -eq 1 -o ! $num -eq 2 -o ! $num -eq 3 ]&&{ echo "the num you input must be{1|2|3}" echo "Input ERROR" exit 4 } 跳板机自动输入密码查看 #!/bin/expect spawn ssh [email protected] uptime expect "*password" send "123\n" expect eof 批量生成随机字符文件名 #!/bin/sh Path=/oldboy [ -d "$Path" ]||mkir -p $Path for n in `seq 10` do random=`openssl rand -base64 40|sed ‘s#[^a-z]##g‘|cut -c 2-8` touch $Path/${random}_oldboy.html done 防止恶意破解root账户的脚本 #!/bin/sh cat /var/log/secure | awk ‘/Failed/{print $(NF-3)}‘ |sort|uniq -c|awk ‘{print $2"="$1}‘ >>/root/black.txt DENY=50 for i in $(cat /root/black.txt) do IP=$($i| awk -F‘=‘ ‘{print $1}‘) NUM=$($i| awk -F‘=‘ ‘{print $2}‘) if [ $NUM -gt $DENY ];then grep $IP /etc/hosts.deny > /dev/null if [ $? -gt 0 ];then echo "ssh:$IP" >> /etc/hosts.deny fi fi done
以上是关于工作中编写的一些小脚本的主要内容,如果未能解决你的问题,请参考以下文章