今天給大家整理一些實(shí)用的 Linux Shell 腳本案例
希望可以幫到大家,讓大家更熟悉 shell 編程
1.顯示系統(tǒng)一些基本信息
顯示信息如下:
系統(tǒng)版本
系統(tǒng)內(nèi)核
虛擬平臺(tái)
主機(jī)名
ip地址
開機(jī)信息有沒有報(bào)錯(cuò),有的話輸出到屏幕
可以將該腳本加入到開機(jī)自啟動(dòng)里面,這樣開機(jī)就會(huì)輸出基本信息
#!/bin/bash
info(){
system=$(hostnamectl|grepSystem|awk'{print$3}')
kernel_release=$(hostnamectl|grepKernel|awk-F:'{print$2}')
Virtualization=$(hostnamectl|grepVirtualization|awk'{print$2}')
server_name=$(hostname)
ipaddr=$(hostname-I)
echo"當(dāng)前系統(tǒng)版本是:${system}"
echo"當(dāng)前系統(tǒng)內(nèi)核是:${kernel_release}"
echo"當(dāng)前虛擬平臺(tái)是:${Virtualization}"
echo"當(dāng)前主機(jī)名是:${server_name}"
echo"當(dāng)前ip地址:${ipaddr}"
}
checkerrror(){
error_info=$(dmesg|greperror)
if[-e${error_info}]
then
echo"無錯(cuò)誤日志!"
else
ehcho${error_info}
fi
}
info
checkerrror

2.關(guān)閉系統(tǒng)防火墻和SELinux
檢查防火墻狀態(tài),是否安裝防火墻,如果安裝則關(guān)閉 關(guān)閉SELinux 清空iptables規(guī)則
#!/bin/bash
close_firewalld(){
code=$(systemctlstatusfirewalld)
if[${code}-eq0]
then
systemctlstopfirewalld
fi
}
close_selinux(){
sed-i'/^SELINUX/s/=.*/=disabled/'/etc/selinux/config
setenforce0
}
close_iptables(){
iptables-F
serviceiptablessave
serviceiptablesrestart
}
close_firewalld
close_selinux
close_iptables
3.定時(shí)任務(wù)計(jì)劃:歸檔備份
打包壓縮/var/log/nginx目錄下所有內(nèi)容,存放在/tmp/nginx目錄里
壓縮文件命名規(guī)范:yymmdd_logs.tar.gz,只保存七天內(nèi)的文件,超過七天的文件會(huì)進(jìn)行清理
#!bin/bash date="$(date+%Y%m%d)" dir='/tmp/nginx' backupfile='yymmdd_logs.tar.gz' #查看/tmp/nginx是否存在,不存在則創(chuàng)建 checkbak(){ if[!-e${dir}] then mkdir${dir} fi } #壓縮文件 backup(){ tar-zcvf${dir}/${backupfile}/var/log/nginx/>/dev/null2>&1 echo"${backupfile}Compressed and packaged successfully !" } #清除七天過期文件 cleanup(){ find${dir}-typef-mtime+7|xagrsrm-rf if[$?-eq0] then echo"Cleaned up successfully!" else echo"datacleaningfailederror,pleasepayattentionintime" fi } checkbak backup cleanup
4.自動(dòng)批量創(chuàng)建用戶
批量創(chuàng)建user1、user2、user3.....
#!/bin/bash
#檢查用戶是否存在,不存在則創(chuàng)建
checkuser(){
foriin$(seq120)
do
iduser${i}>/dev/null2>&1
if[$?-eq0]
then
echo"user${i}已存在!"
else
useradduser${i}&&echo"user${i}"|passwd--stdinuser${i}>/dev/null2>&1
fi
done
}
checkuser
5.通過位置參數(shù)創(chuàng)建用戶
$1 是執(zhí)行腳本的第一個(gè)參數(shù)
$2 是執(zhí)行腳本的第二個(gè)參數(shù)
#!/bin/bash
checkuser(){
id${1}>/dev/null2>&1
if[$?-eq0]
then
echo"${1}已存在!"
else
useradd"$1"
echo"$2"|passwd‐‐stdin"$1"
fi
}
6.批量刪除用戶
批量刪除user1...user20
#!/bin/bash
#檢查用戶是否存在,存在則刪除
checkuser(){
foriin$(seq120)
do
iduser${i}>/dev/null2>&1
if[$?-eq0]
then
userdel-ruser${i}
else
echo"user${i}不存在!"
fi
done
}
checkuser
7.更新系統(tǒng)時(shí)間,并寫入硬件時(shí)間里
查看是否安裝ntpdate工具
創(chuàng)建上海時(shí)區(qū)文件的軟鏈接
更新時(shí)間并寫入到硬件時(shí)間里
#!/bin/bash
package="ntpdate"
info=$(rpm-q${package})
check_pkgs(){
if[!-e${info}]
then
echo"ntpdate already exists!"
else
echo"start installation!"
yumcleanall>/dev/null2>&1
fi
yumupdate-y&&yuminstall-y${package}>/dev/null2>&1
fi
}
modify_time(){
echo"開始修改時(shí)間"
rm-rf/etc/localtime&&ln-s/usr/share/zoneinfo/Asia/Shanghai/etc/localtime
/usr/sbin/ntpdatecn.pool.ntp.org>/dev/null2>&1&&hwclock-w
}
check_pkgs
modify_time
8.檢查服務(wù)運(yùn)行狀態(tài)
檢查某一服務(wù)是否正常運(yùn)行,執(zhí)行腳本的時(shí)候第一個(gè)參數(shù)為服務(wù)名
#!/bin/bash result=$(pidof$1|wc-l) echo${result} if[${result}-eq0] then echo"service does not exist !" else echo"Service is running normally !" fi

9.對目標(biāo)主機(jī)進(jìn)行心跳檢測
ping目標(biāo)主機(jī)看是否ping得通,三次ping通表示主機(jī)正常運(yùn)行
將目標(biāo)主機(jī)的ip地址作為第一個(gè)參數(shù)傳進(jìn)去
#!/bin/bash
ipaddr=$1
echo${ipaddr}
ping_status(){
ifping-c1${ipaddr}>/dev/null2>&1
then
echo"ping${ipaddr}issuccessful!"
continue
fi
}
foriin$(seq13)
do
ping_status
echo"ping${ipaddr}isfailure!"
done

進(jìn)階版:對ip地址池里的主機(jī)分別進(jìn)行心跳檢測
ipaddr=(192.168.149.131192.168.149.130192.168.149.132192.168.149.133)
foriin${ipaddr[*]}
do
echo"....begintoping${i}....."
ifping-c3${i}>/dev/null2>&1
then
echo"ping${i}issuccessful!"
else
echo"ping${i}isfailure!"
fi
done

10.系統(tǒng)磁盤內(nèi)存容量告警
根分區(qū)剩余空間小于20%(即使用空間大于80%) 輸出告警信息 內(nèi)存使用空間大于80% 輸出告警信息 配合crond每5分鐘檢查一次
#!/bin/bash disk_letfspace=$(df-Th|grep-w/|awk'{print$6}'|cut-d%-f1) mem_used=$(free-m|grepMem|awk'{print$3}') mem_total=$(free-m|grepMem|awk'{print$2}') mem_letfspace=$[${mem_used}*100/${mem_total}] if[${disk_letfspace}-gt80] then echo"Diskfreespaceislessthan20%!" else echo"${disk_letfspace}%ofdiskspaceleft" fi if[${mem_letfspace}-gt80] then echo"memoryspaceislessthan20%!" else echo"${mem_letfspace}%ofmemoryspaceleft" fi
crontab-l */5****/root/check_space.sh

審核編輯:劉清
-
Linux
+關(guān)注
關(guān)注
88文章
11579瀏覽量
217076 -
磁盤
+關(guān)注
關(guān)注
1文章
394瀏覽量
26215 -
Shell
+關(guān)注
關(guān)注
1文章
373瀏覽量
25000
原文標(biāo)題:【干貨】10個(gè)實(shí)用 Linux Shell 腳本案例
文章出處:【微信號(hào):釋然IT雜談,微信公眾號(hào):釋然IT雜談】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評(píng)論請先 登錄
如何使用Shell腳本在 Linux 服務(wù)器尋找攻擊證據(jù)
一步一步了解并搞懂shell腳本
Linux中常用的Shell腳本示例
如何利用Shell腳本掩藏Linux服務(wù)器使用痕跡
如何創(chuàng)建和執(zhí)行一個(gè)簡單的Linux shell腳本
34個(gè)Linux Shell腳本匯總
通過Shell腳本掩蓋Linux系統(tǒng)上的操作痕跡
使用Shell腳本掩蓋Linux上的操作痕跡
Linux Shell腳本經(jīng)典案例分享

整理一些實(shí)用的Linux Shell腳本案例
評(píng)論