第五周

1、每12小时备份并压缩/etc/目录至/backup目录中,保存文件名称格式为,“etc-年-月-日-时-分.tar.gz”

~]# mkdir /backup

在/root目录下创建备份命令脚本:
vim backup-ect.cron
#!/bin/bash
#
tar -zcf /backup/etc-$(date +%Y-%m-%d-%H-%M).tar.gz /etc/
:wq

chmod a+x /root/backup-ect.cron

crontab -e
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  *   command to be executed    

* */12 * * * /bin/bash /root/backup-etc.cron
:wq

conrtab -l

cat /var/spool/cron/USERNAME

2、rpm包管理功能总结以及实例应用演示。

安装、升级、卸载、查询和校验、数据库维护
rpm命令:rpm [OPTIONS] [PACKAGE_FILE]
安装:-i, --install
升级:-U, --update, -F, --freshen
卸载:-e, --erase
查询:-q, --query
校验:-V, --verify
数据库维护:–builddb, --initdb

安装:
rpm -ivh PACKAGE_FILE …

~]# rpm -ivh zsh-5.0.2-31.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:zsh-5.0.2-31.el7                 ################################# [100%]

卸载:

~]# rpm -evh zsh
Preparing...                          ################################# [100%]
Cleaning up / removing...
   1:zsh-5.0.2-31.el7                 ################################# [100%]

升级:

~]# rpm -Uvh zsh-5.0.2-31.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
	package zsh-5.0.2-31.el7.x86_64 is already installed

查询:
rpm {-q|–query} [select-options] [query-options]

[select-options]
PACKAGE_NAME:查询指定的程序包是否已经安装,及其版本;
-a, --all:查询所有已经安装过的包;
-f FILE:查询指定的文件由哪个程序包安装生成;

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

--whatprovides CAPABILITY:查询指定的CAPABILITY由哪个程序包提供;
--whatrequires CAPABILITY:查询指定的CAPABILITY被哪个包所依赖;

[query-options]
–changelog:查询rpm包的changlog;
-l, --list:程序安装生成的所有文件列表;
-i, --info:程序包相关的信息,版本号、大小、所属的包组,等;
-c, --configfiles:查询指定的程序包提供的配置文件;
-d, --docfiles:查询指定的程序包提供的文档;
–provides:列出指定的程序包提供的所有的CAPABILITY;
-R, --requires:查询指定的程序包的依赖关系;
–scripts:查看程序包自带的脚本片断;

例1:
查询/bin/zsh由哪个程序包安装

~]# rpm -qf /bin/zsh
zsh-5.0.2-31.el7.x86_64

查询/usr/bin/find由哪个程序包安装

~]# rpm -qf /usr/bin/find
findutils-4.5.11-6.el7.x86_64

例2:
查询zsh程序包的相关信息

~]# rpm -qi zsh
Name        : zsh
Version     : 5.0.2
Release     : 31.el7
Architecture: x86_64
Install Date: Sun 03 Mar 2019 10:00:10 PM CST
Group       : System Environment/Shells
Size        : 5854390
License     : MIT
Signature   : RSA/SHA256, Mon 12 Nov 2018 10:49:55 PM CST, Key ID 24c6a8a7f4a80eb5
Source RPM  : zsh-5.0.2-31.el7.src.rpm
Build Date  : Wed 31 Oct 2018 12:48:17 AM CST
Build Host  : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://zsh.sourceforge.net/
Summary     : Powerful interactive shell
Description :
The zsh shell is a command interpreter usable as an interactive login
shell and as a shell script command processor.  Zsh resembles the ksh
shell (the Korn shell), but includes many enhancements.  Zsh supports
command line editing, built-in spelling correction, programmable
command completion, shell functions (with autoloading), a history
mechanism, and more.

例3:
查看zsh程序包安装后所生成的文件信息:

~]# rpm -ql zsh
/bin/zsh
/etc/skel/.zshrc
/etc/zlogin
/etc/zlogout
/etc/zprofile
/etc/zshenv
/etc/zshrc
...
/usr/lib64/zsh
/usr/lib64/zsh/5.0.2
/usr/lib64/zsh/5.0.2/zsh
...
/usr/share/doc/zsh-5.0.2
/usr/share/doc/zsh-5.0.2/BUGS
/usr/share/doc/zsh-5.0.2/CONTRIBUTORS
/usr/share/info/zsh.info-1.gz
/usr/share/info/zsh.info-2.gz
...
/usr/share/man/man1/zsh.1.gz
/usr/share/man/man1/zshall.1.gz
/usr/share/man/man1/zshbuiltins.1.gz
...
/usr/share/zsh
/usr/share/zsh/5.0.2
/usr/share/zsh/5.0.2/functions
/usr/share/zsh/5.0.2/functions/VCS_INFO_adjust
...
/usr/share/zsh/site-functions

例4:
查看未安装的rpm程序包信息,通过选项p实现:
(1)查看程序包信息:

~]# rpm -qpi zsh-5.0.2-31.el7.x86_64.rpm 
Name        : zsh
Version     : 5.0.2
Release     : 31.el7
Architecture: x86_64
Install Date: (not installed)
Group       : System Environment/Shells
Size        : 5854390
License     : MIT
Signature   : RSA/SHA256, Mon 12 Nov 2018 10:49:55 PM CST, Key ID 24c6a8a7f4a80eb5
Source RPM  : zsh-5.0.2-31.el7.src.rpm
Build Date  : Wed 31 Oct 2018 12:48:17 AM CST
Build Host  : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://zsh.sourceforge.net/
Summary     : Powerful interactive shell
Description :
The zsh shell is a command interpreter usable as an interactive login
shell and as a shell script command processor.  Zsh resembles the ksh
shell (the Korn shell), but includes many enhancements.  Zsh supports
command line editing, built-in spelling correction, programmable
command completion, shell functions (with autoloading), a history
mechanism, and more.

(2)查看程序包安装后所生成的文件:

~]# rpm -qpl zsh-5.0.2-31.el7.x86_64.rpm 
/bin/zsh
/etc/skel/.zshrc
/etc/zlogin
/etc/zlogout
/etc/zprofile
/etc/zshenv
/etc/zshrc
...
/usr/lib64/zsh
/usr/lib64/zsh/5.0.2
/usr/lib64/zsh/5.0.2/zsh
...
/usr/share/doc/zsh-5.0.2
/usr/share/doc/zsh-5.0.2/BUGS
/usr/share/doc/zsh-5.0.2/CONTRIBUTORS
/usr/share/info/zsh.info-1.gz
/usr/share/info/zsh.info-2.gz
...
/usr/share/man/man1/zsh.1.gz
/usr/share/man/man1/zshall.1.gz
/usr/share/man/man1/zshbuiltins.1.gz
...
/usr/share/zsh
/usr/share/zsh/5.0.2
/usr/share/zsh/5.0.2/functions
/usr/share/zsh/5.0.2/functions/VCS_INFO_adjust
...
/usr/share/zsh/site-functions

例5:查看程序包的依赖关系:

~]# rpm -qpR zsh-5.0.2-31.el7.x86_64.rpm 
/bin/sh
/bin/sh
/bin/sh
/bin/sh
/bin/zsh
/sbin/install-info
/sbin/install-info
config(zsh) = 5.0.2-31.el7
coreutils
grep
grep
libc.so.6()(64bit)
libc.so.6(GLIBC_2.11)(64bit)
libc.so.6(GLIBC_2.14)(64bit)
libc.so.6(GLIBC_2.15)(64bit)
libc.so.6(GLIBC_2.2.5)(64bit)
libc.so.6(GLIBC_2.3)(64bit)
libc.so.6(GLIBC_2.3.4)(64bit)
libc.so.6(GLIBC_2.4)(64bit)
libc.so.6(GLIBC_2.7)(64bit)
libdl.so.2()(64bit)
libdl.so.2(GLIBC_2.2.5)(64bit)
libm.so.6()(64bit)
libm.so.6(GLIBC_2.2.5)(64bit)
libncursesw.so.5()(64bit)
librt.so.1()(64bit)
librt.so.1(GLIBC_2.2.5)(64bit)
libtinfo.so.5()(64bit)
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
rtld(GNU_HASH)
rpmlib(PayloadIsXz) <= 5.2-1

3、yum的配置和使用总结以及yum私有仓库的创建。
例1:通过yum工具安装tree程序包

~]# yum -y install tree
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================
 Package                    Arch                         Version                            Repository                  Size
=============================================================================================================================
Installing:
 tree                       x86_64                       1.6.0-10.el7                       base                        46 k

Transaction Summary
=============================================================================================================================
Install  1 Package

Total download size: 46 k
Installed size: 87 k
Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm                                                                          |  46 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : tree-1.6.0-10.el7.x86_64                                                                                  1/1 
  Verifying  : tree-1.6.0-10.el7.x86_64                                                                                  1/1 

Installed:
  tree.x86_64 0:1.6.0-10.el7                                                                                                 

Complete!

例2:通过yum工具卸载程序包

~]# yum -y remove tree
Loaded plugins: fastestmirror, langpacks
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================
 Package                   Arch                        Version                              Repository                  Size
=============================================================================================================================
Removing:
 tree                      x86_64                      1.6.0-10.el7                         @base                       87 k

Transaction Summary
=============================================================================================================================
Remove  1 Package

Installed size: 87 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : tree-1.6.0-10.el7.x86_64                                                                                  1/1 
  Verifying  : tree-1.6.0-10.el7.x86_64                                                                                  1/1 

Removed:
  tree.x86_64 0:1.6.0-10.el7                                                                                                 

Complete!

例3:查看程序包信息

~]# yum info tree
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Available Packages
Name        : tree
Arch        : x86_64
Version     : 1.6.0
Release     : 10.el7
Size        : 46 k
Repo        : base/7/x86_64
Summary     : File system tree viewer
URL         : http://mama.indstate.edu/users/ice/tree/
License     : GPLv2+
Description : The tree utility recursively displays the contents of directories in a
            : tree-like format.  Tree is basically a UNIX port of the DOS tree
            : utility.

例4:查看程序包依赖关系

~]# yum deplist tcpdump
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
package: tcpdump.x86_64 14:4.9.2-3.el7
  dependency: /bin/sh
   provider: bash.x86_64 4.2.46-31.el7
  dependency: libc.so.6(GLIBC_2.14)(64bit)
   provider: glibc.x86_64 2.17-260.el7_6.3
  dependency: libcap-ng.so.0()(64bit)
   provider: libcap-ng.x86_64 0.7.5-4.el7
  dependency: libcrypto.so.10()(64bit)
   provider: openssl-libs.x86_64 1:1.0.2k-16.el7
  dependency: libcrypto.so.10(OPENSSL_1.0.2)(64bit)
   provider: openssl-libs.x86_64 1:1.0.2k-16.el7
  dependency: libcrypto.so.10(libcrypto.so.10)(64bit)
   provider: openssl-libs.x86_64 1:1.0.2k-16.el7
  dependency: libpcap >= 14:1.5.3-10
   provider: libpcap.x86_64 14:1.5.3-11.el7
   provider: libpcap.i686 14:1.5.3-11.el7
  dependency: libpcap.so.1()(64bit)
   provider: libpcap.x86_64 14:1.5.3-11.el7
  dependency: rtld(GNU_HASH)
   provider: glibc.x86_64 2.17-260.el7_6.3
   provider: glibc.i686 2.17-260.el7_6.3
  dependency: shadow-utils
   provider: shadow-utils.x86_64 2:4.1.5.1-25.el7

4、写一个脚本实现列出以下菜单给用户:
 
(1)disk:show disk info信息
 
(2)mem: show memory info信息
 
(3)cpu: show cpu info信息
 
(*)quit

#!/bin/bash
#
cat << EOF
disk) show disks info
mem) show memory info
cpu) show cpu info
*) quit
EOF

read -p "Your choice: " option

if [[ "$option" == "disk" ]]; then
	fdisk -l /dev/[sh]d[a-z]
elif [[ "$option" == "mem" ]]; then
	free -m
elif [[ "$option" == "cpu" ]];then
	lscpu
else
	echo "Unkown option."
	exit 3
fi

5、sed用法总结并结合实例演示
sed [OPTION]… ‘script’ [input-file] …
script:
地址定界编辑命令

常用选项:
-n:不输出模式空间中的内容至屏幕;
-e script, --expression=script:多点编辑;
-f /PATH/TO/SED_SCRIPT_FILE
每行一个编辑命令;
-r, --regexp-extended:支持使用扩展正则表达式;
-i[SUFFIX], --in-place[=SUFFIX]:直接编辑原文件 ;

~]# sed  -e  's@^#[[:space:]]*@@'   -e  '/^UUID/d'  /etc/fstab

地址定界:
(1) 空地址:对全文进行处理;
(2) 单地址:
#:指定行;
/pattern/:被此模式所匹配到的每一行;
(3) 地址范围
#,#:
#,+#:
#,/pat1/
/pat1/,/pat2/
$:最后一行;
(4) 步进:~
1~2:所有奇数行
2~2:所有偶数行

编辑命令:
d:删除;
p:显示模式空间中的内容;
a \text:在行后面追加文本“text”,支持使用\n实现多行追加;
i \text:在行前面插入文本“text”,支持使用\n实现多行插入;
c \text:把匹配到的行替换为此处指定的文本“text”;
w /PATH/TO/SOMEFILE:保存模式空间匹配到的行至指定的文件中;
r /PATH/FROM/SOMEFILE:读取指定文件的内容至当前文件被模式匹配到的行后面;文件合并;
=:为模式匹配到的行打印行号;
!:条件取反;
地址定界!编辑命令;
s///:查找替换,其分隔符可自行指定,常用的有s@@@, s###等;
替换标记:
g:全局替换;
w /PATH/TO/SOMEFILE:将替换成功的结果保存至指定文件中;
p:显示替换成功的行;

	练习1:删除/boot/grub/grub2.cfg文件中所有以空白字符开头的行的行首的所有空白字符;
		~]# sed  's@^[[:space:]]\+@@' /etc/grub2.cfg
	练习2:删除/etc/fstab文件中所有以#开头的行的行首的#号及#后面的所有空白字符;
		~]# sed  's@^#[[:space:]]*@@'  /etc/fstab
	练习3:输出一个绝对路径给sed命令,取出其目录,其行为类似于dirname;
		~]# echo "/var/log/messages/" | sed 's@[^/]\+/\?$@@'
		~]# echo "/var/log/messages" | sed -r 's@[^/]+/?$@@'

高级编辑命令:
h:把模式空间中的内容覆盖至保持空间中;
H:把模式空间中的内容追加至保持空间中;
g:把保持空间中的内容覆盖至模式空间中;
G:把保持空间中的内容追加至模式空间中;
x:把模式空间中的内容与保持空间中的内容互换;
n:覆盖读取匹配到的行的下一行至模式空间中;
N:追加读取匹配到的行的下一行至模式空间中;
d:删除模式空间中的行;
D:删除多行模式空间中的所有行;

示例:
	sed  -n  'n;p'  FILE:显示偶数行;
	sed  '1!G;h;$!d'  FILE:逆序显示文件的内容;
	sed  ’$!d'  FILE:取出最后一行;
	sed  '$!N;$!D' FILE:取出文件后两行;
	sed '/^$/d;G' FILE:删除原有的所有空白行,而后为所有的非空白行后添加一个空白行;
	sed  'n;d'  FILE:显示奇数行;
	sed 'G' FILE:在原有的每行后方添加一个空白行;

6、 用bash实现统计访问日志文件中状态码大于等于400的IP数量并排序
 
7、 使用自制的yum源安装ftp、openssh、curl、wget、tcpdump等软件包
(1)挂载光盘,创建本地仓库

~]# mount -r /dev/sr0 /media/cdrom/
~]# cp -r /media/cdrom/Packages/ /tmp/yum/
~]# createrepo /tmp/yum/Packages/
~]# vim /etc/yum.repos.d/local.repo
[local]
name=local repo
baseurl=file:///tmp/yum/packages/Packages/
gpgcheck=0

~]# yum repolist 
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
repo id                                                                                  repo name                                                                                   status
local                                                                                    local repo                                                                                  4,021
repolist: 4,021

~]# yum -y install httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-88.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================================================================
 Package                                  Arch                                      Version                                                 Repository                                Size
===========================================================================================================================================================================================
Installing:
 httpd                                    x86_64                                    2.4.6-88.el7.centos                                     local                                    2.7 M

Transaction Summary
===========================================================================================================================================================================================
Install  1 Package

Total download size: 2.7 M
Installed size: 9.4 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-2.4.6-88.el7.centos.x86_64                                                                                                                                        1/1 
  Verifying  : httpd-2.4.6-88.el7.centos.x86_64                                                                                                                                        1/1 

Installed:
  httpd.x86_64 0:2.4.6-88.el7.centos                                                                                                                                                       

Complete!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值