運維常用shell指令碼

2021-09-25 16:17:44 字數 3121 閱讀 5522

開頭加直譯器"#/bin/bash"

語法縮排,開頭用四個空格;多加注釋說明。

命名規則:變數名大寫、區域性變數小寫、函式名小寫、名字能夠體現實際作用。

預設變數是全域性的,在函式中變數local指定為區域性變數,避免汙染其他作用域。

指令碼寫完後一定要先除錯再線上使用。

# echo $random|md5sum|cut -c 1-8

2d29dd8d

# openssl rand -base64 4

2et/fa==

# cat /proc/sys/kernel/random/uuid|cut -c 1-8

7ccdc570

# cat /proc/sys/kernel/random/uuid|cksum|cut -c 1-8

37069808

openssl rand -base64 4|cksum|cut -c 1-8

20272150

# echo $random|md5sum|cksum |cut -c 1-8

13997037

# date +%n|cut -c 1-8

76635150

#bin/bash

function echo_color()

echo_color

#bin/bash

for user in us_er

do pass=`echo $random|md5sum|cut -c 1-8`

useradd $user

echo "$pass"|passwd --stdin $user

echo -e "$user:$pass" >> user_list

echo "$user create successful!"

done

#!/bin/bash

if rpm -q sysstat &>/dev/null; then

echo "sysstat is already installed."

else

echo "sysstat is not installed!"

fi

#!/bin/bash

port_c=$(ss -anu |grep -c 123)

ps_c=$(ps -ef |grep ntpd |grep -vc grep)

if [ $port_c -eq 0 -o $ps_c -eq 0 ]; then

echo "內容" | mail -s "主題" 郵箱.com

fi

#!/bin/bash  

ip_list="10 20 30 40"

for ip in $ip_list; do

num=1

while [ $num -le 3 ]; do

if ping -c 1 192.168.1.$ip > /dev/null; then

echo "192.168.1.$ip ping is successful."

break

else

# echo "$ip ping is failure $num"

fail_count[$num]=192.168.1.$ip

let num++

fidone

if [ $ -eq 3 ];then

echo "$ ping is failure!"

unset fail_count[*]

fidone

#!/bin/bash

date=$(date +%f" "%h:%m)

ip=$( ifconfig br0|awk -f' ' 'nr==2')

#ip=$(ifconfig eth0 |awk -f ' :+' '/inet addr/' ) # 只支援centos6

mail="[email protected]"

if ! which vmstat &>/dev/null; then

echo "vmstat command no found, please install procps package."

exit 1

fius=$(vmstat |awk 'nr==3' )

sy=$(vmstat |awk 'nr==3 ')

idle=$(vmstat |awk 'nr==3')

wait=$(vmstat |awk 'nr==3' )

use=$(($us+$sy))

if [ $use -ge 50 ]; then

echo "

date: $date

host: $ip

problem: cpu utilization $use" | mail -s "cpu monitor" $mail

fi

#!/bin/bash

date=$(date +%f" "%h:%m)

ip=$( ifconfig br0|awk -f' ' 'nr==2' )

#ip=$(ifconfig eth0 |awk -f [ :]+ /inet addr/ )

mail="[email protected]"

total=$(free -m |awk '/mem/' )

use=$(free -m |awk '/mem/' )

free=$(($total-$use))

# 記憶體小於1g傳送報警郵件

if [ $free -lt 1024 ]; then

echo "

date: $date

host: $ip

problem: total=$total,use=$use,free=$free

" | mail -s "memory monitor" $mail

fi

運維常用shell

列出你最常用的10條shell history awk end sort rn head history awk end sort rn head grep v bash history awk end head 網路連線數目 netstat an grep e tcp cut c 68 sort ...

運維 我常用的shell指令碼彙總

某天發現 home分割槽滿了,想知道是哪個目錄佔了大頭,使用該指令碼可以幫你完成排序 du max depth 1 home sort n r max depth 1只統計一級目錄 sort n r按照數字 逆序排序 ps ef egrep foo bar grep v grep awk xargs...

使用shell指令碼輔助運維工作

儲存到 etc ethers檔案中,若此檔案已存在,則先轉移進行備份。每行一條記錄,第1列為ip位址,第2列為對應的mac位址。2 編寫乙個scanhost.sh的掃瞄指令碼,檢查有哪些主機開啟了ftp服務,掃瞄物件是 etc ethers中所有的ip位址。指令碼如下 root localhost ...