匯總2022超全必備108道shell腳本面試題_第1頁(yè)
匯總2022超全必備108道shell腳本面試題_第2頁(yè)
匯總2022超全必備108道shell腳本面試題_第3頁(yè)
匯總2022超全必備108道shell腳本面試題_第4頁(yè)
匯總2022超全必備108道shell腳本面試題_第5頁(yè)
已閱讀5頁(yè),還剩61頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

chsh用戶名s新> 用戶變量由系統(tǒng)用戶來生成和定義,變量的值可以通過命令“echo$<變量名>”查看。2>&12>&1如ls/usr/share/docout.txt2>&1&>&>如lsusr/share/doc&>out.txt ifif]if] root@localhost:~#root@localhost:~#lsroot@localhost:~#root@localhost:~#lsls:cannotaccess/usr/bin/share:Nosuchfileordirectoryroot@localhost:~#echo$?2 答:在if-then中使用測(cè)試命令(-gt等)if[$x-gt$y]echo“xisgreaterthanecho“yisgreaterthan 答:break命令一個(gè)簡(jiǎn)單的用途是退出執(zhí)行中的循環(huán)。我們可以在while和until循環(huán)中使用break出循環(huán)。 casecase變量 答:如同for循環(huán),while循環(huán)只要條件成立就重復(fù)它令塊。不同于for循環(huán),while循環(huán)會(huì)不斷迭whilewhile]命令 oda+x bang。它的意思是命令通過/bin/bash forforin 答:使用'-x'參數(shù)(sh-xmyscript.sh)可以調(diào)試s 。另一個(gè)種方法是使用‘-nv’參數(shù)(sh-nv 答:下面的表列出了Bourne 第個(gè)命令行參數(shù) -d-e-f-r-s-w-x 如果文件存在,返回e #Thisisaecho“Iamloggedinas答:read命令可以來自終端(使用鍵盤)的數(shù)據(jù)。read命令得到用戶的輸入并置于你給出的變量中。例子如下:echo‘Pleaseenteryourreadecho“MyNameis#MyNameisunset變量名##expr5+test=$[16test=$[16+4];test=$[16+答:do-while語(yǔ)句類似于while語(yǔ)句,但檢查條件語(yǔ)句之前先執(zhí)行命令(LCTT譯注:意即至少執(zhí)行一{while條件 答:函數(shù)是擁有名字的代碼塊。當(dāng)我們定義代碼塊,我們就可以在我們的調(diào)用函數(shù)名字,該塊就會(huì)被執(zhí)行。示例如下所示:$$diskusage(){df-h; function函數(shù)名{[return}1-24 方法方法#echo$RANDOM|md5sum|cut-c1-8方法#opensslrand-base644方法#cat/proc/sys/kernel/random/uuid|cut-c1-8方法方法#echo$RANDOM|cksum|cut-c1-方法#opensslrand-base644|cksum|cut-c1-方法#date+%N|cut-c1-方法方法functionecho_color()if[$1=="green"];elif[$1=="red"];thenecho-e}方法function{case$1inecho-eecho-eecho"Example:echo_colorred}使用方法:echo_colorgreenif[$1=="green"];elif[$1=="red"];thenecho-e}#如果用戶文件存在并且大小大于0if[-s$USER_FILE];mv$USER_FILE${USER_FILE}-echo_colorgreen"$USER_FILEexist,rename${USER_FILE}-echo"----------------">>forUSERinuser{1..10};if!id$USER&>/dev/null;PASS=$(echo$RANDOM|md5sum|cut-c1-8)useradd$USERecho$PASS|passwd--stdin$USER&>/dev/nullecho-e"$USER\t$PASS">>$USER_FILEecho"$USERUsercreateecho_colorred"$USERUseralreadyifrpm-qsysstat&>/dev/null;thenecho"sysstatisnotPORT_C=$(ss-anu|grep-cPS_C=$(ps-ef|grepntpd|grep-vcgrep)if[$PORT_C-eq0-o$PS_C-eq0];echo"內(nèi)容"|mail-s IP_LIST="forIPin$IP_LIST;dowhile[$NUM-le3];if-c1$IP>/dev/null;thenecho"$IPissuccessful."#echo isfailureletif[${#FAIL_COUNT[*]}-eq3echo"${FAIL_COUNT[1]} unsetFAIL_COUNT[*]IP_LIST="forIPin$IP_LIST;dofor((i=1;i<=3;i++)); -c1$IP>/dev/null;thenecho"$IP#echo"$IP isfailure$i"letFAIL_COUNT++if[$FAIL_COUNT-eq3];then is _success_status()if-c1$IP>/dev/null;thenecho"$IPissuccessful."}IP_LIST="forIPin$IP_LIST;echo"$IPisDATE=$(date+%F"IP=$(ifconfigeth0|awk-F'[:]+''/inetaddr/{print$4}')#只支持CentOS6 if!whichvmstat&>/dev/null;echo"vmstatcommandnofound,Pleaseinstallprocpspackage."exit1US=$(vmstat|awk'NR==3{print$13}')SY=$(vmstat|awk'NR==3{print$14}')if[$USE-ge50];thenecho"Host:$IPProblem:CPUutilization"|mail-s"CPUMonitor"DATE=$(date+%F"IP=$(ifconfigeth0|awk-F'[:]+''/inetaddr/{print$4}') -m|awk'/Mem/{print$2}') =$(($TOTAL-#內(nèi)存小于1G發(fā) if[$ -lt1024];thenecho"Host:$IPProblem:"|mail-s"MemoryMonitor"DATE=$(date+%F"IP=$(ifconfigeth0|awk-F'[:]+''/inetaddr/{print$4}') foriin$PART_USE;doPART=$(echo$i|cut-d"="-f1)USE=$(echo$i|cut-d"="-f2)MOUNT=$(echo$i|cut-d"="-f3)if[$USE-gt80];thenecho"Host:$IPTotal:Problem:"|mail-s"DiskMonitor"forIPin$(awk'/^[^#]/{print$1}'$HOST_INFO);doUSER=$(awk-vip=$IP'ip==$1{print$2}'$HOST_INFO)PORT=$(awk-vip=$IP'ip==$1{print$3}'ssh-p$PORT$USER@$IP'df-h'>USE_RATE_LIST=$(awk'BEGIN{OFS="="}/^\/dev/{print$1,int($5)}'$TMP_FILE)forUSE_RATEin$USE_RATE_LIST;doif[$USE_RATE-ge80];方法方法check_url()HTTP_CODE=$(curl-o/dev/null--connect-timeout3-s-w"%{http_code}"$1)if[$HTTP_CODE-ne200];thenecho"Warning:$1Access}方法check_url()ifwgetT10tries=1spider$1dev/null2>&1;thenecho"Warning:$1Access}方法1:利用循環(huán)技巧,如果成功就跳出當(dāng)前循環(huán),否則執(zhí)行到最后行方法1:利用循環(huán)技巧,如果成功就跳出當(dāng)前循環(huán),否則執(zhí)行到最后行HTTP_CODE=$(curl-o/dev/null--connect-timeout3-s-w"%{http_code}"$1)if[$HTTP_CODE-eq200];then} forURLin$URL_LIST;docheck_url$URLcheck_url$URLcheck_url$URLecho"Warning:$URLAccessforURLin$URL_LIST;"forfor((i=1;i<=3;i++));HTTP_CODE=$(curl-o/dev/null--connect-timeout3-s-wif[$HTTP_CODE-ne200];thenletFAIL_COUNT++if[$FAIL_COUNT-eq3];echo"Warning:$URLAccessforURLin$URL_LIST;dowhile[$NUM-le3];"HTTP_CODE=$(curl-o/dev/null--connect-timeout3-s-wif[$HTTP_CODE-ne200];thenletNUM++if[${#FAIL_COUNT[*]}-eq3];thenunset IO_SQL_STATUS=$(mysqlu$USERp$PASSWDeshowslavestatus\G|awkF:'/Slave_.*_Running/{gsub(":",":");print$0}')#gsub去除冒號(hào)后面的空格foriin$IO_SQL_STATUS;doif["$THREAD_STATUS"!="Yes"];echo"Error:MySQLMaster-Slave$THREAD_STATUS_NAMEstatus DATE=$(dateABNORMAL_IP=$(tail-n5000access.log|grep$DATE|awk'{a[$1]++}END{for(iina)if(a[i]>100)printi}')forIPin$ABNORMAL_IP;if[$(iptables-vnL|grep-c"$IP")-eq0];theniptables-IINPUT-s$IP-jDROP9]+/,"",$5);{a[$5]++}}END{for(iina)if(a[i]>100)printi}')forIPin$ABNORMAL_IP;if[$(iptables-vnL|grep-c"$IP")-eq0];theniptables-IINPUT-s$IP-jDROPDATE=$(date+"%a%b%e%H:%M")#星期月天時(shí)分%e單數(shù)字時(shí)顯示7,而%d顯示07forIPin$ABNORMAL_IP;if[$(iptables-vnL|grep-c"$IP")-eq0];theniptables-IINPUT-s$IP-jDROPDATE=$(date+"%b%d'/Failed/{a[$(NF-3)]++}END{for(iina)if(a[i]>5)printi}')"forIPin$ABNORMAL_IP;doif[$(iptables-vnL|grep-c"$IP")-eq0];theniptables-AINPUT-s$IP-jDROPecho"$(date+"%F%T")-iptables-AINPUT-s$IP-jDROP">>~/ssh-login-VALID_CHECK=$(echo$IP|awk-F.'$1<=255&&$2<=255&&$3<=255&&$4<=255{printifecho$IP|grep-E"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-{1,3}$">/dev/null;if[$VALID_CHECK=="yes"];thenecho"$IPavailable."echo"$IPnotecho"Format}check_ipcheck_ipif[[$IP=~^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$]];FIELD1=$(echo$IP|cut-d.-f1)FIELD2=$(echo$IP|cut-d.-f2)FIELD3=$(echo$IP|cut-d.-f3)FIELD4=$(echo$IP|cut-d.-f4)if[$FIELD1-le255-a$FIELD2-le255-a$FIELD3-le255-a$FIELD4-255];echo"$IPecho"$IPnotecho"Format}}check_ipcheck_ipVALID_CHECK=$(echo$IP|awk-F.'$1<=255&&$2<=255&&$3<=255&&$4<=255{printifecho$IP|grep-E"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0->/dev/null;if[$VALID_CHECK=="yes"];thenreturn0return1echo"Formaterror!Pleaseinputagain."return1}whiletrue;read-p"PleaseenterIP:"IPcheck_ip$IP[$?-eq0]&&break||if[[$1=~^[0-9]+$]];thenecho"IsNumber."echo"Noif[$1-gt0]2>/dev/null;thenecho"IsNumber."echo"Noecho$1|awk{print$0~/^[0-9]+$/?"IsNumber.":"NoNumber12.14forFILEin$(find$DIR-typef);ifgrep$KEY$FILE&>/dev/null;thenecho"-->$FILE" forFILEin$(find$DIR-typef);ifgrep$KEY$FILE&>/dev/null;thenecho"-->$FILE"Q:39,將新創(chuàng)建的文件名追加到日志 inotifywait-mq--format%f-ecreate$MON_DIR|\whilereadfiles;dofunction{localNUMARRAY_LENGTHNUM=0forNIC_NAMEin$(ls/sys/class/net|grep-vE"lo|docker0");doNIC_IP=$(ifconfig$NIC_NAME|awk-F'[:]+''/inetaddr/{print$4}')if[-n"$NIC_IP"];thenif[-n"$NIC_IP"];thenletif[$ARRAY_LENGTH-eq1];thenreturnelif[$ARRAY_LENGTH-eq0];thenecho"Noavailablenetworkcard!"exit1forNICin${NIC_IP_ARRAY[*]};doecho$NICwhiletrue;#read-p"Pleaseenterlocalusetonetworkcardname:"forNICin${NIC_IP_ARRAY[*]};doif[$NIC_NAME=="$INPUT_NIC_NAME"];thenreturnecho"Notmatch!Pleaseinput}traffic_unit_conv(){localif[$traffic- ];elif[$traffic-lt ];thenprintf"%.1f%s""$(($traffic/1024))"}echo-e" whiletrue;OLD_IN=$(awk-F'[:]+''$0~"'$NIC'"{print$3}'/proc/net/dev)sleep1NEW_IN=$(awk-F'[:]+''$0~"'$NIC'"{print$3}'sleep1Q:42、MySQLcd"ifmysqldump-h$HOST-u$USER-p$PASS--single-transaction--routines--triggersB$DB>$SQL_FILE;zip$BAK_FILE$SQL_FILE&&rm-f$SQL_FILEif[!-s$BAK_FILE];thenecho"$DATE內(nèi)容"|mail-s "echo"$DATE內(nèi)容"|mail-s "find$BACKUP_DIR-name'*.zip'-ctime+14-execrm{}場(chǎng)景:使用源碼包安裝Nginx不含帶服務(wù)管理,也就是不能使用"servicenginxstart"或"/etc/init.d/nginxstart",所以寫了以下的服務(wù)管理。#Description:OnlysupportRedHat./etc/init.d/functionsif[-z"$PID"];thenstop()$DAEMON-sstopsleep1[!-f$PID]&&action"* $NAME"/bin/true||action"*$NAME"}start()sleep[-f$PID]&&action"*Starting$NAME"/bin/true||action"*$NAME"}reload()$DAEMON-s}test_config()$DAEMON-}case"$1"if[!-f$PID];then

echo"$NAMEisrunning..."exit0if[-f$PID];then

echo"$NAMEnotrunning!"exit0if[!-f$PID];echo"$NAMEnotrunning!"

[-f$PID]&&echo"$NAMEisrunning..."||echo"$NAMEnot|status}"exit :{##catWeb0rootDB1rootwhiletrue;doselectNAMEin$(awk'{print$1}'$HOST_FILE)quit;do[${NAME:=empty}=="quit"]&&exit0IP=$(awk-vNAME=${NAME}'$1==NAME{print$2}'$HOST_FILE)USER=$(awk-vNAME=${NAME}'$1==NAME{print$3}'$HOST_FILE)PORT=$(awk-vNAME=${NAME}'$1==NAME{print$4}'if[$IP];echo"Name:$NAME,IP:sshoStrictHostKeyChecking=nop$PORTiid_rsa$USER@$IP#echo"Inputerror,Pleaseenteragain!"Q:45從FTP服務(wù)器文if[$#-ne1];echo"Usage:$0dir=$(dirname$1)ftpnv nopen0ftp服務(wù)器useradminpassword cd$dirwhile[$COUNT-le5];if$INT=~^[0-9]+$thenecho"輸入必須是整數(shù)!"exitelif$INTgt100thenecho"輸入必須是100以內(nèi)!"exit1[$MIN-lt$INT]&&MIN=$INT[$MAX-gt$INT]&&MAX=$INTletCOUNT++echo"SUM:$SUM"echo"MIN:$MIN"echo"MAX:forforiin$(echo"456");doevala$i=$iecho$a4$a5foriin$(evalecho$*);do letnum+=1echo$node1$node212arr=(456)arr=(456)INDEX3=$(echo#lsarticle_1.htmlarticle_2.htmlforforfilein$(ls*html);domv$file#mv$file$(echo$file|sed-r's/.*(_.*)/bbs\1/')#mv$file$(echo$file|echobbs_$(cut-d_-f2)forforfilein$(find.-maxdepth1-name"*html");domv$filebbs_${file#*_}##renamearticlebbs ##find.-name"*.html"-execdu-k{}\;|awk'{sum+=$1}END{printforforsizein$(ls-l*.html|awk'{print$5}');doPORT="222580forPORTin$PORT;ifecho&>/dev/null>/dev/tcp/$HOST/$PORT;thenecho"$PORTopen"echo"$PORTQ:51Expect實(shí)現(xiàn)SSH expect<<EOFsettimeoutexpect{"password:"{send"$PASS\r"}}expect"$USER@*"{send"$1\r"}expect"$USER@*"{send"exit\r"}expecteofexpect-cexpect{\"(yes/no)\"{send\"yes\r\";\"password:\"{send\"$PASS\r\";\"$USER@*\"{send\"df-h\rexit\r\";#catlogin.exp#catlogin.expsetip[lindex$argv0]setuser[lindex$argv1]setpasswd[lindex$argv2]setcmd[lindex$argv3]if{$argc!=4}{puts"Usage:expectlogin.expipuserpasswd"exit1}settimeout30expect{"password:"{send"$passwd\r"}}expect"$user@*"{send"$cmd\r"}expect"$user@*"{send"exit\r"}expecteofforipin$(awk'{print$1}'$HOST_INFO)user=$(awk-vI="$ip"'I==$1{print$2}'$HOST_INFO)pass=$(awk-vI="$ip"'I==$1{print$3}'$HOST_INFO)expectlogin.exp$ip$user$pass$1#catuser_info.txt#catuser_info.txt#cat1718內(nèi)容格式:IPUserPasswordPortforIPin$(awk'/^[^#]/{print$1}'$OLD_INFO);doUSER=$(awk-vI=$IP'I==$1{print$2}'$OLD_INFO)PASS=$(awk-vI=$IP'I==$1{print$3}'$OLD_INFO)PORT=$(awk-vI=$IP'I==$1{print$4}'NEW_PASS=$(mkpasswd-l8)# echo expect-c$PORT">>settimeout2expect\"(yes/no)\"{send\"password:\"{send\"$USER@*\"{send\"echo\'$NEW_PASS\'|passwd--stdin$USER\r#catnew_pass.txt17#catnew_pass.txt1718##awk'BEGIN{for(n=0;n++<9;){for(i=0;i++<n;)printfi"x"n"="i*n"";printforfor((i=1;i<=9;i++));getopts是一個(gè)解析選項(xiàng)參數(shù)的工具。whilegetopts:f:n:option;docase$optionin[!-f$FILE]&&echo"$FILEFilenotexist!"&&sed-n"${OPTARG}p"echo"Usage:$0-f<file_path>-n<line_number>"echo"-f,--file specifiedfile"echo"-n,--line-number exit1作者:Q:54IP=`ifconfigeth0|head-2|tail-1|awk'{print$2}'|awk-F":"ZW=`ifconfigeth0|head-2|tail-1|awk'{print$3}'|awk-F":"GW=`route-n|tail-1|awk'{print$2}'`DNS=`head1/etc/resolv.conf|awk{print$2}'`echo'此機(jī)IP地址是'$IPecho'此機(jī)網(wǎng)關(guān)是'$GWecho此機(jī)DNS是 SIZE=`du-shecho"Date:$DAY">>echo"DataSize:$SIZE">>cd/opt/dbbak&>/dev/null||mkdirtarzcf/opt/dbbak/mysqlbak-${DAY}.tar.gz/var/lib/mysql/tmp/dbinfo.txtrm-f5523*/3** #mkdir/mysqlbak#mkdir/mysqlbinlog#mkdir/mysqlbak#mkdir/mysqlbinlogcd/s#viwebdb.shmysql-hlocalhost-uroot-p-e"flushlogs" od+xwebdb.sh#crontab-3023**7 只檢查服務(wù)vsftdhttpdsshdcrond、mysl如果不是這5個(gè)中的服務(wù),就提示用戶能夠檢查的服務(wù)名并退出如果服務(wù)是運(yùn)行著的就輸出"服務(wù)名isrunning"readp請(qǐng)輸入你的服務(wù)名if[$service!='crond'-a$service!='httpd'-a$service!='sshd'-a!='mysqld'-a$service!='vsftpd'];thenecho只能夠檢查'vsftpd,httpd,crond,mysqld,sshd"exit5service$servicestatus&>if[$?-eq0];thhenecho"服務(wù) ifif[-z$1echo"Youmastspecifyaservername!"echo"Usage:`basename$0`servername"exit2if[$1=="crond"]||[$1=="mysql"]||[$1=="sshd"]||[$1==]||[$1=="vsftpd"];thenservice$1status&>/dev/nullif[$?-eq0];thenecho"Usage:`basename$0`serverecho"Butonlycheckforvsftpdhttpdsshdcrondmysqld"&&exit2Q:58foriin`seq1012`-c3$ip$i&>/dev/nullif[$?-eq0];then date>>/tmp/ip.txtecho 的主機(jī)臺(tái)數(shù)有[root@test1[root@test1scripts]#vimread-p"Enterawebsitedirectory:"webdir echo read-p"Enterdbusername:"dbuserecho$dbuserread-sp"Enterdbuserpassword:"read-p"Enterdbname:"dbnameecho$dbnameread-p"Enterdbtablepre:"dbtablepreecho$dbtablepreGlobalphp=`grepGlobalphp=`grep"tablepre*"th/config_global.php|cut-d"'"-Ucenterphp=`grep"UC_DBTABLEPRE*"$W awk-F"'"'{print$1}'`if[$dbtablepre==$Globalphp]&&[$dbtablepre==$Ucenterphpread-p"Enteryouname:"usersetecho$usersetResult=`$MYsql-u$dbuser-p$dbpass$dbname-e"selectusernamefrom$Prewhereusername='$userset'\G"|cut-d''-f2|tail-1`echoif[$userset==$Resultread-p"Enteryourpassword:"userpasspassnew=`echo-n$userpass|opensslmd5|cut-d''-$MYsql-u$dbuser-p$dbpass$dbname-e"update$Presetpassword='$passnew'whereusername='$userset';"$MYsql-u$dbuser-p$dbpass$dbname-e"flushecho"$usersetisnotrightuser!"exit1exit[root@test1[root@test1scripts]#vimnetstat-tulnp|grep:3306>/dev/nullif[$?-eq0];thenecho服務(wù)正在運(yùn)行-c300&>/dev/nullif[$?-eq0];thenecho網(wǎng)絡(luò)連接正常echo網(wǎng)絡(luò)連接失敗mysql-h00-uroot-p123456&>/dev/nullif[$?-eq0];thenIO=mysql-uroot-p123-e"showslavestatus\G"|grepSlave_IO_Running|awk'{print$2}'>/dev/nullSQL=mysql-uroot-p123-e"showslavestatus\G"|grepSlave_SQL_Running|awk'{print$2}'/dev/nullif[IO==Yes]&&[SQL==Yesecho“IOandSQL連接成功echoIO線程和SQL線程連接失敗"ss_ss_row=`cat_list|wc- -H'Content-Type:application/json "msgtype":"text","text":{"content":}apache_check_80(){ip=$1HTTP_CODE=`curl-o/dev/null-s-w"%{http_code}"if[$HTTP_CODE!=200trans_text\n$ipApache服務(wù)器狀態(tài)異常,網(wǎng)頁(yè)返回碼'"$HTTP_CODE"' }ss_row=`cat_list|wc-fortempin`seq1 Ip_Addr=`cat_list|head-n$temp|tail-napache_check_80sleepwhile["$ip_num"-le"$(expr${#remote_ip[@]}-l)"]ssh"$remote_user"@"${remote_ip[$ip_num]}"df-h>/tmp/diskcheck_tmpgrep'^/dev/*'/tmp/diskcheck_tmp|awk'{print$5}'|sed's/\%//g'while["$read_num"-le$(wc-l</tmp/diskcheck_num_tmp)]size=$(sed-n"$read_num"'p'/tmp/diskcheck_num_tmp)if["size"-gt"$FSMAX"]$(grep'^/dev/*'/tmp/diskcheck_tmp|sed-n>$(echo${remote_ip[$ip_num]})>>$(mail-s"diskcheck_alert"adminread_num=$(expr$read_num+ip_num=$(expr$ip_num+ IP=`ifconfigeth0|grep"inetaddr"|cut-f2-d":"|cut-f1-d""`SPACE=`df-hP|awk'{printint($5)}'`if[$SPACE-ge90]echo"$IP服務(wù)器磁盤空間使用率已經(jīng)超過90%,請(qǐng)及時(shí)處理。"|mail-s"$IP:網(wǎng)絡(luò)技術(shù)干貨圈#!#!ftp-n<<END_FTPopenusertest//用戶名prompt DAY=`dateSIZE=`du-shecho"Date:$DAY">>echo"DataSize:$SIZE">>cd/opt/dbbak&>/dev/null||mkdirtarzcf/opt/dbbak/mysqlbak-${DAY}.tar.gz/var/lib/mysql/tmp/dbinfo.txtrm-f5523*/3**Q:66.declaredeclare-aforiin`seq4049`ary[$i]="echoecho-en"\e[$i;5mdeclare-aforsin`seq110000`foriin`seq4049`ary[$i]="echo-en"\e[$i;5mQ:67.for((i=1;i<12;i++))if[[$i-le6]]for((j=$((12-i));j>i;j--))echo-n"for((m=1;m<=$((2*i-1));m++))echo-n"*echo""elif[[$i-gt6]]for((j=$((12-n));j>n;j--))echo-n"for((m=1;m<=$((2*n-1));m++))echo-n"*echo#!/usr/bin/expect#!/usr/bin/expect-setipaddress[lindex$argvsetpasswd[lindex$argvsettimeoutspawnssh-copy-idexpect"yes/no"{send"yes\r";exp_continue"password:"{send"$passwd\r"}#expect#send"mkdir-p#send#expect#i#命令運(yùn)行完你要期待個(gè)結(jié)果結(jié)果就是返回提示符了(是#或者"THHP_CODE=`curl-o/dev/null-s-w"%{http_code}"if[$HTTP_CODE!=200echo-e"apache[-f$log]||touchfunction{ip=`echo$line|awk'{print$2}'`ifif[$count-gt100]&&[`iptables-L-n|grep|wc-l`-lt1iptables-IINPUT-s$ip-jDROPecho-e"$list}function{netstat-an|grep"EST"|awk-F'[:]+''{print$6}'|sort|uniq-sleep180} function{if!rpm-qa|grep-q"^$1"yuminstall$1if[$?-eq0]echo-e"$iinstallisecho-e"$1installecho-e"yianzhuang!}forinsinmysqlphphttpdMyInstall declare-aforiin`seq110`echo-e"Array_1:if[${array[$y]}-gt${array[$y+1]}]echo-e"Array_2:arr=("|""/""-""\\"while[$i-le100]letprintf"[%-100s][%d%%][\e[43;46;1m%c\e[0m]\r""$bar""$i""${arr[$index]}"leti++letindex++printf.forUnamein`cat/root/useradd.txt|gawk'{print$1}'`if[$?-eq0]echo-e"這個(gè)賬號(hào)已存在!來源: forUpasswdin`cat/root/useradd.txt|gawk'{print$2}'`useradd$Uname&>echo"$Upasswd"|passwd--stdin$Uname&>/dev/nullif[$?-eq0]echoe創(chuàng)建失敗declare-aforiin`seq020`ary[$i]="sleep1while:;dospeedday='date+%m"-"%d'speedrx_before='ifconfigspeedrx_before='ifconfigeth0|sed-n"8"p|awk'{print$2}'|cut-c7-'speedtx_before='ifconfigeth0|sed-n"8"p|awk'{print$6}'|cut-c7-'sleep2speedrx_after='ifconfigeth0|sed-n"8"p|awk'{print$2}'|cut-c7-'speedtx_after='ifconfigeth0|sed-n"8"p|awk'{print$6}'|cut-c7-'echo"$speedday$speedtimeNow_In_Speed:"$speedrx_result"kbpsNow_OUt_Speed:Q:77.檢測(cè)CPU#Inspect#SunJul3117:25:41CSTexportPATHexportCpuResult=$(top-bn1|grep"Cpu"|awk'{print$5}'|sedif[[$CpuResult<20echo"CPUWARNING:$CpuResult">/service/script/.cpu_in.txttop-bn1>>/service/script./cpu_in.txtmail-s"InspcetCPU"wl<#InsepctHarddisk,Iftheremainingspaceismorethan80%,themessageissenttothewl#TueAug209:45:56CSTexportforRemainingSpacein$(df-h|awk'{print$5}'|grep-v'Use'|sed-eif[[$RemainingSpace>80]];thenecho-e"$RemainingSpace"echo-e"$(df-h|grep$RemainingSpace)">/service/script/.HarddiskWarningmail-s"diskWarning"wl</service/script/.HarddiskWarningcurl-H'Content-Type:application/json'-{"text":{"content":}functions_row=`df|wc-l`foriin`seq2$s_row`temp=(`df-h|head-n$i|tail-n1|awk'{print$5"\t"$6}'`)disk="`echo${temp[0]}|cut-d"%"-f1`"IP=`ifconfig|grep-v""|grep"inetaddr:"|sed's/^.*inetaddr://g'|sed's/Bcas..*$//g'`#echo-e Dat=`date"+%Fif[$disk-ge$dftype]}

echo

========================主機(jī)名:$hostname\nIP地址:$IP\n分區(qū)名$namen使用率$diskn發(fā)生時(shí)間$Datn=========================HTTP_CODE=`curl-o/dev/null-s-w"%{http_code}"ifif[$HTTP_CODE!=200echo========================IP地址:$IP\n返回代碼:$HTTP_CODE\發(fā)生時(shí)間$Dat=========================}desk_checkapache_checksleepQ:80.#InspectMemory:Ifthememoryislessthan500,thensendmailto#TueAug209:13:43CSTexport-m|grep"Mem"|awk'{printif[[MEM<500echo-e"MemoryWarning:$MEM">mail-s"MemoryWarning"wl<Q:81.剩余inode#InspcetInode:IftheINODEislessthan200,themessageissentto#TueAug210:21:29CSTexportInodein$(df-i|grep-v"Filesystem"|awk'{printif[[Inode<200echo-e"$(df-i|grepInode")"> mail-s InodeWarning"wl< declare-iwhileifwho|grep-q-E"^wang"echoe用戶wang登陸了系統(tǒng)\n這是第$count次!letsleep~示例:找出UID為偶數(shù)的所有用戶,顯示其用戶名和IDwhilereadline;userid=$(echo$line|cut-d:-f3)if[$[$userid%2]-eq0];thenecho$line|cut-d:-f1,3done<while[$sum-le30]if[$sum-le9]useraddecho"123456"|passwd--stdinchagechage-d0$userletsum=sum+1#nmapMAC=`catip|awk'$1=="MAC"&&$NF=="(VMware)"{printforiin`seq1IP=`cat/ip|grep-B5$temp|grep"Nmapscan"|awk'{print egrep"((25[0-5]|2[0-4][0-9]|((1[0-9]{2})|([1-9]?[0-9])))\.){3}(25[0-5]|2[0-4][0-ls|egrep"((25[0-5]|2[0-4][0-9]|((1[0-9]{2})|([1-9]?[0-9])))\.){3}(25[0-5]|2[0- |net|org|cn)$"ls|egrep"^(([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9]function{$1for((i=0;i<=$1;i++))for((s=0;s<=$2;s++))if[$[$i%2]==0]if[$[$s%2]==0]

echo-en"echo-en"\e[;44m

if[$[$s%2]==0

echo-en"\e[;42mecho-en"}ary_go25w|awk'NR>=3{printf$1"\t"$2"\t"$3"\n"}'>/tmp/who.txtforiin$(awk'{printf$1}'/tmp/bai.txt)k=$(egrep-v"$i"/tmp/who.txt|awk'{printf$2}"\n"'|awk'{printfforjin$kpkill-9-t61-88 ##CopyrightThetestscript2021Allrights color()SETCOLOR_SUCCESS="echo-en\\033[1;32m"SETCOLOR_FAILURE="echo-en\\033[1;31m"SETCOLOR_WARNING="echo-en\\033[1;33m"SETCOLOR_NORMAL="echo-en\E[0m"echo-n"$2"&&$MOVE_TO_COLecho-n"["if[$1="success"-o$1="0"]echo-n$"OKelif[$1="failure"-o$1="1"]echo-necho-necho-n"]"}os_typeawk-F'["]''/^NAME/{print$2}'/etc/os-}check()-e$db_dir-oe$install_dir/mongodb&&color1"MongoDB數(shù)據(jù)庫(kù)已安裝";exit;}if[`os_type`="CentOS"rpm-qcurl&>/dev/null||yuminstall-y-qcurlelif[`os_type`="Ubuntu"];thendpkg-lcurl&>/dev/null||apt-yinstall}

color1file_prepare()if[!-e$filecurl-O$url||{color1"MongoDB數(shù)據(jù)庫(kù)文件失敗";exit;}install_mongodb()tarxf$file-C$install_dirmkdir-p$db_dirln-s$install_dir/mongodb-linux-x86_64-*echoPATH=$install_dir/mongodb/bin/:'$PATH'>.$db_dir/mongod.log--$?-eq0&&color0"MongoDB數(shù)據(jù)庫(kù)安裝成功||color1"MongoDB數(shù)據(jù)庫(kù)安裝}xorg-x11-fonts-Type1firefox[root@centos 檢測(cè)兩臺(tái)服務(wù)器指 下的文件致#通過對(duì)比兩臺(tái)服務(wù)器上文件的md5值,達(dá)到檢測(cè)致性的目的 find$dir-typef|xargsmd5sum>ssh$b_ip"find$dir-typef|xargsmd5sum>/tmp/md5_b.txt"scp$b_ip:/tmp/md5_b.txt/tmp 比forfin`awk'{print2}ifgrep-qw"$f"/tmp/md5_b.txtmd5_a=`grepw$f"/tmp/md5_a.txt|awk'{print1}'`md5_b=`grepw$f"/tmp/md5_b.txt|awk'{print1}'`#當(dāng)文件存在時(shí),如果md5值不致則輸出文件改變的結(jié)果if[$md5_a!=$md5_b]thenecho"$fchanged." logfile=/tmp/`date+%H-%F`.logn=`date+%H`if[$n-eq00]||[$n-eq12] foriin`find/data/log/-typef`foriin`find/data/log/-typef`du-sh$i>>$logfile#檢測(cè)網(wǎng)卡流量,并按規(guī)定格式記錄在日志中##2019-08-12#ens33input:1234bps#ens33output:whilelogfile=/tmp/`date exec>>$logfiledate+"%Fsar-nDEV159|grepAverage|grepens33|awkecho#使用awk只輸出文檔行數(shù)(截取第段)n=`wcla.txt|awkprint$1}'`#foriin`seq1n_n=`echo$line|seds'/[^0-9]//'g|wc-L`echo# psaux|grep指定進(jìn)程名|grepvgrep|awk'{print$2}'|xargskillif[$#-ne1];echo"Usage:$0dir=$(dirname$1)ftpnv nopen0ftp服務(wù)器useradminpassword cd$dirwhile[$COUNT-le5];if$INT=~^[0-9]+$thenecho"輸入必須是整數(shù)!"exitelif$INTgt100thenecho"輸入必須是100以內(nèi)!"exit1[$MIN-lt$INT]&&MIN=$INT[$MAX-gt$INT]&&MAX=$INTletCOUNT++echo"SUM:$SUM"echo"MIN:$MIN"echo"MAX:$MAX#!/bin/bash##!/bin/bash# 生成個(gè)100以內(nèi)的隨機(jī)數(shù),提示用戶猜數(shù)字,根據(jù)用戶的輸入,提示用戶猜對(duì)了,#猜小了或猜大了,直至用戶猜對(duì) #RANDOM為系統(tǒng)自帶的系統(tǒng)變量,值為0-32767#使用read提示用戶猜數(shù)字#使用if判斷用戶猜數(shù)字的大小關(guān)系:-eq(等于),-ne(不等于),-gt(大于),-ge(大于等于),#-lt(小于),-le(小于等于)readp計(jì)算機(jī)生成了個(gè)1-100的隨機(jī)數(shù),你猜if[$cai-eq$numelif[$cai-gt$num]echo"Oops,猜大了echo"Oops,猜小了此需要編寫,一旦出現(xiàn)502,則自動(dòng)重啟php-fpm服務(wù)。 死循環(huán),每10秒檢測(cè) 次,10秒的日志條數(shù)為300條,出現(xiàn)502的比例不低于10%(30條)則需要重#3.重啟命令為:/etc/init.d/php-fpmrestart#監(jiān)測(cè) 日志502情況,并做相應(yīng)動(dòng)N=30#設(shè)定閾值while err=`tail-n300$log|grep-c'502"'`if[$err-ge$N]/etc/init.d/php-fpmrestart2>/dev/null sleepforforiin$(echo"456");doevala$i=$idoneecho$a4$a5foriin$(evalecho$*);do letnum+=1echo$node1$node212方法3:arr=(456)INDEX3=$(echo##touch#lsarticle_1.htmlarticle_2.htmlforforfilein$(ls*html);domv$file#mv$file$(echo$file|sed-r's/.*(_.*)/bbs\1/')#mv$file$(echo$file|echobbs_$(cut-d_-f2)forforfilein$(find.-maxdepth1-name"*html");domv$filebbs_${file#*_}done##renamearticlebbs 第第2行 sed-n'1,5'p2.txt|sed'/[a-zA-sed-n'6,10'p2.txt|seds'/[a-zA-Z]//'gsed-n'11,$'p2.txt ##find.-name"*.html"-execdu-k{}\;|awk'{sum+=$1}END{print方法forsizein$(ls-l*.html|awk'{print$5}');doPORT="222580forPORTin$PORT;ifecho&>/dev/null>/dev/tcp/$HOST/$PORT;thenecho"$PORTopen"echo"$PORT用 #Bashalsointerpretsanumberofmulti-characteroptions. forsinBashalsointerpretsanumberofmulti-characteroptions.if[$n-lt6]echo"*cmd*1-date2-ls3-who4-pwd0-exit"while:read-p"pleaseinputnumber:"nn1=`echo$n|seds'/[0-9]//'g`if[-z"$n"]if[-n"$n1"]exit0case$ninecho"pleaseinputnumberis[1-4]"Q:103、Expect實(shí)現(xiàn)SSHexpect<<EOFsettimeout30spawnssh expect"(yes/no)""yes\r"; "password:"{send}expect"$USER@*"{send"$1\r"}expect"$USER@*"{send"exit\r"}expecteofexpect-cexpect{\"(yes/no)\"{send\"yes\r\";\"password:\"{send\"$PASS\r\";\"$USER@*\"{send\"df-h\rexit\r\"; #catlogin.expsetip[lindex$argv0]setuser[lindex$argv1]setpasswd[lindex$argv2]setcmd[lindex$argv3]if{$argc!=4}{puts"Usage:e

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論