脚本写的好,下班下得早!咱们的日常工作除了编写程序代码,还不可避免地需要处理相关的测试和验证工作。
例如,访问某个网站一直不通,需要确定此地址是否可访问,服务器返回什么,进而确定问题在于什么。完成这个任务,如果一味希望采用编译型语言来编写这样的代码,实践中的时间和精力是不够的,这个时候就需要发挥脚本的神奇作用!
好不夸张的说,能否写出高效实用的脚本代码,直接影响着一个程序员的性福生活[下班时间]。下面整理 13 个实用的脚本,需要的时候改改直接用,建议收藏!
显示系统使用的以下信息:
主机名、IP地址、子网掩码、网关、DNS服务器IP地址信息
#!/bin/bash
IP=`ifconfig eth0 | head -2 | tail -1 | awk '{print $2}' | awk -F":" '{print $2}'`
ZW=` ifconfig eth0 | head -2 | tail -1 | awk '{print $3}' | awk -F":" '{print $2}'`
GW=`route -n | tail -1 | awk '{print $2}'`
HN=`hostname`
DNS=`head -1 /etc/resolv.conf | awk '{print $2}'`
echo '此机IP地址是' $IP
echo '此机子网掩码是' $ZW
echo '此机网关是' $GW
echo '此机主机名是' $HN
echo '此机DNS是' $DNS
#!/bin/bash
DAY=`date +%Y%m%d`
SIZE=`du -sh /var/lib/mysql`
echo "Date: $DAY" >> /tmp/dbinfo.txt
echo "Data Size: $SIZE" >> /tmp/dbinfo.txt
cd /opt/dbbak &> /dev/null || mkdir /opt/dbbak
tar zcf /opt/dbbak/mysqlbak-${DAY}.tar.gz /var/lib/mysql /tmp/dbinfo.txt &> /dev/null
rm -f /tmp/dbinfo.txt
crontab-e
55 23 */3 * * /opt/dbbak/dbbak.sh
每备份文件保存到系统的/mysqlbak目录里
用系统日期做备份文件名 webdb-YYYY-mm-dd.sql
每次完整备份后都生成新的binlog日志
把当前所有的binlog日志备份到/mysqlbinlog目录下
#mkdir /mysqlbak
#mkdir /mysqlbinlog
#service mysqld start
cd /shell
#vi webdb.sh
#!/bin/bash
day=`date +%F`
mysqldump -hlocalhost -uroot -p123 webdb > /mysqlbak/webdb-${day}.sql
mysql -hlocalhost -uroot -p -e "flush logs"
tar zcf /mysqlbinlog.tar.gz /var/lib/mysql/mysqld-bin.0*
#chmod +x webdb.sh
#crontab -e
30 23 * * 7 /shell/webdb.sh
只检查服务vsftpd httpd sshd crond、mysql中任意一个服务的状态
如果不是这5个中的服务,就提示用户能够检查的服务名并退出脚本
如果服务是运行着的就输出 "服务名 is running"
如果服务没有运行就启动服务
方法1:使用read写脚本
#!/bin/bash
read -p "请输入你的服务名:" service
if [ $service != 'crond' -a $service != 'httpd' -a $service != 'sshd' -a $service != 'mysqld' -a $service != 'vsftpd' ];then
echo "只能够检查'vsftpd,httpd,crond,mysqld,sshd"
exit 5
fi
service $service status &> /dev/null
if [ $? -eq 0 ];thhen
echo "服务在线"
else
service $service start
fi
或
方法2:使用位置变量来写脚本
if [ -z $1 ];then
echo "You mast specify a servername!"
echo "Usage: `basename$0` servername"
exit 2
fi
if [ $1 == "crond" ] || [ $1 == "mysql" ] || [ $1 == "sshd" ] || [ $1 == "httpd" ] || [ $1 == "vsftpd" ];then
service $1 status &> /dev/null
if [ $? -eq 0 ];then
echo "$1 is running"
else
service $1 start
fi
else
echo "Usage:`basename $0` server name"
echo "But only check for vsftpd httpd sshd crond mysqld" && exit2
fi
输出192.168.1.0/24网段内在线主机的ip地址
统计不在线主机的台数,
并把不在线主机的ip地址和不在线时的时间保存到/tmp/ip.txt文件里
#!/bin/bash
ip=192.168.1.
j=0
for i in `seq 10 12`
do
ping -c 3 $ip$i &> /dev/null
if [ $? -eq 0 ];then
echo 在线的主机有:$ip$i
else
let j++
echo $ip$i >> /tmp/ip.txt
date >> /tmp/ip.txt
fi
done
echo 不在线的主机台数有 $j
用交互式的输入方法实现自动登录论坛数据库,修改用户密码
[root@test1 scripts]# vim input.sh
#!/bin/bash
End=ucenter_members
MYsql=/home/lnmp/mysql/bin/mysql
read -p "Enter a website directory : " webdir
WebPath=/home/WebSer/$webdir/config
echo $WebPath
read -p "Enter dbuser name : " dbuser
echo $dbuser
read -sp "Enter dbuser password : " dbpass
read -p "Enter db name : " dbname
echo $dbname
read -p "Enter db tablepre : " dbtablepre
echo $dbtablepre
Globalphp=`grep "tablepre*" $WebPath/config_global.php |cut -d "'" -f8`
Ucenterphp=`grep "UC_DBTABLEPRE*" $WebPath/config_ucenter.php |cut -d '.' -f2 | awk -F "'" '{print $1}'`
if [ $dbtablepre == $Globalphp ] && [ $dbtablepre == $Ucenterphp ];then
Start=$dbtablepre
Pre=`echo $Start$End`
read -p "Enter you name : " userset
echo $userset
Result=`$MYsql -u$dbuser -p$dbpass $dbname -e "select username from $Pre where username='$userset'\G"|cut -d ' ' -f2|tail -1`
echo $Result
if [ $userset == $Result ];then
read -p "Enter your password : " userpass
passnew=`echo -n $userpass|openssl md5|cut -d ' ' -f2`
$MYsql -u$dbuser -p$dbpass $dbname -e "update $Pre set password='$passnew' where username='$userset';"
$MYsql -u$dbuser -p$dbpass $dbname -e "flush privileges;"
else
echo "$userset is not right user!"
exit 1
fi
else
exit 2
fi
1)本机的数据库服务是否正在运行
2)能否与主数据库服务器正常通信
3)能否使用授权用户连接数据库服务器
4)本机的slave_IO进程是否处于YES状态
本机的slave_SQL进程是否处于YES状态
[root@test1 scripts]# vim test.sh
#!/bin/bash
netstat -tulnp | grep :3306 > /dev/null
if [ $? -eq 0 ];then
echo "服务正在运行"
else
service mysqld start
fi
ping -c 3 192.168.1.100 &> /dev/null
if [ $? -eq 0 ];then
echo "网络连接正常"
else
echo "网络连接失败"
fi
mysql -h192.168.1.100 -uroot -p123456 &> /dev/null
if [ $? -eq 0 ];then
echo "数据库连接成功"
else
echo "数据库连接失败"
fi
IO= mysql -uroot -p123 -e "show slave status\G" | grep Slave_IO_Running | awk '{print $2}' > /dev/null
SQL= mysql -uroot -p123 -e "show slave status\G" | grep Slave_SQL_Running | awk '{print $2}' /dev/null
if [ IO==Yes ] && [ SQL==Yes ];then
echo “IO and SQL 连接成功”
else
echo "IO线程和SQL线程连接失败"
fi
结合/var/log/secure文件,将ssh登陆失败次数大于N的IP封顶
N=3
SEC_FILE=/var/log/secure
for ip in `grep "Failed password" $SEC_FILE|grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}"|sort -n|uniq -c|awk '{if($1>$N) print $2}'`
do
iptables -A INPUT -s $ip -p tcp --dport 22 -j DROP
done
#!/bin/bash
#Denyhosts SHELL SCRIPT
#2017-01-24
#
#When a IP is accessed 50 times through sshd, it is written to the hosts.deny file,
#which prohibits the IP from connecting to the host via sshd
#
#Add to timing task
cat /var/log/secure | awk '/Failed/{print $(NF-3)}' | sort | uniq -c | awk '{print $2"="$1;}' > /root/black.txt
DEFINE=50
for i in $(cat /root/black.txt)
do
IP=$( $i | awk -F'=' '{print $1}')
NUM=$( $i | awk -F'=' '{print $2}')
if [$NUM -gt $DEFINE]; then
grep $IP /etc/hosts.deny > /dev/null
if [$? -gt 0];then
echo "sshd:$IP" >> /etc/hosts.deny
fi
fi
done
# vi check_network.sh
"#!/bin/bash
NIC=$1
echo -e "" In ------ Out""
while true; do
OLD_IN=$(awk '$0~""'$NIC'""{print $2}' /proc/net/dev)
OLD_OUT=$(awk '$0~""'$NIC'""{print $10}' /proc/net/dev)
sleep 1
NEW_IN=$(awk '$0~""'$NIC'""{print $2}' /proc/net/dev)
NEW_OUT=$(awk '$0~""'$NIC'""{print $10}' /proc/net/dev)
IN=$(printf ""%.1f%s"" ""$((($NEW_IN-$OLD_IN)/1024))"" ""KB/s"")
OUT=$(printf ""%.1f%s"" ""$((($NEW_OUT-$OLD_OUT)/1024))"" ""KB/s"")
echo ""$IN $OUT""
sleep 1
done"
执行:sh check.sh ens33
# vi check_web.sh
"#!/bin/bash
URL_LIST=""www.baidu.com www.sina.cn""
for URL in $URL_LIST; do
FAIL_COUNT=0
for ((i=1;i<=3;i++)); do
HTTP_CODE=$(curl -I -m 10 -o /dev/null -s -w %{http_code} $URL)
if [ $HTTP_CODE -eq 200 ]; then
echo ""$URL OK""
break
else
echo ""$URL retry $FAIL_COUNT""
let FAIL_COUNT++
fi
done
if [ $FAIL_COUNT -eq 3 ]; then
echo ""Warning: $URL Access failure!""
fi
done"
# vi check_mysql.sh
"#!/bin/bash
USER=bak
PASSWD=123456
IO_SQL_STATUS=$(mysql -u$USER -p$PASSWD -e 'show slave statusG' |awk -F:
'/Slave_.*_Running/{gsub("": "","":"");print $0}') #gsub 去除冒号后面的空格
for i in $IO_SQL_STATUS; do
THREAD_STATUS_NAME=${i%:*}
THREAD_STATUS=${i#*:}
if [ ""$THREAD_STATUS"" != ""Yes"" ]; then
echo ""Error: MySQL Master-Slave $THREAD_STATUS_NAME status is
$THREAD_STATUS!""
fi
done"
"#!/bin/bash
DATE=$(date +%d/%b/%Y:%H:%M)
LOG_FILE=/etc/nginx/logs/access.log
ABNORMAL_IP=$(tail -n5000 $LOG_FILE |grep $DATE |awk '{a[$1]++}END{for(i in a)if(a[i]>10)print i}')
for IP in $ABNORMALIP; do
if [ $(iptables -vnL |grep -c ""$IP"") -eq 0 ]; then
iptables -I INPUT -s $IP -j DROP
echo ""$(date +'%F%T') $IP"" >> /tmp/drop_ip.log
fi
done"
本人还编写很多日常实用脚本 由于篇幅太大 就不一一写出来了 需要的朋友可以友情三连后扫描下方二维码 暗号:csdn 即可领取!