19.1.4 企业Shell面试题4:扫描网络内存活主机案例
写一个Shell脚本,判断10.0.0.0/24网络里,当前在线的IP有哪些?
解答:方法一
. /etc/init.d/functions
for n in {1..254}
do
m=`nmap 10.0.0.$n|grep open|wc -l`
if [ $m -ne 0 ]
then
action "10.0.0.${n}" /bin/true
else
action "10.0.0.${n}" /bin/false
fi
done
方法二
P="ping -W 2 -c 2"
ip="10.0.0."
for n in `seq 254`
do
$P $ip$n &>/dev/null
if [ $? -eq 0 ];then
echo $ip$n is ok
else
echo $ip$n is no
fi
done
方法三
#!/bin/bash
. /etc/init.d/functions
for i in {0..254}
do
{
ping -W 2 -c 2 10.0.0.$i &>/dev/null
if [ $? -eq 0 ]
then
action "10.0.0.$i is ok." /bin/true
fi
}&
done
方法四
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
for ip in `seq 254`
do
{
ping -w 2 -c 2 10.0.0.$ip &>/dev/null
if [ $? -eq 0 ];then
echo "10.0.0.$ip is ok"
# action 10.0.0.$ip /bin/true
fi
}&
done
方法五
p="ping -W 2 -c 2"
ip="10.0.0."
for n in $(seq 254)
do
$p $ip$n &>/dev/null
if [ $? -eq 0 ];then
echo "$ip$n is ok"
fi
done
方法六
#!/bin/sh
CMD="ping -W 2 -c 2"
Ip="10.0.0."
for n in $(seq 254)
do
{
$CMD $Ip$n &> /dev/null
if [ $? -eq 0 ]
then
echo "$Ip$n is ok"
fi
}&
done
方法七
#!/bin/bash
ip="10.0.0."
export LANG=zh_CN.UTF-8
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
for n in `seq 1 255`
do
{
ping -W 1 -c 1 $ip$n &>/dev/null
if [ $? -eq 0 ]
then
action "$ip$n 可以的" /bin/true
fi
}&
done
方法八
nap_ip=$(nmap 10.0.0.0/24 |grep "scan"|egrep -v "0.1|0.254|\)" |awk '{print $NF}')
host_ip=$(ifconfig eth0 |awk 'NR==2{print $2}')
for n in $nap_ip
do
echo $n is ok
done
echo "$host_ip is ok"
方法九
command="ping -W 2 -c 2 "
IP=10.0.0.
if [ -f /tmp/IP.log ]
then
>/tmp/IP.log
else
""
fi
for num in `seq 254`
do
{
${command} ${IP}${num} &>/dev/null
if [ $? -eq 0 ]
then
echo "${IP}${num} is OK." >>/tmp/IP.log
fi
}&
done
方法十
for n in {1..254}
do
ping -W 2 -c 2 10.0.0.$n &>/dev/null
if [ $? -eq 0 ]
then
echo "10.0.0.$n is alive."
else
echo "10.0.0.$n is dead."
fi
done
for n in {1..254}
do
{
ping -W 2 -c 2 10.0.0.$n &>/dev/null
if [ $? -eq 0 ]
then
echo "10.0.0.$n is alive."
else
echo "10.0.0.$n is dead."
fi
}&
done
方法十一
[root@web01 scripts]# nmap -sP 10.0.0.0/24|awk '/Nmap scan report for/ {print $NF}'
10.0.0.1
10.0.0.8
10.0.0.9
10.0.0.61
10.0.0.254
(10.0.0.7)
十二、参数解释
-sP: Ping Scan - go no further than determining if host is online
19.1.14 企业Shell面试题14:开发脚本入侵检测与报警案例
面试及实战考试题:监控web站点目录(/var/html/www)下所有文件是否被恶意篡改(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次。
解答:
RETVAL=0
export LANG=en
CHECK_DIR=/var/html/www
[ -e $CHECK_DIR ] || exit 1
zhiWenDbOri="/opt/zhiwen.db.ori"
FileCountDbOri="/opt/wenjian.db.ori"
ErrLog="/opt/err.log"
[ -e $zhiWenDbOri ] || exit 1
[ -e $FileCountDbOri ] || exit 1
echo "md5sum -c --quiet /opt/zhiwen.db.ori">$ErrLog
md5sum -c --quiet /opt/zhiwen.db.ori &>>$ErrLog
RETVAL=$?
find $CHECK_DIR -type f >/opt/wenjian.db_curr.ori
echo "diff /opt/wenjian.db*" &>>$ErrLog
diff /opt/wenjian.db* &>>$ErrLog
if [ $RETVAL -ne 0 -o `diff /opt/wenjian.db*|wc -l` -ne 0 ]
then
mail -s "`uname -n` $(date +%F) err" 760342663@qq.com <$ErrLog
else
echo "Sites dir is ok"|mail -s "`uname -n` $(date +%F) is ok" 760342663@qq.com
fi
19.1.20 企业Shell面试题20:单词及字母去重排序案例
用shell处理以下内容
1、按单词出现频率降序排序!
2、按字母出现频率降序排序!
the squid project provides a number of resources to assist users design,implement and support squid installations. Please browse the documentation and support sections for more infomation,by oldboy training.
课堂实战考察某企业shell面试考试题
方法一
tr "[,. ]" "\n" <test.txt |grep -v "^$"|sort|uniq -c|sort -nr
方法二
grep -o "[^ ,.]" test.txt |sort|uniq -c|sort -nr -k1
方法三
word=(
the squid project provides a number of resources to assist users design implement and support squid installat
ions Please browse the documentation and support sections for more infomation
)
for n in ${word[*]}
do
echo $n >>/tmp/19_20.txt &>/dev/null
done
echo "按单词出现的频率"
cat /tmp/19_20.txt |sort|uniq -c|sort -rn
echo "按字母出现的频率"
cat /tmp/19_20.txt |sed -r 's#(.)#\1\n#g' |sed '/^$/d' |sort |uniq -c|sort -rn
方法四
[ -f ar.txt ]&& > ar.txt||touch ar.txt
arr=(`sed -n 's#[,\.]# #gp' oldboy.txt`)
for i in ${arr[*]}
do
echo $i >>ar.txt
done
cat ar.txt|sort|uniq -c |sort -nr
方法五
tr "[ ,.]" "\n" <20.txt |grep -v "^$"|sort|uniq -c|sort -nr
方法六
grep -o "[^ ,.]" 20.txt |sort|uniq -c|sort -nr
方法七
tr " ,." "\n" <oldboy.txt|sort|uniq -c|sort -rn
方法八
awk '{S[$1]++}END{for(key in S) print S[key],key}' new.txt|sort -rn
方法九
tr " ,." "\n" <oldboy.txt|awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -rn
方法十
cat netstat.log|awk -F "[ ]+" 'NR>1{print $6}'|awk '{S[$1]++}END{for (key in S) print S[key],key}'|sort -rn
方法十一
awk '{S[$6]++}END{for (key in S) print S[key],key}' netstat.log |sort -nr
方法十二
awk '{S[$6]++}END{for(k in S) print S[k],k}' netstat.log |sort -rn
方法十三
awk 'NR>2{print $NF}' netstat.log|sort|uniq -c|sort -nr
方法十四
cat netstat.log|awk '{S[$6]++}END{for(key in S) print S[key],key}' netstat.log|sort -rn
方法十五
awk '{S[$NF]++}END{for(key in S)print S[key],key }' netstat.log|sort -rn
awk '{print $NF}' netstat.log|sort|uniq -c|sort -rn
tr " ,." "\n" <oldboy.txt|sort|uniq -c|sort -rn|head -5
tr " ,." "\n" <oldboy.txt|awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -rn|head -5
awk -F "[, .]+" '{for(i=1;i<=NF;i++)S[$i]++}END{for(key in S) print S[key],key}' oldboy.txt|sort -rn|head -5
tr " ,." "\n" <oldboy.txt|awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -rn|head -5
tr " ,." "\n" <oldboy.txt|sort|uniq -c|sort -rn|head -5
awk '{for(i=1;i<=NF;i++)S[$i]++}END{for(key in S) print S[key],key}' ip.log|sort -rn
awk -F "[, .]+" '{for(i=1;i<=NF;i++)S[$i]++}END{for(key in S) print S[key],key}' oldboy.txt|sort -rn|head -5
tr " ,." "\n" <oldboy.txt|awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -rn|head -5
tr " ,." "\n" <oldboy.txt|sort|uniq -c|sort -rn|head -5
awk -F "" '{for(i=1;i<=NF;i++)array[$i]++}END{for(key in array)print array[key],key|"sort -nr"}' oldboy.txt|head -5
awk -F "[, .]+" '{for(i=1;i<=NF;i++)S[$i]++}END{for(key in S) print S[key],key|"sort -rn"}' oldboy.txt|head -5
awk -F "" '{for(i=1;i<=NF;i++)array[$i]++}END{for(key in array)print array[key],key|"sort -nr"}' oldboy.txt|head -5
grep -o "[^ ]" oldboy.txt|sort|uniq -c|sort -rn|head -5
grep -o "[^ ]" oldboy.txt|awk '{S[$1]++}END{for(key in S)print S[key],key|"sort -rn"}'|head -5
19.1.28 企业Shell面试题28:51CTO博文爬虫案例
获取51CTO博客列表倒序排序考试题
老男孩教育培训机构需求:需求入下:
请把http://oldboy.blog.51cto.com 地址中的所有博文,按照时间倒序列表如下:
2013-09-13 运维就是一场没有硝烟的战争
http://oldboy.blog.51cto.com/2561410/1296694
2016-04-17 运维人员写项目方案及推进项目的基本流程思路
http://oldboy.blog.51cto.com/2561410/1764820
附加:高级要求:
生成html页面,并设置超链接。
结果如下:
http://oldboy.blog.51cto.com/2561410/1862041
http://oldboy.blog.51cto.com/2561410/1686891
方法一
Url="https://blog.51cto.com/oldboy/p"
Html_list="/server/scripts/shell-19/oldboy_blog.html"
for n in `seq 31`
do
curl -s "$Url$n"|grep -A 5 "time fl"|sed '/^.*zan fr.*/d'>>$Html_list
done
方法二
#!/bin/bash
url=https://blog.51cto.com/oldboy/p
site=/html/oldboy/
[ -d $site ] || mkdir -p $site
echo -e "<h1>oldboy文章html整理</h1><br>" >$site/oldboy.html
for i in `seq 31`
do
curl -s ${url}$i | grep '<a class="tit" href="https://blog.51cto.com/oldboy\|发布于:' -A 1 | grep -v 'zan fr'|sed -r 's#<a href="javascript:;" class="time fl">(.*)</a>#\1#g' | sed -r 's#<span class=.*</span>##g'|sed 's#--##g'| sed 's#发布于#<br>发布于#g' >> $site/oldboy.html
done