shell進階和計劃任務
阿新 • • 發佈:2021-08-15
1、編寫指令碼實現登陸遠端主機。(使用expect和shell指令碼兩種形式)
expect實現遠端登入:
[20:52:46 root@CentOS8 ~]\ [#cat expect.sh #!/usr/bin/expect spawn ssh 10.0.0.109 expect { "yes/no" { send "yes\n" ; exp_continue } "password" { send "123456\n" } } interact [20:54:16 root@CentOS8 ~]\ [#chmod +x expect.sh [20:54:39 root@CentOS8 ~]\ [#./expect.sh spawn ssh 10.0.0.109 [email protected]'s password: Last login: Sat Aug 14 20:52:32 2021 from 10.0.0.105 [20:54:57 root@CentOS7 ~]\ [#exit logout Connection to 10.0.0.109 closed.
shell指令碼實現遠端登入:
[21:34:05 root@CentOS8 ~]\ [#cat shell_expect.sh #!/bin/bash ip=$1 user=$2 password=$3 expect <<EOF set timeout 20 spawn ssh $user@$ip expect { "yes/no" { send "yes\n" ; exp_continue } "password" { send "$password\n" } } expect eof EOF [21:34:21 root@CentOS8 ~]\ [#chmod +x shell_expect.sh [21:34:29 root@CentOS8 ~]\ [#./shell_expect.sh 10.0.0.109 root 123456 spawn ssh [email protected] [email protected]'s password: Last login: Sat Aug 14 21:30:58 2021 from 10.0.0.105
2、生成10個隨機數保存於陣列中,並找出其最大值和最小值
[21:48:12 root@CentOS8 data]\ [#cat min_max.sh #!/bin/bash # ################################### #Auntor: Zhaoyaxuan #QQ: 907620409 #Email: [email protected] #Date: 2021-08-14 21:40:39 #Description: script ################################### declare -i min max declare -a nums for ((i=0;i<10;i++));do nums[$i]=$RANDOM [ $i -eq 0 ] && min=${nums[0]} && max=${nums[0]} && continue [ ${nums[$i]} -gt $max ] && max=${nums[$i]} && continue [ ${nums[$i]} -lt $min ] && min=${nums[$i]} done echo "ALL Numbers are ${nums[*]}" echo Max is $max echo Min is $min [21:48:19 root@CentOS8 data]\ [#chmod +x min_max.sh [21:48:30 root@CentOS8 data]\ [#bash min_max.sh ALL Numbers are 17785 4631 17727 19062 14535 15120 6926 20604 7355 24110 Max is 24110 Min is 4631
3、輸入若干個數值存入陣列中,採用冒泡演算法進行升序或降序排序
[15:24:00 root@CentOS8 ~]\ [#cat number.sh
#!/bin/bash
#
###################################
#Auntor: Zhaoyaxuan
#QQ: 907620409
#Email: [email protected]
#Date: 2021-08-15 15:21:30
#Description: script
###################################
declare -a number
for (( i=0; i<10; i++ ));do
number[$i]=$RANDOM
done
echo "before sort:"
echo ${number[@]}
declare -i n=10
for (( i=0; i<n-1; i++ ));do
for (( j=0; j<n-1-i; j++ ));do
let next=$j+1
if (( ${number[$j]} < ${number[$next]} ));then
tmp=${number[$next]}
number[$next]=${number[$j]}
number[$j]=$tmp
fi
done
done
echo "after sort:"
echo ${number[*]}
echo "the max integer is ${number[0]},the min integer is ${number[$(( n-1 ))]}"
[15:24:10 root@CentOS8 ~]\ [#chmod +x number.sh
[15:24:13 root@CentOS8 ~]\ [#bash number.sh
before sort:
984 30551 31434 32264 19109 16360 11345 12374 7813 14151
after sort:
32264 31434 30551 19109 16360 14151 12374 11345 7813 984
the max integer is 32264,the min integer is 984
4、總結檢視系統負載的幾種命令,總結top命令的指標大概什麼含義(不要求全部寫出來)
cat /ect/cpuinfo 檢視cpu資訊
uptime 檢視負載
w 檢視負載
mpstat 顯示CPU相關統計
vmstat 虛擬記憶體資訊
iostat 統計CPU和裝置IO資訊
top 提供動態的實時程序狀態
htop 增強版的top命令。
總計top命令:
running 執行態 ready 就緒態 stopped 停止態(暫停於記憶體,但不會被排程,出發手動啟動)
zombie 殭屍態(結束程序,父程序結束前,子程序不關閉,殺死父程序可以關閉殭屍態的子程序)
睡眠態分兩種,可終端:interruptable,不可中斷:uninterruptable
top命令欄:
us:使用者空間 sy:核心空間 ni:調整nice時間 id:空閒 wa:等待IO時間 hi:硬中斷
si:軟中斷(模式切換) st:虛擬機器偷走的時間
5、編寫指令碼,使用for和while分別實現192.168.0.0/24網段內,地址是否能夠ping通,若ping通則輸出"success!",若ping不通則輸出"fail!"
for迴圈編寫指令碼
[13:55:52 root@CentOS8 data]\ [#cat ping_test.sh
#!/bin/bash
#
###################################
#Auntor: Zhaoyaxuan
#QQ: 907620409
#Email: [email protected]
#Date: 2021-08-15 13:49:43
#Description: script
###################################
NET=192.168.0
for ID in {1..254};do
{
ping -c1 -W1 $NET.$ID &> /dev/null && echo $NET.$ID is success || echo $NET.$ID is fail
}
done
[13:56:22 root@CentOS8 data]\ [#chmod +x ping_test.sh
[13:56:36 root@CentOS8 data]\ [#bash ping_test.sh
192.168.0.1 is fail
192.168.0.2 is success
192.168.0.3 is fail
192.168.0.4 is fail
......
while迴圈編寫指令碼
[14:13:54 root@CentOS8 data]\ [#cat while_ping_test.sh
#!/bin/bash
#
###################################
#Auntor: Zhaoyaxuan
#QQ: 907620409
#Email: [email protected]
#Date: 2021-08-15 14:09:58
#Description: script
###################################
NET=192.168.0
declare -i i=1
while [ $i -le 254 ];do
ping -c1 -W1 $NET.$i &> /dev/null
if [ $? -eq 0 ];then
echo $NET.$i is success
else
echo $NET.$i is fail
fi
let i++
done
[14:14:01 root@CentOS8 data]\ [#chmod +x while_ping_test.sh
[14:14:11 root@CentOS8 data]\ [#bash while_ping_test.sh
192.168.0.1 is fail
192.168.0.2 is success
192.168.0.3 is fail
192.168.0.4 is fail
......
6、每週的工作日1:30,將/etc備份至/backup目錄中,儲存的檔名稱格式 為“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的時間
[14:40:16 root@CentOS8 ~]\ [#cat /data/back.sh
#!/bin/bash
#
###################################
#Auntor: Zhaoyaxuan
#QQ: 907620409
#Email: [email protected]
#Date: 2021-08-15 14:27:39
#Description: script
###################################
[ -d /backup ] || mkdir /backup
tar -Jcpvf /backup/ectbak-`date -d '-1 day' +'%F-%H'`.tar.xz /etc
[14:40:31 root@CentOS8 ~]\ [#chmod +x /data/back.sh
[14:40:41 root@CentOS8 ~]\ [#crontab -l
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
30 1 * * 1-5 /data/back.sh