; 马哥教育网络班22期+第八周课程练习 | Linux运维部落

马哥教育网络班22期+第八周课程练习

1. 请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 
网桥:工作在OSI模型的数据链路层,将两个局域网连起来,根据MAC地址来转发帧。它可以有效地连接两个LAN,使本地通信限制在本网段内,并转发相应的信号至另一网段,网桥通常用于连接数量不多的、同一类型的网段,它可以扩展网络的距离或范围,而且可提高网络的性能、可靠性和安全性。

集线器:主要功能是对接收到的信号进行再生整形放大,以扩大网络的传输距离,同时把所有节点集中在以它为中心的节点上,它工作于OSI模型中的“物理层”,因为采用“广播”模式,所以很容易产生“广播风暴”,大部份集线器已被交换机取代。

二层交换机: 工作于OSI模型的第2层(数据链路层),故而称为二层交换机。二层交换技术的发展已经比较成熟,二层交换机属数据链路层设备,可以识别数据包中的MAC地址信息,根据MAC地址进行转发,并将这些MAC地址与对应的端口记录在自己内部的一个地址表中,能够隔离冲突域和有效抑制“广播风暴”的产生。

三层交换机:具有部分路由器功能的交换机,三层交换机的最重要目的是加快大型局域网内部的数据交换,所具有的路由功能也是为这目的服务的,能够做到一次路由,多次转发。对于数据包转发等规律性的过程由硬件高速实现,而像路由信息更新、路由表维护、路由计算、路由确定等功能,由软件实现。三层交换技术就是二层交换技术+三层转发技术。传统交换技术是在OSI网络标准模型第二层——数据链路层进行操作的,而三层交换技术是在网络模型中的第三层实现了数据包的高速转发,既可实现网络路由功能,又可根据不同网络状况做到最优网络性能。

路由器:其功能是决定最优路由和转发数据包。路由表中写入各种信息,由路由算法计算出到达目的地址的最佳路径,然后由相对简单直接的转发机制发送数据包。接受数据的下一台路由器依照相同的工作方式继续转发,依次类推,直到数据包到达目的路由器。其工作模式与二层交换机相似,但路由器工作在OSI模型的第三层——网络层,这个区别决定了路由和交换机在传递包时使用不同的控制信息,实现功能的方式就不同。适用于局域网间互联,局域网与城域网互联互通。


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

blob.png


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

blob.png


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


1
<span class="hljs-number">1</span>.编辑/etc/sysconfig/network-scripts/ifcfg-eno16777728,此为centos7系统,名称可能有所不同,centos6系统为ifcfg-eth0<br /><br />~]<span class="hljs-comment"># cat ifcfg-eno16777728 </span><br />TYPE=Ethernet <span class="hljs-comment">#指明接口类型,常见的有Ethernet,Bridge,可忽略不写;</span><br />BOOTPROTO=static <span class="hljs-comment">#激活此接口时使用什么协议来配置接口属性,这里为静态</span><br />IPV6INIT=yes <span class="hljs-comment">#是否初始化IPV6</span><br />NAME=eno16777728<br />DEVICE=eno16777728<br />ONBOOT=yes &nbsp;<span class="hljs-comment">#在系统的引导过程中,是否激活此接口</span><br />IPADDR=<span class="hljs-number">192.168</span>.<span class="hljs-number">137.70</span> <span class="hljs-comment">#IP地址</span><br />NETMASK=<span class="hljs-number">255.255</span>.<span class="hljs-number">255.0</span> <span class="hljs-comment">#掩码</span><br />GATEWAY=<span class="hljs-number">192.168</span>.<span class="hljs-number">137.1</span> <span class="hljs-comment">#网关</span><br />DNS1=<span class="hljs-number">210.21</span>.<span class="hljs-number">196.6</span> <span class="hljs-comment">#主DNS</span><br />DNS2=<span class="hljs-number">119.233</span>.<span class="hljs-number">255.229</span> <span class="hljs-comment">#备DNS</span><br /><br /><span class="hljs-number">2</span>.重启网络<br />~]<span class="hljs-comment"># systemctl restart network.service</span><br />

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


1
<span class="hljs-number">1</span>.临时修改,只在此shell中和子shell中运行,下次登录失效<br />]<span class="hljs-comment"># ifconfig eno16777728 192.168.137.71/24</span><br /><br /><span class="hljs-number">2</span>.编辑配置文件<br />~]<span class="hljs-comment"># cat ifcfg-eno16777728 </span><br />DEVICE=eno16777728<br />BOOTPROTO=static <br />ONBOOT=yes &nbsp;<br />IPADDR=<span class="hljs-number">192.168</span>.<span class="hljs-number">137.70</span> <br />NETMASK=<span class="hljs-number">255.255</span>.<span class="hljs-number">255.0</span> <br />GATEWAY=<span class="hljs-number">192.168</span>.<span class="hljs-number">137.1</span> <br />DNS1=<span class="hljs-number">210.21</span>.<span class="hljs-number">196.6</span> <br />DNS2=<span class="hljs-number">119.233</span>.<span class="hljs-number">255.229</span> <br />

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


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br />IP=<span class="hljs-number">172.16</span>.<span class="hljs-number">250</span><br /><br /><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {<span class="hljs-number">1</span>..<span class="hljs-number">254</span>};<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">if</span> ping -c <span class="hljs-number">1</span> -w <span class="hljs-number">1</span> <span class="hljs-variable" style="line-height: 1.6">$IP</span>.<span class="hljs-variable" style="line-height: 1.6">$i</span> &amp;&gt;/dev/null;<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-operator" style="line-height: 1.6">-e</span> <span class="hljs-string">&quot;\033[32m <span class="hljs-variable">$IP</span>.<span class="hljs-variable">$i</span> is online. \033[0m&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-operator" style="line-height: 1.6">-e</span> <span class="hljs-string">&quot;\033[31m <span class="hljs-variable">$IP</span>.<span class="hljs-variable">$i</span> is off. \033[0m&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">fi</span><br /><span class="hljs-keyword">done</span><br />

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

1
ifcfg-接口名配置文件

 

1
DEVICE

:此配置文件对应的设备的名称; 

1
ONBOOT

:在系统的引导过程中,是否激活此接口; 

1
UUID

:此设备的唯一标识; 

1
IPV6INIT

:是否初始化IPV6 

1
BOOTPROTO

:激活此接口时使用什么协议来配置接口属性,常用的有dhcp、bootp、static、none; 

1
TYPE

:指明接口类型,常见的有Ethernet,Bridge; 

1
DNS1

:第一dns服务器指向; 

1
DNS2

:备用dns服务器指向; 

1
DOMAIN

:DNS搜索域; 

1
IPADDR

:配置ip地址; 

1
NETMASK

:子网掩码;centos7支持使用PREFIX以长度方式指明子网掩码; 

1
GATEWAY

:默认网关; 

1
USERCTL

:是否允许普通用户控制此设备; 

1
PEERDNS

:如果BOOTPROTO的值为“dhcp”,是否允许dhcp server分配的dns服务器指向覆盖本地手动指定的dns服务器指向;默认为允许:yes 

1
NM_CONTROLLED

:是否使用NetworkManager服务来控制接口; 

1
HWADDR

:设备的mac地址;


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


1
<span class="hljs-number">1</span>.临时生效<br /><span class="hljs-comment"># ifconfig eno16777728:0 192.168.137.71/24 up</span><br /><br /><span class="hljs-number">2</span>.修改配置文件生效<br />~]<span class="hljs-comment"># cat ifcfg-eno16777728:0</span><br />DEVICE=eno16777728:<span class="hljs-number">0</span><br />BOOTPROTO=static <br />ONBOOT=yes &nbsp;<br />IPADDR=<span class="hljs-number">192.168</span>.<span class="hljs-number">137.71</span> <br />NETMASK=<span class="hljs-number">255.255</span>.<span class="hljs-number">255.0</span> <br />GATEWAY=<span class="hljs-number">192.168</span>.<span class="hljs-number">137.1</span> <br />DNS1=<span class="hljs-number">210.21</span>.<span class="hljs-number">196.6</span> <br />DNS2=<span class="hljs-number">119.233</span>.<span class="hljs-number">255.229</span> <br />

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

1
ifconfig命令

:接口及地址查看和管理 

1
ifconfig [INTERFACE]

 

1
ifconfig interface [aftype] options |address

 

1
ifconfig IFACE IP/MASK [up]

 

1
ifconfig IFACE IP netmask NETMASK

1
route命令

:路由查看及管理 
查看路由: 

1
route -n

 
添加: 

1
route -add [-net|-host] target [netmask NM] [gw GW] [[dev]if]

删除: 

1
route del [-net|-host] target [gw GW] [netmask Nm] [[dev]if]

1
netstat命令:

 
显示路由: 

1
netstat -rn

 

1
-r

:显示内核路由表 

1
-n

:数字格式

显示网络连接: 

1
netstat [--tcp|-t] [--udp|-u] [--listening|-l] [--raw|-w] [--all|-a] [--numeric|-n] [--extend|-e] [--program|-p]

 

1
-t

:TCP协议的相关连接,连接均有其状态;FSM(finate state macheine); 

1
-u

:udp相关的连接 

1
-w

:raw socket相关的连接 

1
-l

:处于监听状态的连接 

1
-a

:所有状态 

1
-n

:以数字格式显示IP和port 

1
-e

:扩展格式 

1
-p

:显示相关的进程及PID

常用组合: 

1
-tan -uan -tnl ,unl,tunlp

显示接口的统计数据: 

1
netstat {--interfaces|-I|-i} [iface] [--all|-a] [--extend|-e] [--verbose|-v] [--program|-p] [--numeric|-n]

所有接口: 

1
netstat -i

 
指定接口: 

1
netstat -I&lt;iface&gt;

1
ifup/ifdown命令:

 
注意:通过配置文件来识别接口并完成配置;

1
iproute2家族:

 

1
ip命令:

 

1
show / manipulate routing, devices, policy routing and tunnels

1
ip [ OPTIONS ] OBJECT { COMMAND | help }

 

1
OBJECT={link|addr|route|netns}

1
ip OBJECT:

 

1
ip link

:network device configuration 

1
ip link set

 – change device attributes 

1
dev NAME (default)

:指明要管理的设备,dev关键字可省略; 

1
up and down

 

1
multicast on

1
multicast off

启用或关闭多播功能; 

1
ip link show - display device attributes

 

1
ip link help

显示简要使用帮助

1
ip netns list

 列出所有的netns 

1
ip netns add NAME

创建指定的netns 

1
ip netns del NAME

 删除指定的netns 

1
ip netns exec NAME COMMAND

 在指定的netns中运行命令

ip address add -add new protocol address 

1
ip addr add IFADDR dev IFACE

 

1
[label NAME]

:为额外的地址指明接口别名

1
ip address delete

 删除指定接口地址; 

1
ip address flush

 清空地址别名;

1
ip route { add | del | change | append | replace |monitor } ROUTE

 

1
ip route add TYPE PREFIX via GW [dev IFACE] [src SOURCE_IP]

1
ss命令:

 

1
ss [options] [ FILTER ] [EXPRESSION]

 
选项: 

1
-t

:tcp协议的相关连接 

1
-u

:UDP相关的连接; 

1
-w

:raw socket相关连接 

1
-l

:监听状态的连接 

1
-a

:所有状态连接; 

1
-n

:数字格式 

1
-p

:相关的程序及PID 

1
-e

:扩展格式信息; 

1
-m

:内存用量 

1
-o

:计时器信息


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

rpm安装:

1
rpm {-i|--install} [install-options] PACKAGE_FILE ...

 

1
rpm -ivh package_file

 

1
GENERAL OPTIONS:

 

1
-v

:详细信息 

1
-vv

:更加详细信息

1
[install-options]

: 

1
-h

1
hash marks

输出进度条,每个

1
#

表示

1
2%

的进度; 

1
--test

:测试安装,检查并报告依赖关系及冲突消息等; 

1
--nodeps

:忽略依赖关系;不建议; 

1
--replacepkgs

:重新安装

注意:rpm可以自带脚本 
四类:

1
--noscripts

 

1
preinstall

:安装过程开始之前运行的脚本,

1
%pre,--nopre

 

1
postinstal

l:安装过程完成之后运行的脚本,

1
%post,--nopost

 

1
preuninstall

:卸载过程真正开始执行之前运行的脚本,

1
%postun,--nopreun

 

1
postuninstall

:卸载过程完成之后运行的脚本,

1
%postun,--nopostun

1
--nosignature

:不检查包签名信息,不检查来源合法性; 

1
--nodigest

:不检查包完整性信息;

rpm升级

1
rpm {-U|--upgrade} [install-options] PACKAGE_FILE ...

 

1
rpm {-F|--freshen} [install-options] PACKAGE_FILE ...

 

1
-U:升级或安装

 

1
-F:升级

 

1
rpm -Uvh package_file...

 

1
rpm -Fvh package_file...

1
--oldpackage

:降级; 

1
--force

:强制升级或降级;

注意:

  1. 不要对内核做升级操作;Linux支持多内核并存,因此,直接安装新版内核;

  2. 如果某原程序包的配置文件安装后曾被修改过,升级时,新版本的程序提供的同一配置文件不会覆盖原有版本的配置文件,而是把新版本的配置文件重命名

    1
    (filename.rpmnew)

    后提供;

rpm卸载

1
rpm {-e|--erase} [--allmatches] [--nodeps][--noscripts][--test] PACKAGE_NAME ...

 

1
--allmatches

:卸载所有匹配指定名称的程序包的各版本; 

1
--nodeps

:忽略依赖关系; 

1
-test

:测试卸载,dry run 模式

rpm查询

1
rpm {-q|--query} [select-options] [query-options]

 

1
[select-options]

 

1
PACKAGE_NAME

:查询指定的程序包是否已经安装,及其版本; 

1
-a,--all

:查询所有已经安装过的包; 

1
-f,FILE

:查询指定的文件由哪个程序包安装生成; 

1
-p,--package PACKAGE_FILE

:用于实现对未安装的程序包执行查询操作; 

1
--whatprovides CAPABILITY

:查询指定的

1
CAPABILITY

由哪个程序包提供; 

1
--whatrequires CAPABILITY

:查询指定的

1
CAPABILITY

被哪个包所依赖;

1
[query-options]

 

1
--changelog

:查询

1
rpm

包的

1
changelog

; 

1
-l,--list

:程序包安装生成的所有列表; 

1
-i,--info

:程序包相关的信息,版本号、大小、所属的包组,等; 

1
-c,--configfiles

:查询指定的程序提供的配置文件; 

1
-d,--docfiles

:查询指定的程序提供的文档; 

1
--provides

:列出指定的程序包提供的所有的CAPABILITY; 

1
-E,--requires

:查询指定的程序包的依赖关系; 

1
--scripts

:查看程序包自带的脚本片段;

用法: 

1
-qi PACKAGE,-qf FILE,-qc PACKAGE,-ql PACKAGE,-qd PACKAGE,-qpi PACKAGE_FILE,-qpl PACKAGE_FILE,-qpc PACKAGE_FILE...

rpm校验

1
rpm {-V|--verify} [select-options] [verify-options]

1
S file Size differs&nbsp;<br />M Mode differs (includes permissions and file type)&nbsp;<br />5 digest (formerly MD5 sum) differs&nbsp;<br />D Device major/minor number mismatch&nbsp;<br />L readLink(2) path mismatch&nbsp;<br />U User ownership differs&nbsp;<br />G Group ownership differs&nbsp;<br />T mTime differs&nbsp;<br />P caPabilities differ

包来源合法性验证和完整性验证: 
来源合法性验证: 
完整性验证:

获取并导入信任的包制作者密钥: 
对于centos发行版来说:

1
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-xxx

验证: 
1. 安装此组织签名的程序时,会自动执行验证; 
2. 手动验证:

1
rpm -K package_file

数据库重建

rpm管理器数据库路径:

1
/var/lib/rpm/

 
查询操作:通过此处的数据库进行;

获取帮助: 
centos:

1
man rpm

 
centos:

1
man rpmdb

1
rpm {--initdb|--rebuilddb}

 

1
--initdb

:初始化数据库,当前无任何数据库可初始化创建一个新的;当前有时不执行任何操作; 

1
--rebuilddb

:重新构建,通过读取当前系统上所有已经安装过的程序包进行重新创建;

1
yum

命令的用法:

1
yum [options] [command] [package ...]

显示仓库列表 

1
repolist [all|enabled|disabled]

显示程序包 

1
yum list [all | glob_exp1] [glob_exp2] [...]

 

1
yum list {available|insitalled|updates} [glob_exp1] [...]

安装程序包: 

1
install package1 [package2] [...]

 

1
reinstall package1 [package2] [...]

 重新安装

升级程序包: 

1
update [package1] [package2] [...]

 

1
downgrade package1 [package2] [...]

降级

检查可用升级: 

1
check-update

卸载程序包: 

1
remove | erase package1 [package2] [...]

查看程序包

1
information

 

1
info [...]

查看指定的特性(可以是某文件哪个程序包所提供)是由:

1
rpm -qf

 

1
provides | whatprovides feature1 [feature2] [...]

清理本地缓存: 

1
clean [ packages | metadata | expire-cache | rpmdb| plugins | all ]

构建缓存: 

1
makecache

搜索: 

1
search string1 [string2] [...]

 
以指定的关键字搜索程序包名已

1
summary

(摘要)信息;

查看指定包所依赖的

1
capabilities

: 

1
deplist package1 [package2] [...]

安装及升级本地程序包: 

1
localinstall rpmfile1 [rpmfile2] [...]

 

1
(maintained for legacy reasons only - use install)

 centos6以后可忽略 

1
localupdate rpmfile1 [rpmfile2] [...]

 

1
(maintained for legacy reasons only - use update)

1
yum

命令行选项: 

1
--nogpgcheck

:禁止运行

1
gpg check

; 

1
-y

:自动回答为“

1
yes

”; 

1
-q

:静默模式; 

1
--disablerepo=repoidglob

:临时禁用此处指定的repo; 

1
--enablerepo=repoidglob

:临时启用此处指定的repo; 

1
--noplugins

:禁用所有的插件;

1
yum

1
repo

配置文件中可用的变量: 

1
$releasever

:当前OS的发行版的主版本号; 

1
$arch

:平台类型; 

1
$basearch

:基础平台; 

1
$YUM0-YUM9

 
例如:

1
http://mirrors.magedu.com/centos/$releaserver/$basearch/os

创建YUM仓库: 

1
createrepo [options] &lt;directory&gt;


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


1
<span class="hljs-number">1</span>.挂载光盘<br />mount -r /dev/cdrom /mnt/<br /><span class="hljs-number">2</span>.创建本地yum仓库<br />~]<span class="hljs-comment"># cat isodata.repo </span><br />[isodata]<br />name=isodata<br />baseurl=file:///mnt/<br /><span class="hljs-built_in">enable</span>=<span class="hljs-number">1</span><br />gpgcheck=<span class="hljs-number">0</span><br />

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


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-built_in">declare</span> -i k_file=<span class="hljs-number">0</span><br /><span class="hljs-built_in">declare</span> -i s_file=<span class="hljs-number">0</span><br /><br /><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> $(ls /etc/rc.d/rc3.d/ |grep ^K);<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$i</span> stop&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">let</span> k_file++<br /><span class="hljs-keyword">done</span><br /><br /><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> $(ls /etc/rc.d/rc3.d/ |grep ^S);<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$i</span> start&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">let</span> s_file++<br /><span class="hljs-keyword">done</span><br /><br /><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;There are <span class="hljs-variable">$k_file</span> at the beginning of K&quot;</span><br /><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;There are <span class="hljs-variable">$s_file</span> at the beginning of S&quot;</span><br />

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


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-built_in">declare</span> -i sum=<span class="hljs-number">0</span><br /><br /><span class="hljs-keyword">if</span> [ <span class="hljs-variable" style="line-height: 1.6">$#</span> <span class="hljs-operator" style="line-height: 1.6">-lt</span> <span class="hljs-number">1</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Usage:<span class="hljs-variable">$basename</span> <span class="hljs-variable">$0</span> username1 username2 ...&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">exit</span> <span class="hljs-number">3</span><br /><span class="hljs-keyword">fi</span><br /><br /><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> $*;<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">if</span> id <span class="hljs-variable" style="line-height: 1.6">$i</span> &amp;&gt; /dev/null;<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">let</span> sum=<span class="hljs-variable" style="line-height: 1.6">$sum</span>+$(id -u <span class="hljs-variable" style="line-height: 1.6">$i</span>)<br /> &nbsp; &nbsp;<span class="hljs-keyword">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;user <span class="hljs-variable">$i</span> is not existed!&quot;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">exit</span> <span class="hljs-number">5</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">fi</span><br /><span class="hljs-keyword">done</span><br /><br /><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;The sum of the uid for these users is <span class="hljs-variable">$sum</span>&quot;</span><br />

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


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-built_in">declare</span> -i sum=<span class="hljs-number">0</span><br /><br /><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> $*;<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-variable" style="line-height: 1.6">$i</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-keyword">for</span> file <span class="hljs-keyword">in</span> <span class="hljs-variable" style="line-height: 1.6">$i</span>/*;<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[ <span class="hljs-operator" style="line-height: 1.6">-f</span> <span class="hljs-variable" style="line-height: 1.6">$file</span> ] &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$file</span> is a common file.&quot;</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[ <span class="hljs-operator" style="line-height: 1.6">-d</span> <span class="hljs-variable" style="line-height: 1.6">$file</span> ] &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$file</span> is a directory.&quot;</span> &amp;&amp; <span class="hljs-built_in">let</span> sum++<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[ -c <span class="hljs-variable" style="line-height: 1.6">$file</span> ] &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$file</span> is a Character device file.&quot;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[ -L <span class="hljs-variable" style="line-height: 1.6">$file</span> ] &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$file</span> is a symbolic link file&quot;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-keyword">done</span><br /><span class="hljs-keyword">done</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;The $* directory have <span class="hljs-variable">$sum</span>.&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />

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


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-keyword">if</span> [ <span class="hljs-variable" style="line-height: 1.6">$#</span> -ge <span class="hljs-number">2</span> -o <span class="hljs-variable" style="line-height: 1.6">$#</span> <span class="hljs-operator" style="line-height: 1.6">-lt</span> <span class="hljs-number">1</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Usage:basename <span class="hljs-variable">$0</span> USER&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">exit</span> <span class="hljs-number">3</span><br /><span class="hljs-keyword">fi</span><br /><br /><span class="hljs-keyword">if</span> id <span class="hljs-variable" style="line-height: 1.6">$1</span> &amp;&gt;/dev/null;<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">if</span> [[ $(id -u <span class="hljs-variable" style="line-height: 1.6">$1</span>) -ge <span class="hljs-number">500</span> ]];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$1</span> is common user&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">fi</span><br /><span class="hljs-keyword">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$1</span> user is not exists&quot;</span><br /><span class="hljs-keyword">fi</span><br />

16. 写一个脚本 
(1) 添加10用户user1-user10;密码同用户名; 
(2) 用户不存在时才添加;存在时则跳过; 
(3) 最后显示本次共添加了多少用户;


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-built_in">declare</span> -i sum=<span class="hljs-number">0</span><br /><br /><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> user{<span class="hljs-number">1</span>..<span class="hljs-number">10</span>};<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">if</span> id <span class="hljs-variable" style="line-height: 1.6">$i</span> &amp;&gt;/dev/null;<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$i</span> is exists.&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;useradd <span class="hljs-variable" style="line-height: 1.6">$i</span> &amp;&gt;/dev/null<br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$i</span>&quot;</span> |passwd --stdin <span class="hljs-variable" style="line-height: 1.6">$i</span> &amp;&gt;/dev/null<br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">let</span> sum++<br /> &nbsp; &nbsp;<span class="hljs-keyword">fi</span><br /><span class="hljs-keyword">done</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Add user <span class="hljs-variable">$sum</span>.&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />

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


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br />IP=<span class="hljs-number">172.16</span>.<span class="hljs-number">250</span><br /><br /><span class="hljs-keyword">for</span> ((i=<span class="hljs-number">20</span>;i&lt;=<span class="hljs-number">100</span>;i++));<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">if</span> ping -c <span class="hljs-number">1</span> -w <span class="hljs-number">1</span> <span class="hljs-variable" style="line-height: 1.6">$IP</span>.<span class="hljs-variable" style="line-height: 1.6">$i</span> &amp;&gt;/dev/null;<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;The <span class="hljs-variable">$IP</span>.<span class="hljs-variable">$i</span> is online.&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">fi</span><br /><span class="hljs-keyword">done</span><br />

18. 打印九九乘法表;


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-keyword">for</span> ((i=<span class="hljs-number">1</span>;i&lt;=<span class="hljs-number">9</span>;i++));<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">for</span> ((j=<span class="hljs-number">1</span>;j&lt;=<span class="hljs-variable" style="line-height: 1.6">$i</span>;j++));<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">let</span> sum=<span class="hljs-variable" style="line-height: 1.6">$i</span>*<span class="hljs-variable" style="line-height: 1.6">$j</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> -en <span class="hljs-string">&quot;&quot;</span><span class="hljs-variable" style="line-height: 1.6">$i</span><span class="hljs-string">&quot;X&quot;</span><span class="hljs-variable" style="line-height: 1.6">$j</span><span class="hljs-string">&quot;=<span class="hljs-variable">$sum</span> &quot;</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">done</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;&quot;</span> <br /><span class="hljs-keyword">done</span>

原创文章,作者:N22-白蚁,如若转载,请注明出处:/64115

评论列表(1条)

  • 马哥教育
    马哥教育 2016-12-23 00:25

    赞,写的不错~IP地址分类可以补充一下私有地址段。

联系我们

400-080-6560

在线咨询:点击这里给我发消息

邮件:1660809109@qq.com

工作时间:周一至周五,9:30-18:30,节假日同时也值班

友情链接:万达娱乐主管QQ  万达娱乐主管QQ  万达招商  万达主管QQ  万达娱乐注册  万达娱乐招商QQ  万达娱乐开户  万达直属  万达娱乐招商QQ  万达娱乐主管