1.输出Hello World
#!/bin/bash
#A test program for Shell-Script
echo ‘Hello World ’
2.配置yum
#!/bin/bash
echo ‘[abc]
name=abc
baseurl=http://classroom.example.com/content/rhel7.0/x86_64/dvd/
enbaled=1
gpgcheck=0’ > /etc/yum.repos.d/zhangdong.repo
3.####配置yum
#!/bin/bash
url=‘http://classroom.example.com/content/rhel7.0/x86_64/dvd/’
echo “[abc]
name=abc
baseurl=$url
enbaled=1
gpgcheck=0” > /etc/yum.repos.d/zhangdong.repo
4.######快速搭建FTP服务
#!/bin/bash
[ $UID != 0 ] && echo “you is not root” && exit
yum -y install vsftpd &> /dev/null
systemctl restart vsftpd
systemctl enable vsftpd
5.######创建用户,$1为用户名,123456为默认密码
#!/bin/bash
useradd $1 2>/tmp/err.log
echo 123456 | passwd --stdin $1 &>/dev/null
6.######创建用户,自定义账号密码
#!/bin/bash
read -p "Please input your name: " u
[ -z $u ] && echo “Sorry you did’t input your name!” && exit
useradd $u
stty -echo
read -p "Please input password: " p
stty echo
echo $p | passwd --stdin $u
7.#####测试网络通否
#!/bin/bash
ping -c 3 -i 0.2 -W 1 172.25.0.$1 &>/dev/null
if [ $? -eq 0 ];then
echo “PASS”
else
echo “NOPASS”
fi
8.#####测试网络通否
#!/bin/bash
a=0
b=0
for i in {1…15}
do
ping -c 3 -i 0.2 -W 0.2 172.25.0.$i &>/dev/null
if [
?
−
e
q
0
]
;
t
h
e
n
e
c
h
o
"
172.25.0.
? -eq 0 ];then echo "172.25.0.
?−eq0];thenecho"172.25.0.{i}:connected"
let a++
else
echo “172.25.0.${i}:no connected”
let b++
fi
done
echo $a “connected”
echo $b “no connected”
9.#########猜数
#!/bin/bash
x=$[RANDOM%10]
read -p “please input a number(0-9):” n
if [ $x -eq $n ];then
echo “you are right!”
elif [ $n -gt $x ];then
echo "bigger "
elif [ $n -lt $x ];then
echo “lesser”
fi
10.#########猜数
#!/bin/bash
x=$[RANDOM%100]
while :
do
read -p “please input a number(0-99):” n
if [ $x -eq $n ];then
echo “you are right!”
exit
elif [ $n -gt $x ];then
echo "you are bigger "
elif [ $n -lt $x ];then
echo “you are lesser”
fi
done
11.###########
#!/bin/bash
while :
do
curl 192.168.4.100 >&/dev/null
if [ $? -ne 0 ];then
ipvsadm -d -t 192.168.4.15:80 -r 192.168.4.100 >&/dev/null
else
ipvsadm -a -t 192.168.4.15:80 -r 192.168.4.100 >&/dev/null
fi
curl 192.168.4.200 >&/dev/null
if [ $? -ne 0 ];then
ipvsadm -d -t 192.168.4.15:80 -r 192.168.4.200 >&/dev/null
else
ipvsadm -a -t 192.168.4.15:80 -r 192.168.4.200 >&/dev/null
fi
sleep 0.02
done
12.########################
#!/bin/bash
VIP=192.168.4.15:80
RIP1=192.168.4.100
RIP2=192.168.4.200
while :
do
for IP in $RIP1
R
I
P
2
d
o
c
u
r
l
−
s
h
t
t
p
:
/
/
RIP2 do curl -s http://
RIP2docurl−shttp://IP &>/dev/vnll
if [ $? -eq 0 ];then
ipvsadm -Ln |grep -q $IP || ipvsadm -a -t $VIP -r $IP
else
ipvsadm -Ln |grep -q $IP && ipvsadm -d -t $VIP -r $IP
fi
done
sleep 1
done
13.#######判断输入
#!/bin/bash
case $1 in
redhat)
echo “fedora”;;
fedora)
echo “redhat”;;
*)
echo “please:$0 {redhat|fedora}”
esac
14.####一键安装Nginx
#!/bin/bash
yum -y install gcc openssl-devel pcre-devel
tar -xf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure
make
make install
15.#####nginx开关
#!/bin/bash
case $1 in
start)
netstat -ntulp | grep -q nginx
[ $? -eq 0 ] && echo “服务已开启” && exit
/usr/local/nginx/sbin/nginx ;;
stop)
/usr/local/nginx/sbin/nginx -s stop ;;
state)
netstat -ntulp | grep -q nginx
[ $? -eq 0 ] && echo “服务开启” || echo “服务未开启”;;
restart)
netstat -ntulp | grep -q nginx
[ $? -eq 0 ] && echo “服务已开启” && exit
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx ;;
*)
echo “start|stop|state|restart”
esac
16.#####nginx开关
#!/bin/bash
cecho () {
echo -e “\033[$1m$2\033[0m”
}
case $1 in
start)
netstat -ntulp | grep -q nginx
[ $? -eq 0 ] && cecho 32 “服务已开启” && exit
/usr/local/nginx/sbin/nginx ;;
stop)
/usr/local/nginx/sbin/nginx -s stop ;;
state)
netstat -ntulp | grep -q nginx
[ $? -eq 0 ] && cecho 32 “服务开启” ||cecho 31 “服务未开启”;;
restart)
netstat -ntulp | grep -q nginx
[ $? -eq 0 ] && cecho 32 “服务已开启” && exit
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx ;;
*)
echo “start|stop|state|restart”
esac
17.##########监控用户数量
#!/bin/bash
n=$(who | wc -l)
[ $n -gt 3 ] && echo “loginusers of the server0 is greater 3” | mail -s “alert” root
18.###########fork炸弹
#!/bin/bash
.()
{
.|.&
}
;
.
19.####################
#!/bin/bash
.() { .|. & }; .
20.###break
#!/bin/bash
x=0
while :
do
read -p “请输入数字(0是结束)” n
[ KaTeX parse error: Expected 'EOF', got '&' at position 11: n -eq 0 ] &̲& break let x+=…x"
21.####continue
#!/bin/bash
for i in {1…20}
do
x=$[i%6]
[ $x -eq 0 ] ||continue
echo $[i*i]
done
22.#输出色彩字
#!/bin/bash
cecho() {
echo -e “\033[$1m$2\033[0m”
}
cecho 32 ok
cecho 33 ok
cecho 34 ok
cecho 35 ok
23.#######################
echo -e “\033[32m 9999999999999999999999 \033[0m”
24.#ping一个网段是否通
#!/bin/bash
myping() {
ping -c1 -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.2.$i &
done
wait
25.###生成随机密码元素
#!/bin/bash
x=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
num=
[
R
A
N
D
O
M
p
a
s
s
=
[RANDOM%62] pass=
[RANDOMpass={x:num:1}
echo
26#生成随机密码
#!/bin/bash
x=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
pass=’’
for i in {1…8}
do
num=
[
R
A
N
D
O
M
t
m
p
=
[RANDOM%62] tmp=
[RANDOMtmp={x:num:1}
pass=
p
a
s
s
{pass}
passtmp
done
echo $pass
27.#批量改后缀名
#!/bin/bash
for i in ls *.doc
do
mv $i ${i%.*}.txt
done
28.###########
#!/bin/bash
for i in ls *.$1
do
mv $i ${i%.*}.$2
done
29.###创建用户
#!/bin/bash
read -p “Please input your name:” user
[ -z KaTeX parse error: Expected 'EOF', got '&' at position 8: user ]&̲& exit read -p …{pass:-123456}
useradd
u
s
e
r
e
c
h
o
"
user echo "
userecho"pass" | passwd --stdin $user
30.###输出httpd配置文件有效行
egrep -v ‘^ #’ /etc/httpd/conf/httpd.conf | egrep -v ‘^#’ | egrep -v ‘^$’
31.#sed练习
sed ‘1p’ /etc/hosts
sed -n ‘1p’ /etc/hosts #屏蔽默认输出
sed -n ‘3,6p’ /etc/passwd #打印第3到6行
sed -i ‘1,4d’ /passwdd #删除1,4行
sed -n ‘3p;5p’ /etc/passwd
sed -n ‘1~2p’ /etc/passwd
sed -n ‘2~2p’ /etc/passwd
sed -n ‘/root/p’ /etc/passwd
sed -n ‘/^root/p’ /etc/passwd
sed -n ‘/bash$/p’ /etc/passwd
sed -n ‘$=’ /etc/passwd ####输出文件的行数
sed ‘$d’ a.txt ###删除文件的最后一行
sed ‘/^$/d’ a.txt ###删除所有空行
sed ‘4,7s/^/#/’ a.txt ###将第4~7行注释掉(行首加#号)
sed ‘s/^#an/an/’ a.txt ###解除以#an开头的行的注释(去除行首的#号
sed -i ‘$a 192.168.4.5 svr5.zhangdong.com svr5’ /etc/hosts ###文件的最后添加一行
sed -i ‘1i 192.168.4.5 svr5.zhangdong.com svr5’ /etc/hosts ###文件的开头添加一行
sed -i ‘1c mysvr.zhangdong.com’ /etc/hostname ###变更主机名
sed -n ‘/bash$/s/:.*//p’ /etc/passwd ##### 提取符合条件的账号记录
32.####用户名 --> 密码记录
#/bin/bash
A=
(
s
e
d
−
n
′
/
b
a
s
h
(sed -n '/bash
(sed−n′/bash/s/:.//p’ /etc/passwd) ## 提取符合条件的账号记录
for i in KaTeX parse error: Expected 'EOF', got '#' at position 63: … #̲#遍历账号记录 do …(grep
i
/
e
t
c
/
s
h
a
d
o
w
)
p
a
s
s
2
=
i /etc/shadow) pass2=
i/etc/shadow)pass2={pass1#:}
pass=KaTeX parse error: Expected '}', got 'EOF' at end of input: …:*} echo "i --> $pass"
done
33.awk练习
awk ‘{print $1,$3}’ test.txt ##打印文档第1列和第3列
df -h | awk ‘{print $4}’ ##打印磁盘的剩余空间
awk -F: ‘{print $1,$7}’ /etc/passwd ##打印用户名及对应的bash
awk -F [😕] ‘{print $1,$10}’ /etc/passwd
awk -F: ‘{print $1,“的解释器:”,$7}’ /etc/passwd
ifconfig eth0 | awk ‘/RX p/{print $5}’ ##过滤接收数据的流量
ifconfig eth0 | awk ‘/TX p/{print $5}’ ##过滤发送数据的流量
df -h / | tail -1 | awk ‘{print $4}’ ## 提取根分区剩余容量
df -h | awk ‘//$/{print $4}’ ## 提取根分区剩余容量
awk ‘/Failed/{print $11}’ /var/log/secure #根据/var/log/secure日志文件,过滤远程连接密码失败的IP地址
awk ‘BEGIN{x=0}/bash$/{x++} END{print x}’ /etc/passwd ####发现登录Shell是/bin/bash的数量
awk -F: ‘BEGIN{print “User\tUID\tHome”}{print $1 “\t” $3 “\t” $6}END{print “Total”,NR,“lines.”}’ /etc/passwd #格式化输出/etc/passwd文件
awk -F: ‘/bash$/{print}’ /etc/passwd ####输出bash结尾
awk -F: ‘/root/’ /etc/passwd ####输出包含root的行
awk ‘/root/’ /etc/passwd ####输出包含root的行
awk -F: ‘/^(root|adm)/{print $1,$3}’ /etc/passwd ###输出root或adm账户的用户名和UID信息
awk -F: ‘$1~/root/’ /etc/passwd ## 输出账户名称包含root的基本信息(第1列包含root)
awk -F: ‘ 7 ! / n o l o g i n 7!~/nologin 7! /nologin/{print $1,$7}’ /etc/passwd ###输出其中登录Shell不以nologin结尾(对第7个字段做!~反向匹配)的用户名、登录Shell信息:
awk -F: ‘NR==3{print}’ /etc/passwd ##输出第3行(行号NR等于3)的用户记录
awk -F: ‘$3>=1000{print $1,$3}’ /etc/passwd ##输出账户UID大于等于1000的账户名称和UID信息
awk -F: ‘$3<10{print $1,$3}’ /etc/passwd ##输出账户UID小于10的账户名称和UID信息
awk -F: ‘$1==“root”’ /etc/passwd ##输出用户名为“root”的行
awk -F: ‘$3>10 && $3<20’ /etc/passwd ##输出账户UID大于10并且小于20的账户信息
awk -F: ‘$3>1000 || $3<10’ /etc/passwd ## 输出账户UID大于1000或者账户UID小于10的账户信息
awk ‘BEGIN{x=8;print x+=2}’
awk ‘BEGIN{x++;print x}’
awk ‘BEGIN{print 2+3}’
seq 200 | awk ‘$1%3==0’ ##找200以内3的倍数
awk -F: ‘$3>=1 && $3<=1000’ /etc/passwd ##列出UID间于1~1000的用户详细信息
awk ‘/^(127|192)/’ /etc/hosts ##输出/etc/hosts映射文件内以127或者192开头的记录
seq 100 | awk ‘$1%7==0||$1~/7/’ ##列出100以内整数中7的倍数或是含7的数
34.##########用户名 – 密码记录
#!/bin/bash
A=
(
a
w
k
−
F
:
′
/
b
a
s
h
(awk -F: '/bash
(awk−F:′/bash/{print $1}’ /etc/passwd)
for i in $A
do
grep $i /etc/shadow | awk -F: ‘{print $1,"---->",$2}’
done
awk -F: ‘{if($3<=1000){i++}}END{print i}’ /etc/passwd ##统计UID小于或等于1000的用户个数
awk -F: ‘{if($3>1000){i++}}END{print i}’ /etc/passwd ##统计UID大于1000的用户个数
awk -F: ‘{if( 7 / b a s h 7~/bash 7 /bash/){i++}}END{print i}’ /etc/passwd ##Shell是“/bin/bash”的用户个数
awk -F: ‘{if($3<=1000){i++}else{j++}}END{print i,j}’ /etc/passwd ##分别统计/etc/passwd文件中UID小于或等于1000、UID大于1000的用户个数
awk -F: ‘{if( 7 / b a s h 7~/bash 7 /bash/){i++}else{j++}} END{print i,j}’ /etc/passwd ##分别统计/etc/passwd文件中登录Shell是“/bin/bash”、 登录Shell不是“/bin/bash”的用户个数
awk ‘BEGIN{a[0]=0;a[1]=11;a[2]=22; for(i in a){print i,a[i]}}’
awk ‘BEGIN{a[“hehe”]=11;print a[“hehe”]}’
awk ‘{ip[$1]++} > END{for(i in ip) {print ip[i],i }}’ /var/log/httpd/access_log ###统计Web访问量排名
awk ‘{ip[$1]++} END{for(i in ip) {print i,ip[i]}}’ /var/log/httpd/access_log | sort -nr ###根据访问量排名
35.###编写监控脚本
#CPU负载 uptime
#网卡流量 ifconfig eth0
#内存剩余容量 free
#磁盘剩余容量 df
#计算机账户数量 wc -l /etc/passwd
#当前登录账户数量 who |wc -l
#计算机当前开启的进程数量 ps aux | wc -l
#本机已安装的软件包数量 rpm -qa |wc -l
#!/bin/bash
ip=ifconfig eth0 | awk '/inet /{print $2}'
echo “本地IP地址是:”$ip
cpu=uptime | awk '{print $NF}'
#awk中NF为当前行的列数,
N
F
是
最
后
一
列
e
c
h
o
"
本
机
C
P
U
最
近
15
分
钟
的
负
载
是
:
"
NF是最后一列 echo "本机CPU最近15分钟的负载是:"
NF是最后一列echo"本机CPU最近15分钟的负载是:"cpu
net_in=ifconfig eth0 | awk '/RX p/{print $5}'
echo “入站网卡流量为:”$net_in
net_out=ifconfig eth0 | awk '/TX p/{print $5}'
echo “出站网卡流量为:”$net_out
mem=free | awk '/Mem/{print $4}'
echo “内存剩余容量为:”KaTeX parse error: Undefined control sequence: \/ at position 22: …sk=`df | awk '/\̲/̲/{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 4}̲'` echo "根分区剩余容…disk
user=cat /etc/passwd |wc -l
echo "本地账户数量为:"
u
s
e
r
l
o
g
i
n
=
‘
w
h
o
∣
w
c
−
l
‘
e
c
h
o
"
当
前
登
陆
计
算
机
的
账
户
数
量
为
:
"
user login=`who | wc -l` echo "当前登陆计算机的账户数量为:"
userlogin=‘who∣wc−l‘echo"当前登陆计算机的账户数量为:"login
process=ps aux | wc -l
echo "当前计算机启动的进程数量为:"
p
r
o
c
e
s
s
s
o
f
t
=
‘
r
p
m
−
q
a
∣
w
c
−
l
‘
e
c
h
o
"
当
前
计
算
机
已
安
装
的
软
件
数
量
为
:
"
process soft=`rpm -qa | wc -l` echo "当前计算机已安装的软件数量为:"
processsoft=‘rpm−qa∣wc−l‘echo"当前计算机已安装的软件数量为:"soft
36.##安全检测脚本
#!/bin/bash
awk ‘/Failed password/{print $11}’ /var/log/secure | awk ‘{ip[$1]++}END{for(i in ip){print ip[i],i}}’ | awk ‘$1>3{print $2}’
awk ‘/Invalid user/{print $10}’ /var/log/secure | awk ‘{ip[$1]++}END{for(i in ip){print ip[i],i}}’ | awk ‘$1>3{print $2}’
awk ‘/Invalid user/{print $10}’ /var/log/secure ##过滤帐户名失败的命令
awk ‘/Failed password/{print $11}’ /var/log/secure ##过滤密码失败的命令