shell练习题
来源:互联网 发布:阿里云邮箱前缀怎么写 编辑:程序博客网 时间:2024/06/05 16:34
第一题
ip1 22
ip2 22
ip3 22
ip4 22
ip5 22
以上是iplist.sh 的文件内容
22为ssh端口
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 的文件内容
22为ssh端口
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
- shell练习题
- shell练习题
- shell练习题
- shell练习题,shell基础学习
- Linux Shell上手练习题
- shell脚本练习题
- shell脚本练习题
- Shell编程练习题
- shell脚本练习题
- shell脚本:练习题
- shell 练习题 1-10
- shell 练习题 1-10
- Shell和正则表达式练习题
- shell练习题和数据库基础
- shell脚本练习题(一)
- 【shell编程】 awk 常见练习题整理
- 【shell编程】sed 常见练习题总结
- Shell编程练习题精选及答案
- adb工具(通用的调试工具、debug工具)&操作命令详解
- matlab出现错误: Error using xlsread (line 247)Error: 服务器出现意外情况。
- 7218:献给阿尔吉侬的花束
- 2016 Pacific Northwest Region Programming Contest—Division 2 Problem M
- 题目1019:简单计算器
- shell练习题
- android_Zrevo目录结构
- MapReduce简介
- vector容器中iterator的使用
- 高斯消元
- [10] Linux系统日常运维
- 多行数据输出与处理
- 2016 Pacific Northwest Region Programming Contest—Division 2 Problem R — limit 1 second Equality
- Intel P6以来核心架构及对应型号、芯片组一览表