腳本實作,探測C類、B類、A類網絡中的所有主機是否線上;
cping() {
local i=1
while [ $i -le 245 ];do
if ping -W 1 -c 1 $1.$i &>/dev/null;then
echo "$1.$i is up"
else
echo "$1.$i is down"
fi
let i++
done
}
#cping 192.168.0
bping() {
local j=0
while [ $j -le 255 ];do
cping $1.$j
let j++
done
}
#bping 172.16
aping() {
local x=0
while [ $x -le 255 ];do
bping $1.$x
let x++
done
}
#aping 10
編寫腳本/root/bin/hostping.sh,接受一個主機的IPv4位址做為參數,測試是否可連通。如果能ping通,則提示使用者“該IP位址可通路”;如果不可ping通,則提示使用者“該IP位址不可通路”
[root@centos7 script35]# vim hostping.sh
#!/bin/bash
[[ "$1" =~ ^(([0-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]] || { echo "please input a IP address" ; exit; }
ping -c1 -w1 $1 &> /dev/null && echo the host is up || echo the host is down
編寫腳本/root/bin/checkdisk.sh,檢查磁盤分區空間和inode使用率,如果超過80%,就發廣播警告空間将滿
A、[root@centos7 script35]# vim checkdisk.sh
$N=80
DISK=df |egrep "^/dev/sd" | tr -s " " "%" | cut -d% -f5 |sort -nr |head -1
INODE="df -i" | egrep "^/dev/sd" | tr -s " " "%" | cut -d% -f5 | sort -rn |head -1
[[ "$DISK" -gt "$N" ]] && echo "clean up the disk"
[[ "$INODE" -gt "$N" ]] && echo "please clean the file"
B、[root@centos7 script35]# vim checkdisk.sh
WARNING=80
df |grep "^/dev/sd" |while read LINE;do
USED=
echo $LINE | sed -rn 's/^([^[:space"]]+).*([0-9]+)%.*/\2/p'
PART=
echo $LINE | sed =rn 's/^([^[:space"]]+).*([0-9]+)%.*/\1/p'
if [ $USED -ge $WARNING ];then
echo "$PART will be full, used : $USED"
fi
done
1、建立使用者的腳本:(标準版本)
[ ! -f "$1" ] && echo "$1 file is not exit ! " && exit
STUDEN_FILE = $ 1
for USER in
cat $STUDEN_FILE
;do
if id ${USER} &> /dev/null;then
echo " 使用者: ${USER}已存在,建立失敗 "
else
useradd ${USER}
echo ${USER} | passwd --stdin ${USER} &> /dev/null
echo "使用者:${USER}建立成功"
注:/dev/urandom 本檔案可生成随機字元
2、提示請輸入網址如172.20.119 判斷輸入的網段中主機線上狀态:
NET=172.20.119
START=1
END=254
for HOSTID in
seq $START $END
{
if ping -c1 -w1 ${NET}.${HOSTID} &> /dev/null;then
echo ${NET}.${HOSTID} is up
echo ${NET}.${HOSTID} >> hostlist。txt
echo ${NET}.${HOSTID} is down
}&
wait
3、判斷/var/目錄下所有檔案的類型
for i in $(find /var) ;do
if [ -b $i ];then
echo "$i是塊裝置"
elif [ -c $i ];then
echo "$i是字元裝置"
elif [ -f $i ];then
echo "$i是普通檔案"
elif [ -h $i ];then
echo "$i是符号連結檔案"
elif [ -p $i ];then
echo "$i是管道檔案"
elif [ -s $i ];then
echo "$i是 套接檔案"
elif [ -d $i ];then
echo "$i是目錄檔案"
echo “檔案或目錄不存在”
exit 0
4、目錄下分别有多個以K開頭和以S開頭的檔案;分别讀取每個檔案, 以K開頭的輸出為檔案加stop,以S開頭的輸出為檔案名加start。
for i in /etc/rc.d/rc3.d/k;do
echo $i stop
for i in /etc/rc.d/rc3.d/s;do
echo $i start
done
5、提示輸入正整數n的值,計算1+2+…+n的總和
read -p "please input a positive integer:" n
i=1
sum=0
for i in
seq 1 $n
let sum+=i
echo "sum is $sum"
6、計算100以内所有能被3整除的整數之和
for n in {1..100};do
if [ $[ $n%3 ] -eq 0 ];then
let sum+=n
echo "sum is $sum”
7、列印九九乘法表
for i in {1..9};do
for j in
seq 1 $i
result = $[ $j$i ]
echo -e "${j}x${i}=$result\t\c"
echo
8、在/testdir目錄下建立10個html檔案,檔案名格式為數字N(從1到10)加随機8個字 母,如: 1AbCdeFgH.html
if [ ! -d /testdir ];then
mkdir /testdir
a=1
while [ $a -le 10 ];do
touch /testdir/$a$(tr -cd [:alnum:] < /dev/random |head -c 8).html
a=$(echo $a+1|bc)
9、列印等腰三角形
read -p "請輸入需要生成的等腰三角形的行數:" zongline
for curline in $(seq 1 $zongline);do
spacenum=$[$zongline-$curline]
anum=$[2$curline-1]
for kongge in $(seq 1 $spacenum);do
echo -e "" "\c"
for a in $(seq 1 $anum);do
echo -e "a\c"
10、求100以内所有正奇數之和
n=1
while [ $n -le 100 ]
do
sum=$((sum+n))
n=$((n+2))
echo $sum
11、提示請輸入網絡位址,如192.168.0.0,判斷輸入的網段中主機 線上狀态,并統計線上和離線主機各多少
read -p "please input nework(eg:192.168.0.0): " network
netid=
echo $network|cut -d. -f1-3
hostid=1
up=0
down=0
while [ $hostid -le 254 ];do
if ping -c1 -w1 $netid.$hostid &>/dev/null;then
echo "the $netid.$hostid is up"
let up++
echo "the $netid.$hostid is down"
let down++
let hostid++
echo the up is $up
echo the down is $down
12、列印九九乘法表
while [ $i -le 9 ];do
j=1
while [ $j -le $i ];do
let k=ij
echo -en "$i$j=$k\t"
let j++
let i++
13、利用變量RANDOM生成10個随機數字,輸出這個10數字,并顯 示其中的最大值和最小值
x=0
while [ $x -le 9 ];do
if [ $x -le 9 ];then
echo $RANDOM |tee -a /tmp/suiji.txt
break
let x++
echo "max num is : $(sort -n /tmp/suiji.txt |tail -1)"
echo "min num is : $(sort -n /tmp/suiji.txt |head -1)"
14、實作列印國際象棋棋盤
for i in {1..8};do
for j in {1..8};do
if [ $[$i%2] -eq 1 ];then
if [ $[$j%2] -eq 1 ];then
echo -en "\033[47;37m \033[0m"
echo -en "\033[40;37m \033[0m"
15、後續六個字元串: efbaf275cd、 4be9c40b8b、 44b2395c46、 f8c8873ce0、 b902c16c8b、 ad865d2f63是通過對随機數變量RANDOM随機 執行指令: echo $RANDOM|md5sum|cut –c1-10 後的結果,請破解這些 字元串對應的RANDOM值
ps1=efbaf275cd
ps2=4be9c40b8b
ps3=44b2395c46
ps4=f8c8873ce0
ps5=b902c16c8b
ps6=ad865d2f63
echo 'num ' 'random'
time=1
for i in {0..32767}
if [ $time -gt 6 ];then
ps=$(echo $i | md5sum | cut -c1-10)
for j in $ps1 $ps2 $ps3 $ps4 $ps5 $ps6
if [ $ps == $j ];then
echo $i $ps
let time++
16、每隔3秒鐘到系統上擷取已經登入的使用者的資訊;如果發現使用者hacker登入, 則将登入時間和主機記錄于日志/var/log/login.log中,并退出腳本
until false;do
if who |grep "^hacker\>" &> /dev/null;then
who|grep "^hacker\>" > /var/log/login.log
sleep 3
17、随機生成10以内的數字,實作猜字遊戲,提示比較大或小,相等則退出
suiji=$[$RANDOM%10]
while [ $time -le 3 ];do
read -p "請輸入0-9的任意整數:" shuru
if [ $shuru -eq $suiji ];then
echo "猜中了"
elif [ $shuru -gt $suiji ];then
echo "請小一點"
echo "您還有$[3-$time]次機會"
elif [ $shuru -lt $suiji ];then
echo "請大一點"
18、判斷作業系統的版本号:
OS ( ) {
OS num=
sed -r 's/.*([0-9]+)\..*/\1/p' /etc/redhat-release
echo "$OSnum"
}
if [
OS
-eq 5 ];then
echo "OSversion is 5"
elif [
OS
-eq 6 ];then
echo "OSversion is 6"
OS
-eq 7 ];then
echo "OSversion is 7"
echo "unkonwn OS"