脚本要求:
当根目录已用空间在80%以上,以邮件形式发送警告
[root@136 mnt]# vim Emilwarning.sh
[root@136 mnt]# cat Emilwarning.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@136 mnt]#
[root@136 mnt]# df -H
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 11G 3.5G 7.4G 32% /
devtmpfs 481M 0 481M 0% /dev
tmpfs 497M 82k 497M 1% /dev/shm
tmpfs 497M 14M 484M 3% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 496M 2.5M 463M 1% /home
[root@136 mnt]# dd if=/dev/zero of=/bigfile bs=1M count=5000
5000+0 records in
5000+0 records out
5242880000 bytes (5.2 GB) copied, 74.2743 s, 70.6 MB/s
[root@136 mnt]#
[root@136 mnt]# df -H
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 11G 8.7G 2.1G 81% /
devtmpfs 481M 0 481M 0% /dev
tmpfs 497M 82k 497M 1% /dev/shm
tmpfs 497M 14M 484M 3% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 496M 2.5M 463M 1% /home
[root@136 mnt]# sh Emilwarning.sh
Your / will full !!
Your / will full !!
Your / will full !!
Your / will full !!
Your / will full !!
Your / will full !!
Your / will full !!
^C
[root@136 mnt]#
脚本要求:
执行脚本时,没有目标文件报错
文件不存在报错
文件存在时,显示文件是什么类型的
[root@136 mnt]# vim file.sh
[root@136 mnt]# cat file.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@136 mnt]# sh file.sh
Please input a file follow script!!
[root@136 mnt]# sh file.sh /mnt/file.sh
/mnt/file.sh is common file
[root@136 mnt]# sh file.sh /etc/passwd
/etc/passwd is common file
[root@136 mnt]# sh file.sh /etc/system
/etc/system is not exist !!
[root@136 mnt]# sh file.sh /dev/vdb
/dev/vdb is block
脚本要求:
文件数量不对报错
文件不存在报错
文件行数差异报错
用户存在时显示用户存在,但是不改变此用户密码
当用户不存在时,建立用户并设定相应密码
[root@136 mnt]# vim user_create.sh
[root@136 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@136 mnt]# vim userfile
[root@136 mnt]# cat userfile
user1
user2
user3
[root@136 mnt]# vim passfile
[root@136 mnt]# cat passfile
123
123
123
[root@136 mnt]# sh user_create.sh
ERROR1: please input userfile and passwordfile follow script!!
[root@136 mnt]# sh user_create.sh pass passfile
ERROR2: pass is not exist!!
[root@136 mnt]# sh user_create.sh userfile passfile
useradd: user 'user1' already exists
useradd: user 'user2' already exists
useradd: user 'user3' already exists
[root@136 mnt]# vim user_create.sh 先删除用户
[root@136 mnt]# sh user_create.sh userfile passfile
[root@136 mnt]# vim user_create.sh
[root@136 mnt]# sh user_create.sh userfile passfile
user1 created!!
user2 created!!
user3 created!!
[root@136 mnt]#
执行:sh filename.sh create userfile passfile 时,创建用户并设定相应密码
执行:sh filename.sh delete userfile passfile 时,删除用户
[root@136 mnt]# cat userfile
user1
user2
user3
[root@136 mnt]# cat passfile
123
123
123
[root@136 mnt]# vim case.sh
[root@136 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@136 mnt]# id user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)
[root@136 mnt]# id user2
uid=1002(user2) gid=1002(user2) groups=1002(user2)
[root@136 mnt]# id user3
uid=1003(user3) gid=1003(user3) groups=1003(user3)
[root@136 mnt]# sh case.sh delete userfile passfile
[root@136 mnt]# id user1
id: user1: no such user
[root@136 mnt]# id user2
id: user2: no such user
[root@136 mnt]# id user3
id: user3: no such user
[root@136 mnt]# sh case.sh create userfile passfile
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
[root@136 mnt]#
执行:/mnt/auto_connect.exp IP password 时
密码正确,则通过 ssh 连接到该 IP 主机
[root@136 mnt]# vim auto_connect.exp
[root@136 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@136 mnt]# chmod +x /mnt/auto_connect.exp
[root@136 mnt]# /mnt/auto_connect.exp 172.25.254.236 redhat
spawn ssh root@172.25.254.236
The authenticity of host '172.25.254.236 (172.25.254.236)' can't be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.236' (ECDSA) to the list of known hosts.
root@172.25.254.236's password:
Last login: Fri Jun 29 09:15:47 2018
[root@server ~]# logout
Connection to 172.25.254.236 closed.
[root@136 mnt]#
linux \n
windows \n\r
unix \r
上次脚本会出错
文本处理工具不兼容
不同操作系统的换行符不同
[root@136 mnt]# vim host.sh
[root@136 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 {236..237}
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 | sed 's/\r//g'
done
[root@136 mnt]# sh host.sh
foundation236.ilt.example.com 172.25.254.236
Permission denied, please try again. 172.25.254.237