shell练习题

来源:互联网 发布:阿里云邮箱前缀怎么写 编辑:程序博客网 时间:2024/06/05 16:34

第一题

ip1 22

ip2 22

ip3 22

ip4 22

ip5 22

以上是iplist.sh 的文件内容

22ssh端口

ssh ip上执行命令:ls /tmp

 

PORT=22

IP=`cat iplist.sh |awk '{print $2}'`

user=root

for i in $IP

do

        ssh -p $PORT $user@$i  ls /tmp

done

 

 

第二题:

获取本机IP

#检查服务器型号  

#检查硬盘型号

#检查核心数量

#检查内存大小,单位GB

#检查硬盘大小,单位GB

#检查根分区可用大小,单位GB  

#硬盘低于20%时提示警告

#检查网卡类型

#检查网卡驱动

#检查带宽

#检查mysql状态

#服务器时间

#计划任务crond状态

 

IP=`ifconfig eth0 | grep "inet addr" | awk '{print $2}' |sed 's/addr://'`

TYPE=`dmidecode | grep Product`

VERDOR=`cat /proc/scsi/scsi |grep -i "vendor" |cut -d : -f 3 |sed 's/Rev//'`

CPU=`cat /proc/cpuinfo |grep "processor"|wc -l`

MEM=`free -hg |awk 'NR==2{print $2}'`

DISK=`fdisk -l | awk 'NR==2{print $3$4}'`

ROOTDISK=`df -Th |sed -n '/\/$/p' |awk '{print $4}'`

WARNING=`df | sed -n '/\/$/p' | awk '{print $4}' |sed 's/%//'`

NETTYPE=`ethtool eth0 |grep -i "speed" |awk '{print $2}'`

NETDRE=`ethtool -i eth0 |awk 'NR==1{print $2}'`

BANDWITCH=`dmesg |grep eth0 |grep -i "Mbps" |cut -d , -f 1 |sed 's/eth[0-9] NIC Link is Up//'`

MYSQL=`service mysqld status |awk 'NR==1{print $3}'`

DATE=`date +%F:%T`

CROND=`service crond status |awk '{print $5}'`

echo "本机IP是:$IP"

echo "服务器型号为:$TYPE"

echo "硬盘型号为:$VERDOR"

echo "CPU共有 $CPU "

echo "内存大小为:$MEM"

echo "硬盘大小为:$DISK"

echo "根分区大小为:$ROOTDISK"

 

if [ $WARNING -lt 20 ];then

        echo "硬盘Warning>20%"

else

        echo "硬盘正常"

fi

 

echo "网卡类型是:$NETTYPE"

echo "网卡驱动为:$NETDRE"

echo "网卡带宽是:$BANDWITCH"

echo "mysql状态:$MYSQL"

echo "服务器时间:$DATE"

echo "crond状态:$CROND"

 

标准答案:

获取本机IP

ifconfig | awk '/inet addr/{gsub(/:/," ",$0);print $3}'|egrep -v '^192.168.|^172.1[6-9].|^172.2[0-9].|^172.3[0-1].|^10.|^127.'

检查服务器型号

hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.vendor

检查硬盘型号

lshal -s|grep storage|grep -vi DVD|grep -vi CDROM|grep -vi Floppy

检查核心数量

cat /proc/cpuinfo | grep -c processor

检查内存大小,单位GB

cat /proc/meminfo | awk '/MemTotal/{print $2/1024/1024"GB"}'

检查硬盘大小,单位GB

fdisk -l |awk '/Disk/{print $3$4}'|sed 's/,//g'

检查根分区可用大小,单位GB

df -h|egrep /$|awk '{print $(NF-2)}'


 

第三题(附加题)

gatfile ip1 22

gatfile ip2 22

gatfile ip3 22

gatfile ip4 22

gatfile ip5 22

———————

以上是iplist.sh 的文件内容

22ssh端口

ssh ip上执行命令:ls /tmp

提示:gatfile为函数名称

 

执行提示:1

######执行gatfile列表

function gatfile {

......

}

while read line

do

$line

done < iplist.sh

 

 

function gatfile {

        ssh -p $2 root@$1 ls /tmp

}

while read line

do

$line

done < /mnt/test/iplist.sh

 

进阶:多线程

######执行gatfile列表

sh test.sh iplist.sh

 


编写脚本实现将/usr/local/test目录下大于100K文件,将它拷贝到/tmp目录下

#!/bin/bash

for file in `ls /usr/local/test`

do

if [ -f $file ];then

    if [ `ls -l $file`|awk '{print $5}' -gt 10000];then

       mv $file /tmp/

    fi

fi

done


利用shell开发rsync服务启动、停止脚本,并通过chkconfig 进行开关机管理

[root@mysql-1 download]# vi RSstart.sh

#!/bin/sh

##create by mingongge at 2017-01-10

. /etc/init.d/functions

case "$1" in

start)

      rsync --daemon

      if [ $? -eq 0 ];then

        action "rsync is started" /bin/true

     else

        action "rsync is started" /bin/false

     fi

;;

stop)

     pkill rsync

     sleep 2

     if [ `ps -ef|grep rsync|grep -v grep |wc -l` -eq 0 ];then

         action "rsync is stoped " /bin/true

     else

         action "rsync is stoped " /bin/false

     fi

;;

restart)

    pkill rsync

    sleep 2     

    if [ `ps -ef|grep rsync|grep -v grep |wc -l` -eq 0 ];then

       rsync --daemon

       if [ $? -eq 0 ];then

          action "rsync is restarted" /bin/true

       fi

    fi

;;

*)

   echo "USAGE :{start|stop|restart}"

;;

esac








 

原创粉丝点击