一、
脚本要求:
当根目录已用空间在80%以上,以邮件形式发送警告
[root@desktop27 mnt]# vim Warning.sh
[root@desktop27 mnt]# cat Warning.sh
#!/bin/bash
DISK_NUM=`df -h | awk '/\/$/{print $5}' | awk -F "%" '{print $1}'`
TTY=`ps $$ | awk '/bash$/{print $2}'`
while true
do
[ "$DISK_NUM" -ge "80" ]&&{
echo "Your / will full !!"
}
sleep 3
done
[root@desktop27 mnt]#
二、
脚本要求:
执行脚本时,没有目标文件报错
文件不存在报错
文件存在时,显示文件是什么类型的
[root@desktop27 mnt]# vim if.sh
[root@desktop27 mnt]# cat if.sh
#!/bin/bash
Check_file()
{
if
[ "$1" "$2" ]
then
echo "$2" is $3
exit 0
fi
}
if
[ "$#" -ne "1" ]
then
echo "Please input a file follow script!!"
elif
[ ! -e "$1" ]
then
echo "$1 is not exist !!"
else
Check_file -L $1 link
Check_file -f $1 "common file"
Check_file -s $1 socket
Check_file -b $1 block
Check_file -c $1 character
Check_file -d $1 directory
fi
[root@desktop27 mnt]#
##执行脚本
[root@desktop27 mnt]# sh if.sh
Please input a file follow script!!
[root@desktop27 mnt]# sh if.sh /etc/passwd
/etc/passwd is common file
[root@desktop27 mnt]# sh if.sh /etc/system
/etc/system is not exist !!
[root@desktop27 mnt]# sh if.sh /dev/vdb
/dev/vdb is block
[root@desktop27 mnt]#
三、
脚本要求:
文件数量不对报错
文件不存在报错
文件行数差异报错
用户存在时显示用户存在,但是不改变此用户密码
当用户不存在时,建立用户并设定相应密码
[root@desktop27 mnt]# vim user_create.sh
[root@desktop27 mnt]# cat user_create.sh
#!/bin/bash
################ Check Rule ##############
if
[ "$#" -ne "2" ]
then
echo "ERROR1: please input userfile and passwordfile follow script!! "
exit 1
elif
[ ! -e "$1" ]
then
echo "ERROR2: $1 is not exist!!"
exit 1
elif
[ ! -e "$2" ]
then
echo "ERROR3: $2 is not exist!!"
exit 1
elif
USERFILE_LINE=`awk 'BEGIN{N=0}{N++}END{print N}' $1`
PASSFILE_LINE=`awk 'BEGIN{N=0}{N++}END{print N}' $2`
[ "$USERFILE_LINE" -ne "$PASSFILE_LINE" ]
then
echo "ERROR4: $1's line is different form $2's"
else
################ Create User ############
for Line_Num in `seq 1 $USERFILE_LINE`
do
USERNAME=`sed -n ${Line_Num}p $1`
PASSWORD=`sed -n ${Line_Num}p $2`
useradd $USERNAME
[ "$?" -eq "0" ]&&{
echo $PASSWORD | passwd --stdin $USERNAME &> /dev/null && echo $USERNAME created!!
}
done
fi
[root@desktop27 mnt]#
四、
脚本要求:
执行:sh filename.sh create userfile passfile 时,创建用户并设定相应密码
执行:sh filename.sh delete userfile passfile 时,删除用户
[root@desktop27 mnt]# vim userfile
[root@desktop27 mnt]# cat userfile
user1
user2
user3
[root@desktop27 mnt]# vim passfile
[root@desktop27 mnt]# cat passfile
user1123
user2123
user3123
[root@desktop27 mnt]# vim case.sh
[root@desktop27 mnt]# cat case.sh
#!/bin/bash
[ "$#" -ne "3" ]&&{
echo "Please input create or delete and userfile and passwordfile after script!!"
exit 1
}
case $1 in
create)
USERLINE=`awk 'BEGIN{N=0}{N++}END{print N}' $2`
for LINE_NUM in `seq 1 $USERLINE`
do
USERNAME=`sed -n "${LINE_NUM}p" $2`
PASSWORD=`sed -n "${LINE_NUM}p" $3`
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME
done
;;
delete)
USERLINE=`awk 'BEGIN{N=0}{N++}END{print N}' $2`
for LINE_NUM in `seq 1 $USERLINE`
do
USERNAME=`sed -n "${LINE_NUM}p" $2`
PASSWORD=`sed -n "${LINE_NUM}p" $3`
userdel -r $USERNAME
done
;;
esac
[root@desktop27 mnt]#
五、
脚本要求:
执行:/mnt/auto_connect.exp IP password 时
密码正确,则通过 ssh 连接到该 IP 主机
[root@desktop27 mnt]# vim auto_connect.exp
[root@desktop27 mnt]# cat auto_connect.exp
#!/usr/bin/expect
set timeout 5
set IP [ lindex $argv 0 ]
set PASS [ lindex $argv 1 ]
spawn ssh root@$IP
expect {
"yes/no" { send "yes\r";exp_continue }
"password" { send "$PASS\r"}
}
interact
[root@desktop27 mnt]# chmod +x /mnt/auto_connect.exp
[root@desktop27 mnt]# /mnt/auto_connect.exp 172.25.254.50 westos
spawn ssh root@172.25.254.50
root@172.25.254.50's password:
Last login: Mon Jun 25 17:32:11 2018 from www.westos.com
[root@foundation50 ~]# logout
Connection to 172.25.254.50 closed.
[root@desktop27 mnt]#
六、
脚本要求:
执行脚本,ping IP ,能 ping通,显示该IP的 hostname以及IP
不能 ping 通,报错并显示 IP
[root@desktop27 mnt]# vim host.sh
[root@desktop27 mnt]# cat host.sh
#!/bin/bash
Auto_Connect()
{
/usr/bin/expect << EOF
set timeout 5
spawn ssh root@172.25.254.$IP_NUM hostname
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "westos\r" }
}
expect eof
EOF
}
for IP_NUM in {50..51}
do
ping -c1 -w1 172.25.254.$IP_NUM &> /dev/null &&{
Host_Name=`Auto_Connect | grep -E "authenticity|fingerprint|connecting|password|spawn|Warning" -v`
}
echo $Host_Name 172.25.254.$IP_NUM
done
[root@desktop27 mnt]# sh host.sh
172.25.254.50lt.example.com
172.25.254.51ied, please try again.
[root@desktop27 mnt]# vim host.sh
[root@desktop27 mnt]# cat host.sh
#!/bin/bash
Auto_Connect()
{
/usr/bin/expect << EOF
set timeout 5
spawn ssh root@172.25.254.$IP_NUM hostname
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "westos\r" }
}
expect eof
EOF
}
for IP_NUM in {50..51}
do
ping -c1 -w1 172.25.254.$IP_NUM &> /dev/null &&{
Host_Name=`Auto_Connect | grep -E "authenticity|fingerprint|connecting|password|spawn|Warning" -v`
}
echo $Host_Name 172.25.254.$IP_NUM >> hostname
done
[root@desktop27 mnt]#