工作中编写的一些小脚本

#批量处理域名访问时间
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
时间: 2024-10-08 00:00:47

工作中编写的一些小脚本的相关文章

爬虫:工作中编写的一个python爬取web页面信息的小demo

最近公司要求编写一个爬虫,需要完善后续金融项目的数据,由于工作隐私,就不付被爬的网址url了,下面总结下spider的工作原理. 语言:python:工具:jupyter: (1)使用requests模块,获取url页面. import requests url = "http://www.~~~~~~~~~~~~~~~~~~~~~~~~~~" r = requests.get(url) (2)解析html页面(若是pdf页面需要其他工具模块)需要使用BeautifulSoup模块,把

工作中常遇到的小知识

一则是Firefox浏览器调试:Firebug有6个主要的功能:控制台.Html查看器.Css查看器.脚本调试器.Dom查看器.网络状况监视 控制台显示当前页面中的javascript错误以及警告,并提示出错的文件和行号,方便调试.而且在调试Ajax的时候也是特别有用,能够在控制台里看到每一个XMLHttpRequests请求post出去的参数.URL,http头以及回馈的内容,还能在控制台中查看变量内容,直接运行javascript语句,就算是大段的javascript程序也能够正确运行并得到

个人工作中ssd、audio python脚本总结

1.os.system(cmd)或者os.popen(cmd)调用外部命令 cmd中需要注意特殊字符的转义功能,如: USBSTOR\DISK&VEN_GENERIC-&PROD_SD/MMC&REV_1.00\20100818841300000&0 &在批处理中会将前段与后段分割,使用^&可避免转义: 或者整个字符串使用""括起来 同理,如果字符中有‘<','>',无需转义时使用^<,^>

工作中的第一份LoadRunner脚本

录制的第一份脚本 虽然是第一份但是调试执行,跑场景等都成功了. Action() { web_url("login1.jsp", "URL=http://192.168.1.102:8080/eos-default/countrules/login1.jsp", "TargetFrame=", "Resource=0", "RecContentType=text/html", "Referer=&

工作中总结的编程小技巧

技巧一:int-->bool要注意的!bool a=255; bool b= 254; 理论上应该认为a==b,但是根据编译器的不同,结果可能完全不一样 所以更好的写法是 char ch =1; 或者0bool a = (0 != atoi(ch)); 技巧二:将std::string转int,double先利用c_str()转成C string,再用atoi()与atof() 技巧三:const_cast操作符可以强置去除const指针的const属性 技巧四:CString读取文本的每一行C

9.28工作中的一些常见小坑---样式类

一.自定义select样式: select { /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/ border: solid 1px #000; /*很关键:将默认的select选择框样式清除*/ appearance:none; -moz-appearance:none; -webkit-appearance:none; /*在选择框的最右侧中间显示小箭头图片*/ background: url("http://ourjs.github.io/static/2015/a

Mysql 监控小脚本

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司.Mysql是最流行的关系型数据库管理系统,在WEB应用方面MySQL是最好的RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一.MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性.MySQL所使用的SQL语言是用于访问数据库的最常用标准化语言.MyS

Shell进行MySQL表跨服务器增量更新同步小脚本

工作所需写的小脚本. # !/bin/bash # Author : 蛙鳜鸡鹳狸猿# create_ts : 2017年 01月 03日 星期二 14:48:14 CST# program : Incremental transfer table records between MySQL servers# crontab : 11/min# # # __init__ host_src=yourht_A # source db server config listuser_src=rootpsw

【工作中的Python】随机点名小脚本

背景:项目组每周的例会中,有一项固定内容就是技术分享,可以是与工作相关或无关的任何技术主题.进行技术分享讲解的同学是随机抽签的.由此做了一个Python的小脚本用于抽取姓名. 脚本内容如下: #!/usr/bin/python import os import sys import tty, termios import random name_list = ["member_1","member_2","member_3"] input = '