生活随笔
收集整理的這篇文章主要介紹了
linux——脚本的练习示例二
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、
腳本要求:
當根目錄已用空間在80%以上,以郵件形式發送警告
[root@desktop27 mnt]
[root@desktop27 mnt]
#!/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]
[root@desktop27 mnt]
#!/bin/bash
Check_file()
{
if[
"$1" "$2" ]
thenecho "$2" is
$3exit 0fi
}
if
[
"$#" -ne "1" ]
thenecho "Please input a file follow script!!"elif
[ !
-e "$1" ]
thenecho "$1 is not exist !!"
elseCheck_file -L
$1 linkCheck_file
-f $1 "common file"Check_file
-s $1 socketCheck_file -b
$1 blockCheck_file -c
$1 characterCheck_file
-d $1 directory
fi
[root@desktop27 mnt]
[root
@desktop27 mnt]
Please input a file follow script!!
[root
@desktop27 mnt]
/etc/passwd
is common file
[root
@desktop27 mnt]
/etc/system
is not exist !!
[root
@desktop27 mnt]
/dev/vdb
is block
[root
@desktop27 mnt]
三、
腳本要求:
文件數量不對報錯
文件不存在報錯
文件行數差異報錯
用戶存在時顯示用戶存在,但是不改變此用戶密碼
當用戶不存在時,建立用戶并設定相應密碼
[root@desktop27 mnt]
[root@desktop27 mnt]
#!/bin/bash
if
[
"$#" -ne "2" ]
thenecho "ERROR1: please input userfile and passwordfile follow script!! "exit 1
elif
[ !
-e "$1" ]
thenecho "ERROR2: $1 is not exist!!"exit 1
elif
[ !
-e "$2" ]
thenecho "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" ]
thenecho "ERROR4: $1's line is different form $2's"
else
for Line_Num
in `seq
1 $USERFILE_LINE`
doUSERNAME=`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]
[root@desktop27 mnt]
user1
user2
user3
[root@desktop27 mnt]
[root@desktop27 mnt]
user1123
user2123
user3123
[root@desktop27 mnt]
[root@desktop27 mnt]
#!/bin/bash
[
"$#" -ne "3" ]&&{
echo "Please input create or delete and userfile and passwordfile after script!!"exit 1
}
case $1 increate)USERLINE=`awk
'BEGIN{N=0}{N++}END{print N}' $2`
for LINE_NUM
in `seq
1 $USERLINE`
doUSERNAME=`sed -n
"${LINE_NUM}p" $2`PASSWORD=`sed -n
"${LINE_NUM}p" $3`useradd
$USERNAMEecho $PASSWORD | passwd --stdin
$USERNAMEdone;;delete)USERLINE=`awk
'BEGIN{N=0}{N++}END{print N}' $2`
for LINE_NUM
in `seq
1 $USERLINE`
doUSERNAME=`sed -n
"${LINE_NUM}p" $2`PASSWORD=`sed -n
"${LINE_NUM}p" $3`userdel -r
$USERNAMEdone;;
esac
[root@desktop27 mnt]
五、
腳本要求:
執行:/mnt/auto_connect.exp IP password 時
密碼正確,則通過 ssh 連接到該 IP 主機
[root
@desktop27 mnt]
[root
@desktop27 mnt]
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]
[root
@desktop27 mnt]
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]
[root@desktop27 mnt]
#!/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}
doping -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]
172.25.
254.50lt.example.com
172.25.
254.51ied, please try again.
[root@desktop27 mnt]
[root@desktop27 mnt]
#!/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}
doping -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]
總結
以上是生活随笔為你收集整理的linux——脚本的练习示例二的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。