第八周作业

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

#!/bin/bash
for i in {1..254};do
        if ping -c 1 -w 1 192.168.1.$i &> /dev/null;then
                echo -e "\033[32m192.168.1.$i\033[0m is up"
        else
                echo -e "\033[31m192.168.1.$i\033[0m is down"
        fi
done


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

1.使用ifcfg工具来配置

[root@localhost ~]# ifconfig                                                       #先查看下本地网络接口,以ens36为例进行配置多个地址
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.22  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::e4e8:fedc:3ce4:f467  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ca:aa:a0  txqueuelen 1000  (Ethernet)
        RX packets 258664  bytes 16503651 (15.7 MiB)
        RX errors 0  dropped 41  overruns 0  frame 0
        TX packets 21160  bytes 2557022 (2.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.25  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::e9d2:3bcd:cc12:fcd6  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ca:aa:aa  txqueuelen 1000  (Ethernet)
        RX packets 128498  bytes 8532781 (8.1 MiB)
        RX errors 0  dropped 7653  overruns 0  frame 0
        TX packets 17151  bytes 2660221 (2.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 210  bytes 18936 (18.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 210  bytes 18936 (18.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ee:0e:1c  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# ifconfig  ens36:0 192.168.1.101/24                              #ifconfig进行配置多地址时需要在接口号后面加上:#来进行配置多个地址
[root@localhost ~]# ifconfig                                                        #再次查看本地网络接口时出现一个名为ens36:0的接口号,这个为ens36的第2个网络地址
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.22  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::e4e8:fedc:3ce4:f467  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ca:aa:a0  txqueuelen 1000  (Ethernet)
        RX packets 259063  bytes 16528702 (15.7 MiB)
        RX errors 0  dropped 41  overruns 0  frame 0
        TX packets 21174  bytes 2558222 (2.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.25  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::e9d2:3bcd:cc12:fcd6  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ca:aa:aa  txqueuelen 1000  (Ethernet)
        RX packets 129198  bytes 8583799 (8.1 MiB)
        RX errors 0  dropped 7653  overruns 0  frame 0
        TX packets 17375  bytes 2687139 (2.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens36:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.101  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:0c:29:ca:aa:aa  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 210  bytes 18936 (18.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 210  bytes 18936 (18.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ee:0e:1c  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2.使用ip命令进行配置

[root@localhost ~]# ip addr list                                                                          #使用ip addr list来查看本地接口配置,依旧以ens36为例进行配置
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ca:aa:a0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.22/24 brd 192.168.1.255 scope global dynamic ens33
       valid_lft 85503sec preferred_lft 85503sec
    inet6 fe80::e4e8:fedc:3ce4:f467/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:ee:0e:1c brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:ee:0e:1c brd ff:ff:ff:ff:ff:ff
5: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ca:aa:aa brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.25/24 brd 192.168.1.255 scope global dynamic ens36
       valid_lft 55915sec preferred_lft 55915sec
    inet 192.168.1.101/24 brd 192.168.1.255 scope global secondary ens36:0                            #此为刚才ifconfig命令配置的地址
       valid_lft forever preferred_lft forever
    inet6 fe80::e9d2:3bcd:cc12:fcd6/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost ~]# ip addr add 192.168.1.102/24 dev ens36                                             #使用 ip addr add命令为ens36添加地址为192.168.1.102/24的地址
[root@localhost ~]# ip addr lis                                                                        #配置完毕再次查看接口
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ca:aa:a0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.22/24 brd 192.168.1.255 scope global dynamic ens33
       valid_lft 85418sec preferred_lft 85418sec
    inet6 fe80::e4e8:fedc:3ce4:f467/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:ee:0e:1c brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:ee:0e:1c brd ff:ff:ff:ff:ff:ff
5: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ca:aa:aa brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.25/24 brd 192.168.1.255 scope global dynamic ens36
       valid_lft 55830sec preferred_lft 55830sec
    inet 192.168.1.101/24 brd 192.168.1.255 scope global secondary ens36:0
       valid_lft forever preferred_lft forever
    inet 192.168.1.102/24 scope global secondary ens36                                               #此为是用ip命令为此接口配置的第3条地址
       valid_lft forever preferred_lft forever
    inet6 fe80::e9d2:3bcd:cc12:fcd6/64 scope link 
       valid_lft forever preferred_lft forever

注意:以上2条命令为通过命令直接送至内核当场有效,重启后无效,若要永久有效需通过修改配置文件来实现。

3.通过修改配置文件进行配置

网络配置的配置文件在/etc/sysconfig/network-scripts/下以ifcfg-IFACE名明的文件。

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls                                                                                           #此处任然以ifcfg-ens36为例,由于是命令行配置的地址所以没有配置文件需要手动去创建配置文件
ifcfg-ens33  ifdown-ippp    ifdown-sit       ifup-bnep  ifup-plip    ifup-Team          network-functions-ipv6
ifcfg-lo     ifdown-ipv6    ifdown-Team      ifup-eth   ifup-plusb   ifup-TeamPort
ifdown       ifdown-isdn    ifdown-TeamPort  ifup-ib    ifup-post    ifup-tunnel
ifdown-bnep  ifdown-post    ifdown-tunnel    ifup-ippp  ifup-ppp     ifup-wireless
ifdown-eth   ifdown-ppp     ifup             ifup-ipv6  ifup-routes  init.ipv6-global
ifdown-ib    ifdown-routes  ifup-aliases     ifup-isdn  ifup-sit     network-functions

[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36                                                                  #复制ens33的配置文件命名为ens36然后进行手动配置
[root@localhost network-scripts]# ls
ifcfg-ens33  ifdown-ib    ifdown-routes    ifup-aliases  ifup-isdn    ifup-sit          network-functions
ifcfg-ens36  ifdown-ippp  ifdown-sit       ifup-bnep     ifup-plip    ifup-Team         network-functions-ipv6
ifcfg-lo     ifdown-ipv6  ifdown-Team      ifup-eth      ifup-plusb   ifup-TeamPort
ifdown       ifdown-isdn  ifdown-TeamPort  ifup-ib       ifup-post    ifup-tunnel
ifdown-bnep  ifdown-post  ifdown-tunnel    ifup-ippp     ifup-ppp     ifup-wireless
ifdown-eth   ifdown-ppp   ifup             ifup-ipv6     ifup-routes  init.ipv6-global

使用vim编辑ifcfg-ens36这个文件

TYPE="Ethernet"
BOOTPROTO="static"
IPADDR="192.168.2.100"
NETMASK="255.255.255.0"
GATEWAY="192.168.2.1"
NAME="ens36"
DEVICE="ens36"
ONBOOT="yes"
~                        

配置完成后保存退出,由于是通过配置文件进行修改,此时需要重启网络服务

[root@localhost network-scripts]# systemctl restart network                          #重启网络服务
[root@localhost network-scripts]# ifconfig                                           #再次查看网络接口
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.22  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::e4e8:fedc:3ce4:f467  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ca:aa:a0  txqueuelen 1000  (Ethernet)
        RX packets 268920  bytes 17141298 (16.3 MiB)
        RX errors 0  dropped 42  overruns 0  frame 0
        TX packets 21373  bytes 2579314 (2.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.100  netmask 255.255.255.0  broadcast 192.168.2.255          #此时接口地址已经变为配置文件所修改的内容
        inet6 fe80::20c:29ff:feca:aaaa  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ca:aa:aa  txqueuelen 1000  (Ethernet)
        RX packets 140651  bytes 9339322 (8.9 MiB)
        RX errors 0  dropped 7666  overruns 0  frame 0
        TX packets 19594  bytes 2914428 (2.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 262  bytes 23376 (22.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 262  bytes 23376 (22.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ee:0e:1c  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

再此需要对此接口进行配置网络地址时,需要复制原配置文件并把配置文件名字改为原名字后加上:#的格式,然后使用vim对配置文件内容进行修改

[root@localhost network-scripts]# cp ifcfg-ens36 ifcfg-ens36:0                     #复制原文件改名
[root@localhost network-scripts]# vim ifcfg-ens36:0                                #编辑此配置文件

TYPE="Ethernet"
BOOTPROTO="static"
IPADDR="192.168.2.105"
NETMASK="255.255.255.0"
GATEWAY="192.168.2.1"
NAME="ens36:0"
DEVICE="ens36:0"
ONBOOT="yes"
~               
[root@localhost network-scripts]# systemctl restart network                          #重启网络服务
[root@localhost network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.22  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::e4e8:fedc:3ce4:f467  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ca:aa:a0  txqueuelen 1000  (Ethernet)
        RX packets 269861  bytes 17214441 (16.4 MiB)
        RX errors 0  dropped 42  overruns 0  frame 0
        TX packets 21709  bytes 2627749 (2.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.100  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::20c:29ff:feca:aaaa  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ca:aa:aa  txqueuelen 1000  (Ethernet)
        RX packets 141164  bytes 9373345 (8.9 MiB)
        RX errors 0  dropped 7821  overruns 0  frame 0
        TX packets 19629  bytes 2919336 (2.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens36:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500                    #此时出现了ens36接口上的第二个网络地址
        inet 192.168.2.105  netmask 255.255.255.0  broadcast 192.168.2.255
        ether 00:0c:29:ca:aa:aa  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 298  bytes 26456 (25.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 298  bytes 26456 (25.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ee:0e:1c  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

4.使用nmcli命令添加地址

[root@localhost network-scripts]# nmcli conn modify ens36 +ipv4.address 192.168.2.201/24        #为ens36显卡添加新地址
[root@localhost network-scripts]# systemctl restart network                                     #重启网路服务
[root@localhost network-scripts]# nmcli dev show ens36
GENERAL.设备:                           ens36
GENERAL.类型:                           ethernet
GENERAL.硬盘:                           00:0C:29:CA:AA:AA
GENERAL.MTU:                            1500
GENERAL.状态:                           100 (连接的)
GENERAL.CONNECTION:                     ens36
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/31
WIRED-PROPERTIES.容器:                  开
IP4.地址[1]:                            192.168.2.100/24
IP4.地址[2]:                            192.168.2.201/24                                       #此处为新添加的网络地址
IP4.地址[3]:                            192.168.2.105/24
IP4.网关:                               192.168.2.1
IP6.地址[1]:                            fe80::20c:29ff:feca:aaaa/64
IP6.网关:                               

5.使用nmtui进行配置

第八周作业第八周作业第八周作业

完成配置后保存退出


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

#!/bin/bash
declare -i n=0;
declare -i m=0;
for FILE in `ls /etc/rc.d/rc3.d`;do
        if [[ $FILE =~ ^K ]];then
                echo "$FILE stop"
                let n=$n+1
        elif [[ $FILE =~ ^S ]];then
                echo "$FILE start"
                let m=$m+1
        fi
done
echo "First char is S = $m , First char is K = $n"

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

#!/bin/bash
declare -i SUM=0
for i in $@;do
        if ! id $i &> /dev/null ; then
                echo "$i is not exists"
        else
                let SUM=$SUM+$(id -u $i);
        fi
done
echo "uid sum = $SUM"
       


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

#!/bin/bash
declare -i dir=0
declare -i filetype=0
for i in $@;do
        if [ -d $i ];then
                for j in $i/*;do
                        file $j &> /dev/null
                        let filetype=$filetype+1
                        if [ -d $j ];then
                                let dir=$dir+1
                        fi

                done
        else
                echo "$i is not a dirfile or not exists"
        fi
done
echo " dir = $dir ,all file = $filetype

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

#!/bin/bash
read -p "please send a username:" UNAME
if id $UNAME &> /dev/null ;then
        if [ $(id -u $UNAME) -gt 500 ];then
                echo "this is a normal user"
        fi
else
        echo "no this user"
fi

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

#!/bin/bash
for i in {20..100};do
        if ping -c 1 -w 1 172.16.250.$i &> /dev/null;then
                echo "172.16.250.$i online"
        fi
done

8、打印九九乘法表;

#!/bin/bash
for i in {1..9};do
        for j in `seq $i`;do
                let num=$j*$i
                echo -n -e "$j*$i=$num\t"
        done
        echo
done

原创文章,作者:M36-Masuri,如若转载,请注明出处:http://www.178linux.com/79365

(0)
M36-MasuriM36-Masuri
上一篇 2017-07-03
下一篇 2017-07-03

相关推荐