《9 个实用的 Shell 脚本范例.docx》由会员分享,可在线阅读,更多相关《9 个实用的 Shell 脚本范例.docx(4页珍藏版)》请在第壹文秘上搜索。
1、1、Dos攻击防范自动屏蔽攻击IP)%d%b%Y:%H:XM)1.OGmFI1.EusIoca1nginlogsdemo2.access.IogABNORMA1.-IP=(tail-5$IOG_FI1.Egrep$DATlawk,a$l+ENDfor(iin)if(ai10)printi),)forIPin$ABNORMA1._IP;doif$(iptables-vn1.Igrep-c-eq0;thenIptables-IINPUT-s$IP-jDROPechoM$(date+,XF-%T,)$IPM八mp/drop/p.logfidone2、1.inux系统发送告警脚本#yuminsta1.
2、1.max1.xitvietcaai1.rcsetfrom=baOjingtOngZhi163.coSmtP=smtp.163.COmSetSmtP-auth-user=baOjingtOngZhismtp-auth-password*setsmtp-auth三login3、MySQ1.数据库备份单循环ff!/bin/bahDATES(date+%F_XH-%1-%S)HOST-localhostUSER-backupPASS-BACKUP_DIR-/data/db_backupDB_1.IST=$(mysql-h$HOST-uSUSER-p$PASS-s-eshowdatabases;-2d
3、evnullegrep-vDatabaseinformation-schemamysqlperformance-schemasys)forDBin$DB_1.IST;doBACKUP_NAME=$BACKUP_DIR/$DB_$OATE.sqlifImysqldump-h$HOST-u$USER-PSPASS-B$D8$BACKUP_NAME2devnull;thenecho$BACKUP_NAME备份失败!fidone4、MySQ1.数据库备份多循环DATE“(date+%F_%H-%M-%S)HOST-localhostUSER-backupPASS-BACKUP_DIR-/data/db
4、_backupDB_1.IST=$(mysql-h$HoST-uJUSER-p$PASS-s-eshowdatabases;2devnullegrep-vDatabaseinformation-schemamysqlperformance-schemasys)forDBin$DB_1.IST;doBACKUP_DB_DIR-$BACKUP_DIR/$DB_$DATE!-dSBACKUP.DBDIR&mkdir-p$BACKUP_DB_DIR&/dev/nullTAB1.E_1.IST=$(mysql-h$HOSl-uUSER-p$PASS-s-e-USe$DB;showtables;2devn
5、ull)forTAB1.Ein$TAB1.E_1.IST;doBACKUP_NAME$BACKUP_DB_DIR/$TAB1.E).sqlif!InySqIdUmP-h$HOST-U$USER-p*PASS$DBSTAB1.E$BACKUP_NAME2devnull;thenACKUPNAME备份失败Ifidonedone5.Nginx访问访问日志按天切割1.OG-DIR=usrncal/nginx/logsYESTERDAY_TIME=$(date-dyesterday+%F)1.OG_MONTH_DIR=$1.OG_DIR/$(date+%Y-%m)1.OG_FI1.E_1.IST-def
6、ault.access.logfor1.OG.FI1.Ein$1.OG_FI1.E_1.IST;doI-d$1.OG_MoNTH_DIR&akdir-p$1.OG_MONTH_DlRmv$1.OGDIR/$1.OGFI1.E$1.OG_MONTH_DIR/$1.OG_FI1.E_$YESTERDAY_TIMEdonekill-USRl$(Catvarrunngin.pid)6、Nginx访问日志分析脚本#!/bin/bash#H志格式:$remote_addr-$remote_user$time_localJrequestSstatus$body_bytes_sent-$http_refere
7、r$http_user_agent$http_x_forwarded_for1.OG_FI1.E=Ilecho”统计访问Jft多的10个IP-awka$l+ENDprintUV:,Iength(八);for(vina)printv,av$1.OG_FI1.EIsort-k2-nrIhead-lechoecho统计时间段访问G?的IPawkj4=Dec2l8:13:20:25&$410)printv,av)$1.OGFI1.EIsort-k2-nrecho-echo”统计访M页Ihi状态Fi数fitawkaS7*j9+ENDfor(vIna)if(av5)printv,av),7、堂看网卡实时流
8、IS脚本NIC=Jlecho-eeInOufrjhiIetrue;doO1.D_IN=$(awkS-SNIC,(printS-,tNICprintSNIC,printSe-,SNIC,print$2procnetdev)$10procnetdev)$2),procnetdev)$10procnetdev)H$(devnull;then(echo*1*devnull2&l;crontab-1)ICrontabfise(inuxsed-i/SE1.INUX/spermissivedisabled,etcselinuxconfigifegrep7.0-9/etc/redhat-releasedevn
9、ull;thenSystemctlstopfirewalldsystemctldisablefIrewalldelifegrep,6.0-9metcredhat-release&/dev/null;thenserviceiptablesstopChkconfigiptablesOfffiifIgrepHISTTlMEFoRMATetcbashrc;thenecho,exportHISTTIMEFORMATJ%F%T,whoami,etcbashrcfiif!grep,TMOUT=600etcprofile&/dev/nul1;thenecho-exportTMOUT=600”etcprofil
10、efised-iaSZfPerniitRoot1.oginyesPer*itRoot1.oginno/etcsshsshd-config:sed-i,s/AMAI1.TO=root/MAI1.TO=M/etccrontabifIgrep-softnofile65535”etcsecritylimits.confSdevnull;thencatetcsecuritylimits.cofetcsysctl.confprocsysvmswappinessyuminstallgccmakeautoconfvimsysstatnet-toolsiostatif9、监控100台服务器磁盘利用率脚本HOST
11、_INFO=host.infoforIPin$(awk/AA#/print$1$HoS1.lNF0);doUSER=$(axk-vip=$IPip*=Jlprint$2$HoSTjNFO)PORT=J(awk-vip=$IPip=$lprint$3$HOST_INFO)TMP_FI1.E-tmpdisk.trnpSSh-p$PORT$1JSE昭$1Pdf-h,$TMP_Fl1.EUSE_RATE_1.IST=$(awk*BEGINOFS=./AVdeV/print$NF,int($5)ITMP_Fl1.E)forUSE-RATEin$USE_RATE_1.IST;doPART_NAME=$USE_RATE%=fUSE_RATE-$USE_RATE#X=if$USE_RATE-ge8;thenechoWarning:$PAR1.NAMEPartitionusageSUSE_RATE%!”fldonedone