N26-博客作业-week8

1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;
     在线的主机使用绿色显示;

     不在线的主使用红色显示;

#!/bin/bash
#
for i in {1..254}; do
    if -W 1 -c 1 ping 172.16.250.$i &> /dev/null; then
        echo -e "\033[32mHost 172.16.250.$i is online.\033[0m"
    else
        echo -e "\033[31mHost 172.16.250.$i is offline.\033[0m"
    fi
done

2、如何给网络接口配置多个地址,有哪些方式?

~]# ip  addr  add  192.168.121.11/24 dev eno16777736:1
~]# ifconfig eth0:1
~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0

3、写一个脚本,完成以下功能
   (1) 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干;
   (2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串;
   (3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串;

   (4) 分别统计S开头和K开头的文件各有多少;

#!/bin/bash
#
declare -i k=0,s=0 

for i in $(ls /etc/rc.d/rc3.d/K* | grep -E -o "[^/]+$"); do
    echo "$i stop"
    let k++
done

for j in $(ls /etc/rc.d/rc3.d/S* | grep -E -o "[^/]+$"); do
    echo "$j start"
    let s++
done

echo "Filename begins with "s": $s"
echo "FIlename begins with "k": $k"

4、写一个脚本,完成以下功能
   (1) 脚本能接受用户名作为参数;
   (2) 计算此些用户的ID之和;

#!/bin/bash
#
declare -i sum=0

if [ $# -lt 1 ];then
    echo "At least one username!"
    exit 1
fi
for user in $@;do
    if id $user &> /dev/null; then
            sum=$[$sum+$(id -u $user)]
    else
            echo "No such user!"
            exit 2
    fi
done

echo "$sum"

5、写一个脚本
   (1) 传递一些目录给此脚本;
   (2) 逐个显示每个目录的所有一级文件或子目录的内容类型;
   (3) 统计一共有多少个目录;且一共显示了多少个文件的内容类型;

#!/bin/bash
#
declare -i dir=0,file=0,dir_all=0,file_all=0

for i in $@;do
    if [ -d $i  ]; then
        for j in $(ls $i);do
            echo "$j"
            if [ -f $i/$j ];then
                let file+=$file
            elif [ -d $i/$j ];then
                let dir+=$dir
            fi
        done
    fi
    file_all=$[$file_all+$file]
    dir_all=$[$dir_all+$dir]
done

echo "File num: $file_all"
echo "Directory num: $dir_all"

6、写一个脚本
  通过命令行传递一个参数给脚本,参数为用户名
  如果用户的id号大于等于500,则显示此用户为普通用户;

#!/bin/bash
#
if [ $# -eq 0 ];then
        echo "At least one username!" 
        exit 1
fi

id=$(id -u $1)

if [ $id -ge 500 ]; then
        echo "login user"
else
        echo "system user"
fi

7、写一脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来;

#!/bin/bash
#
for((i=20;i<=100;i++)); do
    if ping -W 1 -c 1 172.16.250.$i &> /dev/null; then
        echo "172.16.250.$i is online"
    fi
done

8、打印九九乘法表;

#!/bin/bash
#
declare -i j=1
declare -i i=1
while [ $j -le 9 ];do
    while [ $i -le $j ];do
        echo -n -e "${i}X${j}=$[$j*$i]\t"
        let i++
    done
    let j++
    let i=1
    echo
done

原创文章,作者:浙江-咲,如若转载,请注明出处:http://www.178linux.com/71784

(0)
上一篇 2017-03-26 17:07
下一篇 2017-03-26 18:43

相关推荐

  • N25第五周总结:程序包管理

    程序包管理:rpm、yum、编译详解 rpm详解: 一:大纲        1、什么是rpm        2、为什么要使用rpm        3、rpm能实现什么功能 &nbs…

    Linux干货 2017-01-10
  • 8月5日文本处理工具作业

    分析文本工具相关练习题 1 、找出ifconfig 命令结果中本机的所有IPv4 地址       ~]# ifconfig|tr -cs '[0-9].' '\n'|sort -ut '….

    Linux干货 2016-08-06
  • 磁盘分区

      1、创建一个10G分区,并格式化为ext4文件系统;    (1)要求其block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包含acl;   (2)挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳 第一步:创建一个10G的分区     [r…

    Linux干货 2016-12-18
  • HA Cluster 之 vrrp (keepalived)

      HA Cluster 之 vrrp (keepalived) 0x00 前言 0x01 VRRP 协议 VRRP 简介 VRRP 工作原理 VRRP负载分担方式的特点 VRRP 中的一些术语 0x02 keepalived 功用: 0x03 keepalived 配置 HA Cluster 的配置前提: 配置文件组件部分: 配置文件解析: 附:…

    Linux干货 2017-05-16
  • 马哥教育网络班21期-第六周课程练习

    请详细总结vim编辑器的使用并完成以下练习题1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; #cp /etc/rc.d/rc.sysinit /tmp #vim /tmp/rc.sysinit :%s/^[[:space:]]/#…

    Linux干货 2016-08-15
  • 网卡别名与多网卡绑定

    网卡别名 一块网卡可以配置多个ip地址,叫做网卡别名   对虚拟主机有用 格式形如:eth0:1  eth0:2 …… 实现方法:   在命令行中临时设置: ifconfig 命令: ifconfig eth0:0 192.168.1.100/24 演示:   修改配置文件 在/etc/sysconfig/networ…

    Linux干货 2016-09-05

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-30 14:19

    可以看出脚本运用的已经比较熟练了,脚本在手,天下我有,继续加油。