小脚本集合

#cpu
#!/bin/bash
date=date +'%Y-%m-%d %H:%M:%S'
cpu_warn=‘70’
cpu_id=top -n1| grep Cpu|awk '{print $8}'|cut -d "." -f1
cpu_use=expr 100 - $cpu_id
echo “ d a t e , 当 前 C P U 使 用 率 为 : date,当前CPU使用率为: date,CPU使cpu_use” >> /shell/cpu.txt
if [ $cpu_use -gt $cpu_warn ]
then
echo “cpu warning!”
else
echo “cpu ok!”
fi

#df2
#!/bin/bash
###当前脚本用于监控虚拟机中磁盘使用情况,超过阈值则发邮件报警
echo “–monitoring–”
DF=df -hl | grep /dev | awk '{print $5}'|cut -d "%" -f1
for d in $DF
do
if [ d − g t 1 ] t h e n e c h o " o v e r ! " e c h o " 当 前 硬 盘 使 用 率 为 : d -gt 1 ] then echo "over!" echo "当前硬盘使用率为: dgt1]thenecho"over!"echo"使{d}%" >> /shell/a.txt
cat /shell/a.txt | mail -s “mail subject” xxxxxxxxxx@163.com
else
echo “normal!”
fi
done

#for3
#!/bin/bash
#此脚本删除当前目录下大小为0的文件
for filename in ls
do
if test -d f i l e n a m e t h e n b = 0 e l s e a = filename then b=0 else a= filenamethenb=0elsea=(ls -l $filename | awk ‘{ print $5 }’)
if test $a -eq 0
then rm $filename
fi
fi
done

#for4
#/bin/bash
#本脚本测试本网段1-10有哪些地址的服务器开启了
for i in 1 2 3 4 5 6 7 8 9
do
echo "the number of i c o m p u t e r i s " p i n g − c 10192.168.152. i computer is " ping -c 10 192.168.152. icomputeris"pingc10192.168.152.i
done

#for5
#/bin/bash
#本脚本用于查看当前目录下最大的文件
a=0
for name in .
do
b=$(ls -l $name | awk ‘{print $5}’)
if test $b -ge a t h e n a = a then a= athena=b
namemax=$name
fi
done
echo “the max file is $namemax”

#for 打印乘法口诀
#!/bin/bash

9*9 乘法表

for i in seq 9
do
for j in seq $i
do
echo -n " j ∗ j* ji=$[i*j] "
done
echo
done

#ftp
#!/bin/bash
#本脚本用于自动下载
ftp -n<<!
open 192.168.152.130
user guest 123456
binary
cd /home/data
lcd /home/databackup
prompt
get a.sh a.sh
close
bye
!

#if1
#!/bin/bash
#本脚本用来比较两个数的大小
echo “please enter two number”
read a
read b
if test $a -eq $b
then echo “NO.1 = NO.2”
elif test $a -gt $b
then echo “NO.1 > NO.2”
else echo “NO.1 < NO.2”
fi

#if2
#/bin/bash
#本脚本用来做Linux服务器模拟登陆
echo -n “login:”
read name
echo -n “password:”
read passwd
if [ $name = “root” -a $passwd = “abc” ];then
echo “the host and password is right!”
else echo “input is error!”
fi

#if3
#/bin/bash
#查找root目录下是否存在某个文件
echo “enter a file name:”
read a
if test -e /root/$a
then echo “the file is exist!”
else echo “the file is not exist!”
fi

#if4
#/bin/bash
#杀死相关进程
pid=ps -ef | grep 'mysqld' | grep -v 'grep' | awk '{ print $2}'
if [ -n “$pid” ]; then
kill -9 $pid
fi

#if5
#!/bin/bash
#检查服务端口是否监听
n=1
echo “mysql服务是否成功启动”
while true
do
if test $n -gt 20
then
echo “mysql服务启动失败”
break
fi

    sleep 5
    n=$(($n+1))
    port=`netstat -antp | grep "0.0.0.0:8000"`
    if [ ${#port} -gt 3 ]; then
            echo "mysql服务已经启动"
            break;
    fi

done

#if6
#/bin/bash
#echo -n “Please enter a user:”
read -ep “username is:” a
b=$(whoami)
if test $a = $b
then echo “the user is running.”
else echo “the user is not running.”
fi

#if6-1
#/bin/bash
echo “Please enter a user:”
read a
b=$(whoami)
if test $a = $b
then echo “the user is running.”
else echo “the user is not running.”
fi

#scp
#!/usr/bin/expect
#本脚本用于自动远程scp
set timeout 30
spawn scp /data/for1.sh root@192.168.152.130:/usr/local/src/
expect “password:”
#send “2008.cn@$abcdef\r”
send “2008.cn\r”
interact

#ssh
#!/usr/bin/expect
#本脚本用于自动ssh登录
set timeout 30
spawn ssh root@192.168.152.135
expect “password:”
send “2008.cn\r”
interact

#while打印网卡发送的数据流量
#!/bin/bash

使用死循环实时显示 eth0 网卡发送的数据包流量

while true
do
echo '本地网卡 eth0 流量信息如下: ’
ifconfig eth0 | grep “RX pack” | awk ‘{print $5}’
ifconfig eth0 | grep “TX pack” | awk ‘{print $5}’
sleep 1
done

#猜数字游戏
#!/bin/bash
ranNum= ( ( (( ((RANDOM%100+1))
echo “欢迎,输入数字”
read gusNum
if [ $gusNum -gt $ranNum ]
then
echo “猜大了!!!”
elif [ $gusNum -lt $ranNum ]
then
echo “猜小了!!!”
else
echo “恭喜猜对了!!!”
echo “本局游戏结束!!!”
fi
echo “是否开始新的游戏”
read flag2
if [ $flag2 = ‘N’ ]
then
echo “游戏结束”
elif [ $flag2 = “Y” ]
then
echo “欢迎,请输入要猜的数”
read gusNum
if [ $gusNum -gt $ranNum ]
then
echo “猜大了!!!”
elif [ $gusNum -lt $ranNum ]
then
echo “猜小了!!!”
else
echo " 恭喜猜对了!!!"
echo " 本局游戏结束!!!"
fi
fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值