homework week08

本周作业内容:

1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。

  • 网桥是早期的两端口二层网络设备,用来连接不同网段。网桥的两个端口分别有一条独立的交换信道,不是共享一条背板总线,可隔离冲突域。后来,网桥被具有更多端口、同时也可隔离冲突域的交换机所取代。
  • 集线器的主要功能是对接收到的信号进行再生整形放大,以扩大网络的传输距离,同时把所有节点集中在以它为中心的节点上。集线器上各端口都是共享同一条背板总线的。不能隔离冲突域。工作在物理层。
  • 二层交换机是指只支持OSI第二层(数据链路层)交换技术的交换机,拥有MAC地址表,根据MAC地址进行转发。
  • 三层交换机就是具有部分路由器功能的交换机。三层交换机的最重要目的是加快大型局域网内部的数据交换,所具有的路由功能也是为这目的服务的,能够做到一次路由,多次转发。对于数据包转发等规律性的过程由硬件高速实现,而像路由信息更新、路由表维护、路由计算、路由确定等功能,由软件实现。传统交换技术是在数据链路层进行操作的,而三层交换技术是在三层实现了数据包的高速转发,既可实现网络路由功能,又可根据不同网络状况做到最优网络性能。
  • 路由器又称网关设备是用于连接多个网络,工作在三层,具有判断网络地址和选择IP路径的功能。

2、IP地址的分类有哪些?子网掩码的表示形式及其作用

  • A类IP地址

A类IP地址由1字节的网络地址和3字节主机地址组成,网络地址的最高位必须是“0”, 地址范围从1.0.0.0 到126.255.255.254。可用的A类网络有126个,每个网络能容纳1亿多个主机。

  • B类IP地址

B类IP地址由2个字节的网络地址和2个字节的主机地址组成,网络地址的最高位必须是“10”,地址范围从128.0.0.0到191.255.255.254。可用的B类网络有16382个,每个网络能容纳6万多个主机。

  • C类IP地址

C类IP地址由3字节的网络地址和1字节的主机地址组成,网络地址的最高位必须是“110”。范围从192.0.0.0到223.255.255.254。C类网络可达209万余个,每个网络能容纳254个主机。

  • D类地址用于多点广播(Multicast)

D类IP地址第一个字节以“lll0”开始,它是一个专门保留的地址。它并不指向特定的网络,目前这一类地址被用在多点广播(Multicast)中。多点广播地址用来一次寻址一组计算机,它标识共享同一协议的一组计算机。D类IP地址不标识网络,地址范围为224.0.0.0-239.255.255.255。

  • E类IP地址

以“llll”开始,为将来使用保留。

全零(“0.0.0.0”)地址对应于当前主机。全“1”的IP地址(“255.255.255.255”)是当前子网的广播地址。

在IP地址3种主要类型里,各保留了3个区域作为私有地址,其地址范围如下:

A类地址:10.0.0.0~10.255.255.254

B类地址:172.16.0.0~172.31.255.254

C类地址:192.168.0.0~192.168.255.254

3、计算机网络的分成模型有哪些(OSI模型和TCP/IP模型),每一层的功能及涉及到的物理设备有哪些。

OSI TCP/IP 物理设备
应用层(Application layer) 应用层
表示层(Presentation layer)
会话层(Session layer)
传输层(Transport layer) 传输层
网络层(Network layer) 网络层 路由器
数据链路层(Data link layer) 网络访问层(Network Access layer) 交换机
物理层(Physical layer) 集线器

4、如何将Linux主机接入到TCP/IP网络,请描述详细的步骤。(手动指定的方式)

[[email protected] network-scripts]# cat ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes   # 开机时启动
NM_CONTROLLED=no
BOOTPROTO=static  # 静态配置
IPv6INIT=no
IPADDR=192.168.1.100  # 指定IP地址
NETMASK=255.255.255.0  # 指定掩码

[[email protected] network-scripts]# service  network restart
Shutting down interface eth0:  [  OK  ]
Shutting down interface eth1:  [  OK  ]
Shutting down loopback interface:  [  OK  ]
Bringing up loopback interface:  [  OK  ]
Bringing up interface eth0:  
Determining IP information for eth0... done.
[  OK  ]
Bringing up interface eth1:  Determining if ip address 192.168.1.100 is already in use for device eth1...
[  OK  ]

[[email protected] network-scripts]# ip add show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:75:60:88 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.100/24 brd 192.168.1.255 scope global eth1
    inet6 fe80::20c:29ff:fe75:6088/64 scope link 
       valid_lft forever preferred_lft forever

#添加路由
[[email protected] ~]# route add -net 192.168.2.0 netmask 255.255.255.0 dev eth1

5、为Linux主机配置网络信息的方式有哪些,请描述各个过程。

配置主机名,重启生效:/etc/sysconfig/network
    NETWORKING=yes  # 如果不为yes,则不启用网络功能
    HOSTNAME=captain  # 主机名。hostname临时修改,重启丢失。

配置IP地址:/etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=dhcp

修改hosts文件: /etc/hosts,用来把主机名字映射到IP地址的方法
    IP_address canonical_hostname [aliases...]

域名服务器配置文件:/etc/resolv.conf,最多可以设置3行,前面的生效
    nameserver IP地址

路由:可以把路由保存在文件,或者使用route add添加
1. 保存在文件:/etc/sysconfig/network-scripts/route-eth1
    192.168.2.0 via 192.168.1.254
2. 使用route add添加
    route add -net 192.168.2.0 netmask 255.255.255.0 dev eth1

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

在线的主机使用绿色显示;

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

[[email protected] shell]# cat practice8_6.sh 
#!/bin/bash
. /home/shell/myfunctions

function pingIP()
{
    local ret;
    ping -c 3 -i 0.1 $1 >/dev/null 2>&1
    ret=$?
    if [ "$ret" -eq 0 ]; then
        echo -e "${Green}$1${EndColor} is up"
    else
        echo -e "${Red}$1${EndColor} is down"
    fi
}
for ip in 192.168.162.{1,2,3,4,134,135}
do
    pingIP $ip
done

[[email protected] shell]# sh practice8_6.sh 
192.168.162.1 is down
192.168.162.2 is up
192.168.162.3 is down
192.168.162.4 is down
192.168.162.134 is up
192.168.162.135 is up

7、详细描述每个网络接口的配置文件中各个参数的含义和其所对应的值;

DEVICE=eth0      # 设备名称
TYPE=Ethernet    # 类型
ONBOOT=yes       # 开机启动
NM_CONTROLLED=no # 禁用NetworkManager
BOOTPROTO=dhcp   # dhcp方式或者IP地址

如果BOOTPROTO=static或者none  # 静态配置IP地址
IPADDR=IP地址
NETMASK=掩码
GATEWAY=网关

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

9、常用的网络管理类工具有哪些,并用示例形式描述他们的使用方法。

10、Linux系统软件包管理方法(安装、升级、卸载等操作)有哪些,以及如何管理的。

11、如何使用发行版光盘作为yum repository,请描述该过程。

12、写一个脚本,完成以下功能

(1) 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干;

(2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串;

(3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串;

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

[[email protected] shell]# cat practice8_12.sh 
#!/bin/bash

readonly workdir=/etc/rc.d/rc3.d
declare -i startswithK=0
declare -i startswithS=0

for filename in `ls -1 $workdir`
do
    case $filename in
    K*)
        echo $filename stop
        let startswithK++
        ;;
    S*)
        echo $filename start
        let startswithS++
        ;;
    *)
        ;;
    esac
done

echo "==========================================="
echo "The number of files that start with K: $startswithK"
echo "The number of files that start with S: $startswithS"

[[email protected] shell]# sh practice8_12.sh 
K01numad stop
K01smartd stop
K02oddjobd stop
K10psacct stop
K10saslauthd stop
K15htcacheclean stop
K15httpd stop
K15mongod stop
K15nginx stop
K15nginx-debug stop
K35dhcpd stop
K35dhcpd6 stop
K35dhcrelay stop
K35dhcrelay6 stop
K35tgtd stop
K50netconsole stop
K50snmpd stop
K50snmptrapd stop
K50vsftpd stop
K60nfs stop
K61nfs-rdma stop
K69rpcsvcgssd stop
K73winbind stop
K75cgconfig stop
K75ntpdate stop
K75quota_nld stop
K76ypbind stop
K86cgred stop
K87named stop
K87restorecond stop
K88sssd stop
K89rdisc stop
K92ip6tables stop
K92iptables stop
K95rdma stop
K99rngd stop
S01sysstat start
S02lvm2-monitor start
S10network start
S11auditd start
S11portreserve start
S12rsyslog start
S13cpuspeed start
S13irqbalance start
S13rpcbind start
S14nfslock start
S15mdmonitor start
S19rpcgssd start
S20kdump start
S22messagebus start
S25blk-availability start
S25cups start
S25netfs start
S26acpid start
S26haldaemon start
S26udev-post start
S28autofs start
S50mcelogd start
S55sshd start
S56xinetd start
S58ntpd start
S80postfix start
S82abrt-ccpp start
S82abrtd start
S90crond start
S95atd start
S99certmonger start
S99local start
===========================================
The number of files that start with K: 36
The number of files that start with S: 32

13、写一个脚本,完成以下功能

(1) 脚本能接受用户名作为参数;

(2) 计算此些用户的ID之和;

[[email protected] shell]# cat practice8_13.sh 
#!/bin/bash
. /home/shell/myfunctions

declare -i total=0
declare -i args=0

if [ "$#" -eq 0 ]; then
    echo "at least one argument!"
    exit 1
else
    args="$#"
fi

while [ "$args" -gt 0 ]
do
    if chkuser $1; then
        let "total += `id -u $1`"
    else
        echo "$1 does not exist, pass!"
    fi
    
    shift
    let args--
done

echo "The sum of ID: $total"

[[email protected] shell]# sh  practice8_13.sh  visitor1 visitor7 visitor16 visitor77
visitor77 does not exist, pass!
The sum of ID: 9093

14、写一个脚本

(1) 传递一些目录给此脚本;

(2) 逐个显示每个目录的所有一级文件或子目录的内容类型;

(3) 统计一共有多少个目录;且一共显示了多少个文件的内容类型;

[[email protected] shell]# cat practice8_14.sh 
#!/bin/bash

if [ "$#" -eq 0 ]; then
    echo "at least one directory"
    exit 1
fi

declare -i dirs=0
declare -i files=0
declare -i idx=0
declare -ir args="$#"
declare -a dirlist

while [ "$idx" -lt "$args" ]
do
    if [ -d "$1" ]; then
        dirlist[$idx]=$1
    fi
    
    shift
    let idx++
done

function listfile(){
    for file in $1/*
    do
        if [ -d $file ]; then
            let dirs++
            echo $file
            listfile $file
        elif [ -f $file ]; then
            let files++
            echo $file
        fi
    done
}

for d in ${dirlist[*]}
do
    listfile ${d%/}
    echo "directories: $dirs"
    echo "files: $files"
    echo "===================="
done

[[email protected] shell]# sh practice8_14.sh /tmp /dev/
/tmp/grub.conf
/tmp/md5_new1.list
/tmp/md5_new.list
/tmp/md5_old.list
/tmp/record
/tmp/srvs
/tmp/sysinit1
/tmp/temp
/tmp/temp/1.sh
/tmp/temp/ac
/tmp/temp/ac/abc
/tmp/temp/ac/file1
/tmp/temp/ac/file2
/tmp/temp/ac/file3
/tmp/temp/ac/file4
/tmp/temp/ac/file5
/tmp/temp/ac/file6
/tmp/temp/ac/file7
/tmp/temp/ad
/tmp/temp/ad/123
/tmp/temp/bc
/tmp/temp/bc/XYZ
/tmp/temp/bd
/tmp/temp/bd/789
/tmp/temp/file10
/tmp/temp/file11
/tmp/temp/file12
/tmp/temp/file13
/tmp/temp/file14
/tmp/temp/file15
/tmp/temp/file16
/tmp/temp/file17
/tmp/temp/file18
/tmp/temp/file19
/tmp/user
/tmp/yum_save_tx-2016-09-19-09-15fmyI1K.yumtx
directories: 5
files: 31
====================
/dev/block
/dev/bsg
/dev/bus
/dev/bus/usb
/dev/bus/usb/001
/dev/bus/usb/002
/dev/bus/usb/003
/dev/bus/usb/004
/dev/char
/dev/core
/dev/cpu
/dev/cpu/0
/dev/cpu/1
/dev/data
/dev/disk
/dev/disk/by-id
/dev/disk/by-label
/dev/disk/by-path
/dev/disk/by-uuid
/dev/fd
/dev/fd/255
/dev/hugepages
/dev/input
/dev/input/by-id
/dev/input/by-path
/dev/MAKEDEV
/dev/mapper
/dev/net
/dev/pts
/dev/raw
/dev/shm
/dev/snd
/dev/snd/by-path
/dev/VolGroup
directories: 36
files: 34
====================

15、写一个脚本

通过命令行传递一个参数给脚本,参数为用户名

如果用户的id号大于等于500,则显示此用户为普通用户;

[[email protected] shell]# cat practice8_14.sh 
#!/bin/bash

. /home/shell/myfunctions

if [ "$#" -ne 1 ]; then
    echo "Usage: `basename $0` username"
    exit 1
fi

if chkuser $1;then
    if [ `id -u $1` -ge 500 ]; then
    echo "normal user"
else
    echo "system user"
    fi
else
    echo "non-existent user"
fi

[[email protected] shell]# sh practice8_14.sh  visitor7
normal user
[[email protected] shell]# sh practice8_14.sh  root
system user
[[email protected] shell]# sh practice8_14.sh  admin
non-existent user

16、写一个脚本

(1) 添加10用户user1-user10;密码同用户名;

(2) 用户不存在时才添加;存在时则跳过;

(3) 最后显示本次共添加了多少用户;

[[email protected] shell]# cat practice8_16.sh 
#!/bin/bash

. /etc/init.d/functions
. /home/shell/myfunctions

declare -i usercnt=0

for user in user{1..10}
do
    if chkuser $user; then
        echo "$user exists!, pass"
    else
        useradd $user >/dev/null 2>&1
        ret=$?
        if [ "$ret" -ne 0 ]; then
            action "add user $user" /bin/false
        else
            echo "$user" | passwd --stdin $user > /dev/null 2>&1
            let usercnt++
            action "add user $user" /bin/true
        fi
    fi
done

echo "$usercnt user(s) added"

[[email protected] shell]# sh practice8_16.sh 
add user user1 [  OK  ]
add user user2 [  OK  ]
add user user3 [  OK  ]
add user user4 [  OK  ]
add user user5 [  OK  ]
add user user6 [  OK  ]
add user user7 [  OK  ]
add user user8 [  OK  ]
add user user9 [  OK  ]
user10 exists!, pass
9 user(s) added

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

略,同第6题。

18、打印九九乘法表;

[[email protected] shell]# cat practice8_18.sh 
#!/bin/bash
for ((i = 1; i <= 9; i++))
{
    for ((j = 1; j <= i; j++))
    {
        let "k = i * j"
        echo -n "${i}x${j}=$k "
    }
    echo
}

[[email protected] shell]# sh practice8_18.sh 
1x1=1 
2x1=2 2x2=4 
3x1=3 3x2=6 3x3=9 
4x1=4 4x2=8 4x3=12 4x4=16 
5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 
6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 
7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 
8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 
9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81
时间: 2024-08-02 08:15:10

homework week08的相关文章

HDU 1789 Doing Homework again(贪心)

Doing Homework again Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadlin

uva 1489 - Math teacher&#39;s homework(数位dp)

题目链接:uva 1489 - Math teacher's homework 题目大意:给定n,k,以及序列m1,m2,-,mn, 要求找到一个长度为n的序列,满足0<=xi<=mi, 并且x1XORx2XOR-XORxn=k 解题思路:数位dp,在网上看了别人的代码,高大上... 假设有二进制数 k : 00001xxxx mi:0001xxxxx, 那么对于xi即可以满足任意的x1XORx2XOR-XORxi?1XORxi+1XOR-XORxn,根据这一点进行数位dp. dp[i][j]

HDU 1074 Doing Homework 状压DP

Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will r

HDU 1789 Doing Homework again

在我上一篇说到的,就是这个,贪心的做法,对比一下就能发现,另一个的扣分会累加而且最后一定是把所有的作业都做了,而这个扣分是一次性的,所以应该是舍弃扣分小的,所以结构体排序后,往前选择一个损失最小的方案直接交换就可以了. #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; struct HomeWork { int de

[2016-03-28][HDU][1074][Doing Homework]

时间:2016-03-28 18:46:36 星期一 题目编号:[2016-03-28][HDU][1074][Doing Homework] 题目大意:给定n门科作业的期限时间和完成耗时,每科每超过一天就扣一份,求最少扣分数 分析:n只有15,二进制枚举,状态压缩,枚举每种科目完成的状态,更新下一个状态,求最小值 #include <cstring> #include <cstdio> using namespace std; const int maxstu = 1 <&

Homework (7th,Mar.)

第一题: 1 /* 2 定义一个水果类(fruit),水果类中有 3 属性:颜色(color).价格(price).重量(weigth), 4 再定义一个<测试类>, 5 创建一个苹果(apple)的对象, 颜色是"红色",价格是5.5,重量10g. 6 然后再创建一个香蕉(banana)的对象,颜色是"黄色",价格是4.2,重量5g. 7 最后输出:苹果的颜色.价格.重量. 8 香蕉的颜色.价格.重量. 9 */ 10 package Homework

hdu 5298 Solid Geometry Homework(几何)

题目链接:hdu 5298 Solid Geometry Homework 每个圈或者是平面将划分出两个区域,每次把一边区域取反即可.最后判断一下是否满足. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int maxn = 3000; typedef long long ll; struct Poi

HDU 1074 Doing Homework(状压dp)

Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6299    Accepted Submission(s): 2708 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a l

HDU1789Doing Homework again(贪婪)

HDU1789Doing Homework again(贪心) 题目链接 题目大意:给你n们作业的最后期限和过了这个期限没做须要扣的分数.问如何安排能够使得扣分最少. 解题思路:贪心,将扣分多的作业排在前面,扣分同样的依照最后期限前的排前面,然后用一个数组来表示第i天是否有安排.每次都将第i个作业放到它的最后期限的那天完毕,但假设这一天被占了,那么就仅仅能往前移动,找空暇的天.假设一直找到了0.那么说明这个作业是无法按时完毕了,就加上分数.假设某项作业完毕的最后期限比n还大,那么这个作业一定是能