測(cè)試 192.168.4.0/24 整個(gè)網(wǎng)段中哪些主機(jī)處于開機(jī)狀態(tài),哪些主機(jī)處于關(guān)機(jī) (for )
#!/bin/bash
for i in {1..254}
do
ping ‐c2 ‐i0.3 ‐W1 192.168.4.$i &>/dev/null
if [ $? –eq 0 ];then
echo "192.168.4.$i is up"
else
echo "192.168.4.$i is down"
fi
done
測(cè)試 192.168.4.0/24 整個(gè)網(wǎng)段中哪些主機(jī)處于開機(jī)狀態(tài),哪些主機(jī)處于關(guān)機(jī) (while )
#!/bin/bash
i=1
while [ $i ‐le 254 ]
do
ping ‐c2 ‐i0.3 ‐W1 192.168.4.$i &>/dev/null
if [ $? –eq 0 ];then
echo "192.168.4.$i is up"
else
echo "192.168.4.$i is down"
fi
let i++
done
測(cè)試 192.168.4.0/24 整個(gè)網(wǎng)段中哪些主機(jī)處于開機(jī)狀態(tài),哪些主機(jī)處于關(guān)機(jī) (多進(jìn)程)
#!/bin/bash
#定義一個(gè)函數(shù),ping 某一臺(tái)主機(jī),并檢測(cè)主機(jī)的存活狀態(tài)
myping(){
ping ‐c2 ‐i0.3 ‐W1 $1 &>/dev/null
if [ $? ‐eq 0 ];then
echo "$1 is up"
else
echo "$1 is down"
fi
}
for i in {1..254}
do
myping 192.168.4.$i &
done
#使用&符號(hào),將執(zhí)行的函數(shù)放入后臺(tái)執(zhí)行
#這樣做的好處是不需要等待 ping 第一臺(tái)主機(jī)的回應(yīng),就可以繼續(xù)并發(fā) ping 第二臺(tái)主機(jī),依次類推。
進(jìn)度條
#!/bin/bash
jindu(){
while :
do
echo ‐n '#'
sleep 0.2
done
}
jindu &
cp ‐a $1 $2
killall $!
echo "拷貝完成''
紅色進(jìn)度條
#!/bin/bash
declare -a ary
for i in `seq 0 20`
do
ary[$i]=" "
echo -en "\e[41;5m ${ary[@]}\e[;0m"
sleep 1
done
進(jìn)度條,動(dòng)態(tài)時(shí)針版本
#!/bin/bash
#定義一個(gè)顯示進(jìn)度的函數(shù),屏幕快速顯示| / ‐ \
rotate_line(){
INTERVAL=0.1 #設(shè)置間隔時(shí)間
COUNT="0" #設(shè)置 4 個(gè)形狀的編號(hào),默認(rèn)編號(hào)為 0(不代表任何圖像)
while :
do
COUNT=`expr $COUNT + 1` #執(zhí)行循環(huán),COUNT 每次循環(huán)加 1,(分別代表 4 中不同的形狀)
case $COUNT in #判斷 COUNT 的值,值不一樣顯示的形狀就不一樣
"1") #值為 1 顯示‐
echo ‐e '‐'"\b\c"
sleep $INTERVAL
;;
"2") #值為 2 顯示\\,第一個(gè)\是轉(zhuǎn)義
echo ‐e '\\'"\b\c"
sleep $INTERVAL
;;
"3") #值為 3 顯示|
echo ‐e "|\b\c"
sleep $INTERVAL
;;
"4") #值為 4 顯示/
echo ‐e "/\b\c"
sleep $INTERVAL
;;
*) #值為其他時(shí),將 COUNT 重置為 0
COUNT="0";;
esac
done
}
rotate_line
使用死循環(huán)實(shí)時(shí)顯示 eth0 網(wǎng)卡發(fā)送的數(shù)據(jù)包流量
#!/bin/bash
while :
do
echo '本地網(wǎng)卡 eth0 流量信息如下: '
ifconfig eth0 | grep "RX pack" | awk '{print $5}'
ifconfig eth0 | grep "TX pack" | awk '{print $5}'
sleep 1
done
使用 user.txt 文件中的人員名單,在計(jì)算機(jī)中自動(dòng)創(chuàng)建對(duì)應(yīng)的賬戶并配置初始密碼
#!/bin/bash
#本腳本執(zhí)行,需要提前準(zhǔn)備一個(gè) user.txt 文件,該文件中包含有若干用戶名信息
for i in `cat user.txt`
do
useradd $i
echo "123456" | passwd ‐‐stdin $i
done
編寫批量修改擴(kuò)展名腳本,如批量將 txt 文件修改為 doc 文件
#!/bin/bash
#執(zhí)行腳本時(shí),需要給腳本添加位置參數(shù)
#腳本名 txt doc(可以將 txt 的擴(kuò)展名修改為 doc) #腳本名 doc jpg(可以將 doc 的擴(kuò)展名修改為 jpg)
for i in "ls *.$1"
do
mv $i ${i%.*}.$2
done
使用 expect 工具自動(dòng)交互密碼遠(yuǎn)程其他主機(jī)安裝 httpd 軟件?
#!/bin/bash
#刪除~/.ssh/known_hosts 后,ssh 遠(yuǎn)程任何主機(jī)都會(huì)詢問是否確認(rèn)要連接該主機(jī)
rm ‐rf ~/.ssh/known_hosts
expect <<EOF
spawn ssh 192.168.4.254
expect "yes/no" {send "yes\r"}
#根據(jù)自己的實(shí)際情況將密碼修改為真實(shí)的密碼字串
expect "password" {send "密碼\r"}
expect "#" {send "yum ‐y install httpd\r"}
expect "#" {send "exit\r"}
EOF
輪詢檢測(cè)Apache狀態(tài)并啟用釘釘報(bào)警
#!/bin/bash
shell_user="root"
shell_domain="apache"
shell_list="/root/ip_list"
shell_row=`cat $shell_list |wc -l`
function trans_text(){
text=$1
curl 'https://oapi.dingtalk.com/robot/send?access_token=b4fcf5862088a1bc7f2bf66a' -H'Content-Type: application/json' -d'{ #指定釘釘機(jī)器人hook地址
"msgtype": "text",
"text": {
"content": "'"$text"'"
},
}'
}
function apache_check_80(){
ip=$1
URL="http://$ip/index.html"
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`
if [ $HTTP_CODE != 200 ]
then
trans_text "
=================================================================
\n $ip Apache 服務(wù)器狀態(tài)異常,網(wǎng)頁返回碼: '"$HTTP_CODE"' 請(qǐng)及時(shí)處理 ! \n
================================================================= \n"
fi
}
while true
do
shell_list="/root/ip_list"
shell_row=`cat $shell_list |wc -l`
for temp in `seq 1 $shell_row`
do
Ip_Addr=`cat $shell_list |head -n $temp |tail -n 1`
apache_check_80 $Ip_Addr
done
sleep 10
done
一臺(tái)監(jiān)控主機(jī),一臺(tái)被監(jiān)控主機(jī)。被監(jiān)控主機(jī)分區(qū)使用率大于80%,就發(fā)告警郵件。放到crontab里面,每10分鐘執(zhí)行一次
#!/bin/bash
FSMAX="80"
remote_user='root'
remote_ip=(IP地址列表)
ip_num='0'
while [ "$ip_num" -le "$(expr ${#remote_ip[@]} -l)"]
do
read_num='1'
ssh "$remote_user"@"${remote_ip[$ip_num]}" df -h > /tmp/diskcheck_tmp
grep '^/dev/*' /tmp/diskcheck_tmp | awk '{print $5}'|sed 's/\%//g' > /tmp/diskcheck_num_tmp
while [ "$read_num" -le $(wc -l < /tmp/diskcheck_num_tmp) ]
do
size=$(sed -n "$read_num" 'p' /tmp/diskcheck_num_tmp)
if [ "size" -gt "$FSMAX" ]
then
$(grep '^/dev/*' /tmp/diskcheck_tmp |sed -n $read_num'p' > /tmp/disk_check_mail)
$(echo ${remote_ip[$ip_num]}) >> /tmp/disk_check_mail)
$(mail -s "diskcheck_alert" admin < /tmp/disk_check_mail)
fi
read_num=$(expr $read_num + 1)
done
ip_num=$(expr $ip_num + 1)
done
自動(dòng)ftp上傳
#! /bin/bash
ftp -n << END_FTP
open 192.168.1.22
user test testing //用戶名test 密碼:testing
binary
prompt off //關(guān)閉提示
mput files //上傳files文件
close
bye
END_FTP
mysqlbak.sh備份數(shù)據(jù)庫(kù)目錄腳本
#!/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
expect實(shí)現(xiàn)遠(yuǎn)程登陸自動(dòng)交互
#!/usr/bin/expect -f
set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set timeout 30
spawn ssh-copy-id root@$ipaddress
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
#expect "*from*"
#send "mkdir -p ./tmp/testfile\r"
#send "exit\r"
#expect "#" #i# 命令運(yùn)行完, 你要期待一個(gè)結(jié)果, 結(jié)果就是返回shell提示符了(是# 或者$)
http心跳檢測(cè)
#!/bin/bash
function MyInstall
{
if ! rpm -qa |grep -q "^$1"
then
yum install $1
if [ $? -eq 0 ]
then
echo -e "$i install is ok\n"
else
echo -e "$1 install no\n"
fi
else
echo -e "yi an zhuang ! \n"
fi
}
for ins in mysql php httpd
do
MyInstall $ins
done
shell實(shí)現(xiàn)插入排序
#!/bin/bash
declare -a array
for i in `seq 1 10`
do
array[$i]=$RANDOM
done
echo -e "Array_1: ${array[@]}"
for (( x=1;x<=9;x++ ))
do
for(( y=1;y<=9;y++ ))
do
if [ ${array[$y]} -gt ${array[$y+1]} ]
then
temp=${array[$y]}
array[$y]=${array[$y+1]}
array[$y+1]=$temp
fi
done
done
echo -e "Array_2: ${array[@]}"
bash實(shí)現(xiàn)動(dòng)態(tài)進(jìn)度條
#!/bin/bash
i=0
bar=''
index=0
arr=( "|" "/" "-" "\\" )
while [ $i -le 100 ]
do
let index=index%4
printf "[%-100s][%d%%][\e[43;46;1m%c\e[0m]\r" "$bar" "$i" "${arr[$index]}"
let i++
let index++
usleep 30000
bar+='#'
clear
done
printf "\n"
根據(jù)文件內(nèi)容創(chuàng)建賬號(hào)
#!/bin/bash
for Uname in `cat /root/useradd.txt |gawk '{print $1}'`
do
id $Uname &> /dev/null
if [ $? -eq 0 ]
then
echo -e "這個(gè)賬號(hào)已存在!來源:微信公眾號(hào)【網(wǎng)絡(luò)技術(shù)干貨圈】"
continue
fi
for Upasswd in `cat /root/useradd.txt |gawk '{print $2}'`
do
useradd $Uname &> /dev/null
echo "$Upasswd" |passwd --stdin $Uname &> /dev/null
if [ $? -eq 0 ]
then
echo -e "賬號(hào)創(chuàng)建成功!"
else
echo -e "創(chuàng)建失敗!"
fi
done
done
監(jiān)控主機(jī)的磁盤空間,當(dāng)使用空間超過90%就通過發(fā)mail來發(fā)警告
#!/bin/bash
#monitor available disk space
#提取本服務(wù)器的IP地址信息
IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "`
SPACE=` df -hP | awk '{print int($5)}'`
if [ $SPACE -ge 90 ]
then
echo "$IP 服務(wù)器 磁盤空間 使用率已經(jīng)超過90%,請(qǐng)及時(shí)處理。"|mail -s "$IP 服務(wù)器硬盤告警,
公眾號(hào):Geek安全" fty89@163.com
fi
監(jiān)控服務(wù)器網(wǎng)卡流量
#!/bin/bash
#network
#Mike.Xu
while : ; do
speedtime='date +%m"-"%d" "%k":"%M'
speedday='date +%m"-"%d'
speedrx_before='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-'
speedtx_before='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-'
sleep 2
speedrx_after='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-'
speedtx_after='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-'
speedrx_result=$[(speedrx_after-speedrx_before)/256]
speedtx_result=$[(speedtx_after-speedtx_before)/256]
echo"$speedday$speedtime Now_In_Speed: "$speedrx_result"kbps Now_OUt_Speed: "$speedtx_result"kbps"
sleep 2
done
檢測(cè)CPU剩余百分比
#!/bin/bash
#Inspect CPU
#Sun Jul 31 17:25:41 CST 2016
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin
export PATH
TERM=linux
export TERM
CpuResult=$(top -bn 1 | grep "Cpu" | awk '{print $5}' | sed 's/\..*$//g')
if [[ $CpuResult < 20 ]];then
echo "CPU WARNING : $CpuResult" > /service/script/.cpu_in.txt
top -bn 1 >> /service/script./cpu_in.txt
mail -s "Inspcet CPU" wl < /service/script/.cpu_in.txt
fi
檢測(cè)磁盤剩余空間
#!/bin/bash
#Insepct Harddisk , If the remaining space is more than 80%, the message is sent to the wl
#Tue Aug 2 09:45:56 CST 2016
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin
export PATH
for RemainingSpace in $(df -h | awk '{print $5}' | grep -v 'Use' | sed -e 's/[%]//g')
do
if [[ $RemainingSpace > 80 ]];then
echo -e "$RemainingSpace"
echo -e "$(df -h | grep $RemainingSpace)" > /service/script/.HarddiskWarning
mail -s "disk Warning" wl < /service/script/.HarddiskWarning
fi
done
判斷哪些用戶登陸了系統(tǒng)
#!/bin/bash
declare -i count=0
while true;do
if who |grep -q -E "^wang"
then
echo -e "用戶wang 登陸了系統(tǒng)\n 這是第$count 次!威信公眾浩:wljsghq"
break
else
let count++
fi
sleep 3
done
~
示例:找出UID為偶數(shù)的所有用戶,顯示其用戶名和ID號(hào);
#!/bin/bash
while read line; do
userid=$(echo $line | cut -d: -f3)
if [ $[$userid%2] -eq 0 ]; then
echo $line | cut -d: -f1,3
fi
done < /etc/passwd
批量掃面存活
#!/bin/bash
#By:lyshark
#nmap 192.168.22.0/24>ip
MAC=`cat ip |awk '$1 == "MAC" && $NF == "(VMware)"{print $3}'`
for i in `seq 1 20`
do
temp=`echo ${MAC[@]} |awk '{print $i}'`
IP=`cat /ip |grep -B5 $temp |grep "Nmap scan"|awk '{print $5}'`
echo $IP |awk '{print $1}'
done
正則匹配IP
^[0-9]{0,2}|^1[0-9]{0,2}|^2[0-5]{0,2}
egrep "(^[0-9]{1,2}|^1[0-9]{0,2}|^2[0-5]{0,2})\.([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})\.([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})\.([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})$"
([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})
([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})
([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})
([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})
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-9]|((1[0-9]{2})|([1-9]?[0-9])))"
ls |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-9]|((1[0-9]{2})|([1-9]?[0-9])$))"
正則匹配郵箱
egrep "^[0-9a-zA-Z][0-9a-zA-Z_]{1,16}[0-9a-zA-Z]\@[0-9a-zA-Z-]*([0-9a-zA-Z])?\.(com|com.cn|net|org|cn)$" rui
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][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$"
