运维之shell和ip划分面试题

7年前 (2017-07-20) gtj shell, 面试题 0评论 已收录 724℃

1.使用for循环在/touch目录下通过随机小写10个字母加固定字符串test批量创建10个html文件,

-rw-r--r-- 1 root root  477 Jul 20 13:59 touch.sh
[root@sf106232 mianshi1]# cat touch.sh 
#!/bin/bash
#*****************************************************
#         Author: suixiaofeng
#           blog:https://bk.devopstack.cn
#          Email: 258818040@qq.com 
#  Last modified: 2017-07-20 13:55
#       Filename: touch.sh
#    Description: 
#****************************************************
path=./touch
[ -d "$path" ] || mkdir $path
for((i=0;i<10;i++))
do 

 random=`echo $RANDOM|md5sum|sed 's#[^a-z]##g'|cut -c 1-10`
 touch $path/${random}_test.html
done 

2.批量改名。

gtj_1223.html
rename  gtj ggg gtj_1223.html 
[root@db01 shell30]# cat 3.sh 
#!/bin/bash 
for i in /oldboy/* do rename oldboy. oldgir. $i|echo oldgirlok done for x in /oldboy/* do rename html HTML $x|echo htmlok done 

3. 写一个脚本解决DOS攻击生产案例

#!/bin/sh

[ -f /etc/init.d/functions ] && . /etc/init.d/functions
IP_file="/server/scripts/ddos.txt"
IP_filter_command="iptables -I INPUT -j DROP -s"
IP_recover_command="iptables -D INPUT -j DROP -s"
function IP_check(){
  grep "EST"  ${IP_file}|awk -F "[ |:]+" '{print $6}'|sort |uniq -c|sort -rn -k1 > /server/scripts/ip.txt
}
function IP_filter(){
   exec < /server/scripts/ip.txt
   while read line
   do
     IP_count=`echo $line|awk '{print $1}'`
     IP=`echo $line|awk '{print $2}'`
     IP_fil=`iptables -L -n|grep "\b${IP}\b"|wc -l`
     if [ ${IP_count} -gt 25 -a ${IP_fil} -eq 0 ];then
        ${IP_filter_command} ${IP}
        echo "${IP}" >> /server/scripts/ip_filtered.txt
        action "Filter ${IP}" /bin/true
     fi
   done
}
function IP_recover(){
   exec < /server/scripts/ip.txt
   while read line
   do
     IP_count=`echo $line|awk '{print $1}'`
     IP=`echo $line|awk '{print $2}'`
     IP_fil=`iptables -L -n|grep "\b${IP}\b"|wc -l`
     if [ ${IP_count} -le 25 -a ${IP_fil} -eq 1 ];then
        ${IP_recover_command} ${IP}
        echo "${IP}" >> /server/scripts/ip_filtered.txt
        action "Recover ${IP}" /bin/true
     fi
   done
}
function main(){
    case "$1" in
      filter)
      IP_check
      echo "$(date +%F-%H:%M:%S) filtered by $(whoami)" >> /server/scripts/ip_filtered.txt
      IP_filter
      ;;
      recover)
      IP_check
      echo "$(date +%F-%H:%M:%S) recovered by $(whoami)" >> /server/scripts/ip_filtered.txt
      IP_recover
      ;;
      *)
      echo "USAGE:$0 {filter|recover}"
      exit 1
    esac
}
main $*

4.1、执行脚本后,想去的同学输入英文名字全拼,产生随机数01-99之间的数字,数字越大就去参加项目实践,前面已经抓到的数字,下次不能在出现相同数字。
 2、第一个输入名字后,屏幕输出信息,并将名字和数字记录到文件里,程序不能退出继续等待别的学生输入。

#!/bin/sh
> /tmp/temp.txt
input(){
while true
do
  read -p "pls input your name:" name
  if [ -z $name ]
  then
    continue
  elif [ $name == "0" ]
  then
    break
  fi
  rand=$((RANDOM%100))
  echo -e $rand"\t"$name >>/tmp/temp.txt
done
}
output(){
cat /tmp/temp.txt |sort -n -k1 -r|sed '3a#################'
}
main(){
input
output
}
main

5.用shell处理以下内容
1、按单词出现频率降序排序!
2、按字母出现频率降序排序!

the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation
#!/bin/sh
str="the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation"
words(){
echo $str|sed 's#[^a-zA-Z]# #g'|tr " " "\n"|grep -v "^$"|sort|uniq -c|sort -r -n
}
letters(){
echo $str|grep -o "."|sort|egrep -v " |^$|[^a-zA-Z]"|uniq -c|sort -n -r
}
case $1 in
words)
  words
  ;;
letters)
  letters
  ;;
*)
  echo "usage:$0 {words|letters}"
esac

6.手工开发ipvsadm管理lvs的脚本ip_vs

#!/bin/bash
if [ $UID -ne 0 ];then
  echo "Permission denied (you must be root)"
  exit 1
fi
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
vip_netmask=10.0.0.3/24
vip=10.0.0.3
service_addr=10.0.0.3:80
rs=(
10.0.0.7:80
10.0.0.8:80
)
start() {
    #add vip
    ifconfig|grep $vip &>/dev/null
    if [ $? -ne 0 ];then
      ip addr add $vip_netmask dev eth0 label eth0:0 && \
      action "add vip $vip_netmask" /bin/true
    else
      echo "vip $vip_netmask already exists."
    fi
    lvs_table=$(ipvsadm -ln|grep "$vip"|wc -l) 
    if [ $lvs_table -eq 1 ];then
      echo "virtual server already exists."
    else
      #add virtual server
      ipvsadm -A -t $service_addr -s wrr && \
      action "add virtual server $service_addr" /bin/true
    fi
    for ip in ${rs[@]};do
      rs_num=$(ipvsadm -ln|grep "$ip"|wc -l)
      if [ $rs_num -eq 1 ];then
        echo "real server $ip already exists."
      else
        #add real server
        ipvsadm -a -t $service_addr -r $ip -g -w 1 && \
        action "add real server $ip" /bin/true
      fi
    done
    #set tcp tcpfin udp connection timeout
    ipvsadm --set 30 5 60 && \
    action "set tcp tcpfin udp connection timeout values." /bin/true
}
stop() {
    ifconfig|grep $vip &>/dev/null
    if [ $? -ne 0 ];then
      echo "without vip $vip"
    else
      #delete vip 
      ip addr del $vip_netmask dev eth0 label eth0:0 && \
      action "delete vip $vip_netmask." /bin/true
    fi
    lvs_table=$(ipvsadm -ln|grep "$vip"|wc -l)
    for ip in ${rs[@]};do
      rs_num=$(ipvsadm -ln|grep "$ip"|wc -l)
      let lvs_table+=rs_num
    done
    if [ $lvs_table -ge 1 ];then
      #clear all table
      ipvsadm -C && \
      action "clear all lvs table." /bin/true
    else
      echo "lvs table is empty."
    fi
}
case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    stop
    sleep 2
    start
    ;;
    *)
    echo "USAGE: $0 {start|stop|restart}"
esac

7.开发LVS客户端设置VIP以及抑制ARP的管理脚本 实现:/etc/init.d/lvsclient {start|stop|restart}

#!/bin/bash
#chkconfig: 2345 37 57
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#Step 1
function L_ipadd() {
  VIPRES=`ip addr|grep 10.0.0.12|wc -l`
  if [ $VIPRES -eq 0 ]
    then
      ip addr add 10.0.0.12/32 dev lo label lo:12
      route add -host 10.0.0.12 dev lo
      sleep 1
      VIPRES=`ip addr|grep 10.0.0.12|wc -l`
      if [ $VIPRES -eq 0 ]
        then
          ip addr add 10.0.0.12/32 dev lo label lo:12
          route add -host 10.0.0.12 dev lo
          sleep 1
          VIPRES=`ip addr|grep 10.0.0.12|wc -l`
          if [ $VIPRES -eq 0 ]
            then
              action "ip addr add 10.0.0.12/32 ..."   /bin/false
              exit 1
            else
              action "ip addr add 10.0.0.12/32 ..."   /bin/true
          fi
        else
          action "ip addr add 10.0.0.12/32 ..."   /bin/true
      fi
    else
      action "ip addr add 10.0.0.12/32 is exist."
  fi
  echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
  echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
  echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
  echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
}
function L_ipdel() {
  VIPRES=`ip addr|grep 10.0.0.12|wc -l`
  if [ $VIPRES -ne 0 ]
    then
      ip addr del 10.0.0.12/32 dev lo
      route del -host 10.0.0.12 dev lo
      sleep 1
      VIPRES=`ip addr|grep 10.0.0.12|wc -l`
      if [ $VIPRES -ne 0 ]
        then
          action "ip addr del 10.0.0.12/32 ..."  /bin/false
          exit 1
        else
          action "ip addr del 10.0.0.12/32 ..."  /bin/true
      fi
    else
          action "ip addr del 10.0.0.12/32 ..."   /bin/true
  fi
  echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore
  echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce
  echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore
  echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
}
#Step 2
function L_exec() {
  case "$1" in
    start)
      L_ipadd
    ;;
    stop)
      L_ipdel
    ;;
    status)
      VIPRES=`ip addr|grep 10.0.0.12|wc -l`
      sleep 1
      if [ $VIPRES -ne 0 ]
        then
          echo "LVS Nginx is working"
        else
          echo "LVS Nginx is not working"
      fi
    ;;
    restart)
      VIPRES=`ip addr|grep 10.0.0.12|wc -l`
      if [ $VIPRES -ne 0 ]
        then
          L_ipdel
          sleep 1
          L_ipadd
        else
          echo  "ip 10.0.0.12 is not working"
          sleep 1
          L_ipadd
      fi
    ;;
    *)
      echo "USAGE: $0 {start|stop|restart|status}"
  esac
}

8.公司网络地址的划分

本例通过子网数来划分子网,未考虑主机数。
一家集团公司有12家子公司,每家子公司又有4个部门。上级给出一个
172.16.0.0/16的网段,让给每家子公司以及子公司的部门分配网段。
思路:既然有12家子公司,那么就要划分12个子网段,但是每家子公司又有
4个部门,因此又要在每家子公司所属的网段中划分4个子网分配给各部门。
步骤:
A. 先划分各子公司的所属网段。
有12家子公司,那么就有2的n次方≥12,n的最小值=4。因此,网络位需要
向主机位借4位。那么就可以从172.16.0.0/16这个大网段中划出2的4次方=16
个子网。
详细过程:
先将172.16.0.0/16用二进制表示
10101100.00010000.00000000.00000000/16
借4位后(可划分出16个子网):
1) 10101100.00010000.00000000.00000000/20【172.16.0.0/20】
2) 10101100.00010000.00010000.00000000/20【172.16.16.0/20】
3) 10101100.00010000.00100000.00000000/20【172.16.32.0/20】
4) 10101100.00010000.00110000.00000000/20【172.16.48.0/20】
5) 10101100.00010000.01000000.00000000/20【172.16.64.0/20】
6) 10101100.00010000.01010000.00000000/20【172.16.80.0/20】
7) 10101100.00010000.01100000.00000000/20【172.16.96.0/20】
8) 10101100.00010000.01110000.00000000/20【172.16.112.0/20】
9) 10101100.00010000.10000000.00000000/20【172.16.128.0/20】
10) 10101100.00010000.10010000.00000000/20【172.16.144.0/20】
11) 10101100.00010000.10100000.00000000/20【172.16.160.0/20】
12) 10101100.00010000.10110000.00000000/20【172.16.176.0/20】
13) 10101100.00010000.11000000.00000000/20【172.16.192.0/20】
14) 10101100.00010000.11010000.00000000/20【172.16.208.0/20】
15) 10101100.00010000.11100000.00000000/20【172.16.224.0/20】
16) 10101100.00010000.11110000.00000000/20【172.16.240.0/20】
我们从这16个子网中选择12个即可,就将前12个分给下面的各子公司。每个
子公司最多容纳主机数目为2的12次方-2=4094。
B. 再划分子公司各部门的所属网段
以甲公司获得172.16.0.0/20为例,其他子公司的部门网段划分同甲公
司。
有4个部门,那么就有2的n次方≥4,n的最小值=2。因此,网络位需要向主机
位借2位。那么就可以从172.16.0.0/20这个网段中再划出2的2次方=4个子网,
正符合要求。
详细过程:
先将172.16.0.0/20用二进制表示
10101100.00010000.00000000.00000000/20
借2位后(可划分出4个子网):
① 10101100.00010000.00000000.00000000/22【172.16.0.0/22】
② 10101100.00010000.00000100.00000000/22【172.16.4.0/22】
③ 10101100.00010000.00001000.00000000/22【172.16.8.0/22】
④ 10101100.00010000.00001100.00000000/22【172.16.12.0/22】
将这4个网段分给甲公司的4个部门即可。每个部门最多容纳主机数目为2的
10次方-2=1024。

9.本例通过计算主机数来划分子网。

某集团公司给下属子公司甲分配了一段IP地址192.168.5.0/24,现在甲公
司有两层办公楼(1楼和2楼),统一从1楼的路由器上公网。1楼有100台电脑联
网,2楼有53台电脑联网。如果你是该公司的网管,你该怎么去规划这个IP?
根据需求,画出下面这个简单的拓扑。将192.168.5.0/24划成3个网段,1
楼一个网段,至少拥有101个可用IP地址;2楼一个网段,至少拥有54个可用IP地
址;1楼和2楼的路由器互联用一个网段,需要2个IP地址。
思路:我们在划分子网时优先考虑最大主机数来划分。在本例中,我们就先使
用最大主机数来划分子网。101个可用IP地址,那就要保证至少7位的主机位可用
(2的m次方-2≥101,m的最小值=7)。如果保留7位主机位,那就只能划出两个
网段,剩下的一个网段就划不出来了。但是我们剩下的一个网段只需要2个IP地址
并且2楼的网段只需要54个可用IP,因此,我们可以从第一次划出的两个网段中选
择一个网段来继续划分2楼的网段和路由器互联使用的网段。
步骤:
A. 先根据大的主机数需求,划分子网
因为要保证1楼网段至少有101个可用IP地址,所以,主机位要保留至少7
位。
先将192.168.5.0/24用二进制表示:
11000000.10101000.00000101.00000000/24
主机位保留7位,即在现有基础上网络位向主机位借1位(可划分出2个子网):
① 11000000.10101000.00000101.00000000/25【192.168.5.0/25】
② 11000000.10101000.00000101.10000000/25【192.168.5.128/25】
1楼网段从这两个子网段中选择一个即可,我们选择192.168.5.0/25。
2楼网段和路由器互联使用的网段从192.168.5.128/25中再次划分得到。
B. 再划分2楼使用的网段
2楼使用的网段从192.168.5.128/25这个子网段中再次划分子网获得。因
为2楼至少要有54个可用IP地址,所以,主机位至少要保留6位(2的m次方-
2≥54,m的最小值=6)。
先将192.168.5.128/25用二进制表示:
11000000.10101000.00000101.10000000/25
主机位保留6位,即在现有基础上网络位向主机位借1位(可划分出2个子
网):
① 11000000.10101000.00000101.10000000/26【192.168.5.128/26】
② 11000000.10101000.00000101.11000000/26【192.168.5.192/26】
2楼网段从这两个子网段中选择一个即可,我们选择192.168.5.128/26。
路由器互联使用的网段从192.168.5.192/26中再次划分得到。
C. 最后划分路由器互联使用的网段
路由器互联使用的网段从192.168.5.192/26这个子网段中再次划分子网
获得。因为只需要2个可用IP地址,所以,主机位只要保留2位即可(2的m次方-
2≥2,m的最小值=2)。
先将192.168.5.192/26用二进制表示:
11000000.10101000.00000101.11000000/26
主机位保留2位,即在现有基础上网络位向主机位借4位(可划分出16个子
网):
① 11000000.10101000.00000101.11000000/30【192.168.5.192/30】
② 11000000.10101000.00000101.11000100/30【192.168.5.196/30】
③ 11000000.10101000.00000101.11001000/30【192.168.5.200/30】
…………………………………
④ 11000000.10101000.00000101.11110100/30【192.168.5.244/30】
⑤ 11000000.10101000.00000101.11111000/30【192.168.5.248/30】
⑥ 11000000.10101000.00000101.11111100/30【192.168.5.252/30】
路由器互联网段我们从这16个子网中选择一个即可,我们就选择192.168.5.252/30。
D. 整理本例的规划地址
1楼:
网络地址:【192.168.5.0/25】
主机IP地址:【192.168.5.1/25—192.168.5.126/25】
广播地址:【192.168.5.127/25】
2楼:
网络地址:【192.168.5.128/26】
主机IP地址:【192.168.5.129/26—192.168.5.190/26】
广播地址:【192.168.5.191/26】
路由器互联:
网络地址:【192.168.5.252/30】
两个IP地址:【192.168.5.253/30、192.168.5.254/30】
广播地址:【192.168.5.255/30】
博主

这货来去如风,什么鬼都没留下!!!

相关推荐

嗨、骚年、快来消灭0回复。

×
订阅图标按钮
Less is more!!!