实用shell脚本

发布时间:2021-08-03 22:12:53 阅读:1232次

 显示经常用的前10的shell命令

  1 #!/bin/bash 
  2 cat ~/.bash_history |awk '{list[$1]++;}\
  3 END{
  4 for(i in list)
  5 {
  6 printf("%s\t%d\n",i,list[i]);}
  7 }' | sort -nrk 2 | head 
root@test:/home/test/shell# ./topten.sh 
ls 338
echo 312
cat 207
cd 131
vim 128
pwd 57
ssh 52
ping 43
ifconfig 40
while 37 
显示远程主机的运行时间
  1 #!/bin/bash
  2 ip_list="192.168.99.232 192.168.100.125"
  3 user="root"
  4 for ip in $ip_list
  5 do
  6         if [ $ip == "192.168.99.232" ]
  7         then
  8         uptime=$(ssh $user@$ip -p2013 uptime|awk '{print $3}')
  9         else
 10         uptime=$(ssh $user@$ip uptime|awk '{print $3}')
 11         fi
 12         echo $ip uptime: $uptime
 13 done 
root@test:/home/test/shell# ./uptime.sh 
root@192.168.99.232's password: 
192.168.99.232 uptime: 1:46,
root@192.168.100.125's password: 
192.168.100.125 uptime: 69 
显示远程主机磁盘使用情况
  1 logfile="diskusage.log"
  2 if [[ -n $1 ]]
  3 then
  4 logfile=$1
  5 fi
  6 
  7 if [ ! -e $logfile ]
  8 then
  9 printf "%-8s %-14s %-9s %-8s %-6s %-6s %-6s %s\n" "DATE" "IP address" "Device" "Capacity" "Used" "Free" "Percent" "Status" > $logfile
 10 fi
 11 
 12 ip_list="192.168.99.232 192.168.100.125"
 13 (
 14 for ip in $ip_list
 15 do 
 16 if [ $ip == "192.168.99.232" ]
 17 then
 18         ssh test@$ip -p2013 'df -H' | grep ^/dev/ > /tmp/$$.df
 19 else
 20         ssh root@$ip -p22 'df -H' | grep ^/dev/ > /tmp/$$.df
 21 fi
 22         while read line
 23         do
 24                 cur_date=$(date +%D)
 25                 printf "%-8s %-14s " $cur_date $ip
 26                 echo $line |awk '{ printf("%-9s %-8s %-6s %-6s %-8s",$1,$2,$3,$4,$5);}'
 27                 pusg=$(echo $line|egrep -o "[0-9]+%")
 28                 pusg=${pusg/\%/};
 29                 if [ $pusg -lt 80 ];
 30                 then
 31                         echo SAFE
 32                 else
 33                         echo ALERT
 34                 fi
 35         done</tmp/$$.df 
 36 done
 37 
 38 ) >>$logfile 
root@test:/home/test/shell# cat diskusage.log 
DATE     IP address     Device    Capacity Used   Free   Percent Status
11/06/13 192.168.99.232 /dev/sda11 97G      41G    51G    45%     SAFE
11/06/13 192.168.99.232 /dev/sda5 53G      48G    5.4G   90%     ALERT
11/06/13 192.168.100.125 /dev/sda2 21G      19G    1.1G   95%     ALERT
11/06/13 192.168.100.125 /dev/sda5 55G      41G    12G    78%     SAFE
11/06/13 192.168.100.125 /dev/sda1 104M     12M    87M    12%     SAFE

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:实用shell脚本 出自老鄢博客 | 欢迎分享