week_5_周期计划任务、sed、rpm、yum


Q:

1、每12小时备份并压缩/etc/目录至/backup目录中,保存文件名称格式为,"etc-年-月-日-时-分.tar.gz"
2、rpm包管理功能总结以及实例应用演示。
3、yum的配置和使用总结以及yum私有仓库的创建。
4、写一个脚本实现列出以下菜单给用户:

(1)disk:show disk info信息
(2)mem: show memory info信息
(3)cpu: show cpu info信息
(*)quit

5、sed用法总结并结合实例演示
6、 用bash实现统计访问日志文件中状态码大于等于400的IP数量并排序
7、 使用自制的yum源安装ftp、openssh、curl、wget、tcpdump等软件包

A:

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

[root@localhost ~]# su - gentoo
Last login: Thu Feb 28 06:29:31 EST 2019 on pts/0
-bash-4.2$ pwd
/users/gentoo
-bash-4.2$ crontab -l
no crontab for gentoo
-bash-4.2$ crontab -e
no crontab for gentoo - using an empty one
crontab: installing new crontab
-bash-4.2$ crontab -l
0 */12 * * * tar -czf /backup/etc-`date +"%F-%H-%M-%S`.tar.gz /etc
-bash-4.2$ ls /var/spool/cron/
ls: cannot open directory /var/spool/cron/: Permission denied
-bash-4.2$ su -c "cat /var/spool/cron/gentoo"
Password: 
0 */12 * * * tar -czf /backup/etc-`date +"%F-%H-%M-%S`.tar.gz /etc

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

查询和验证 PACKAGES:
rpm {-q|--query} [select-options] [query-options]
rpm {-V|--verify} [select-options] [verify-options]

rpmkeys --import PUBKEY ...
rpmkeys {-K|--checksig} PACKAGE_FILE ...

[root@localhost ~]# rpm --import /root/RPM-GPG-KEY-CentOS-7 
[root@localhost ~]# rpm -K zsh-5.0.2-31.el7.x86_64.rpm 
zsh-5.0.2-31.el7.x86_64.rpm: rsa sha1 (md5) pgp md5 OK

安装、升级和移除 PACKAGES:
rpm {-i|--install} [install-options] PACKAGE_FILE ...
rpm {-U|--upgrade} [install-options] PACKAGE_FILE ...
rpm {-F|--freshen} [install-options] PACKAGE_FILE ...
rpm {-e|--erase} [--allmatches] [--justdb] [--nodeps] [--noscripts] [--notriggers] [--test] PACKAGE_NAME ...

[select-options]
        [PACKAGE_NAME] [-a,--all] [-f,--file FILE]
        [-g,--group GROUP] {-p,--package PACKAGE_FILE]
        [--hdrid SHA1] [--pkgid MD5] [--tid TID]
        [--querybynumber HDRNUM] [--triggeredby PACKAGE_NAME]
        [--whatprovides CAPABILITY] [--whatrequires CAPABILITY]
  • 查询
[root@localhost ~]# rpm -qc bash
/etc/skel/.bash_logout
/etc/skel/.bash_profile
/etc/skel/.bashrc

[root@localhost ~]# rpm -qi bash
Name        : bash
Version     : 4.2.46
Release     : 31.el7
Architecture: x86_64
...

[root@localhost ~]# rpm -q --provides bash
/bin/bash
/bin/sh
bash = 4.2.46-31.el7
bash(x86-64) = 4.2.46-31.el7
config(bash) = 4.2.46-31.el7

[root@localhost ~]# rpm -q --whatprovides bash
bash-4.2.46-31.el7.x86_64

[root@localhost ~]# rpm -q --scripts bash
postinstall scriptlet (using ):
...
postuninstall scriptlet (using ):
-- Run it only if we are uninstalling
...

  • 验证
[root@localhost ~]# rpm -ql yum
...
/usr/share/doc/yum-3.4.3/README
...

[root@localhost ~]# rpm -qd yum
/usr/share/doc/yum-3.4.3/AUTHORS
/usr/share/doc/yum-3.4.3/COPYING
/usr/share/doc/yum-3.4.3/ChangeLog
/usr/share/doc/yum-3.4.3/INSTALL
/usr/share/doc/yum-3.4.3/PLUGINS
/usr/share/doc/yum-3.4.3/README
/usr/share/doc/yum-3.4.3/TODO
/usr/share/doc/yum-3.4.3/comps.rng
/usr/share/man/man5/yum.conf.5
/usr/share/man/man8/yum-shell.8
/usr/share/man/man8/yum.8

[root@localhost ~]# file /usr/share/doc/yum-3.4.3/README
/usr/share/doc/yum-3.4.3/README: ASCII text

[root@localhost ~]# vim /usr/share/doc/yum-3.4.3/README

[root@localhost ~]# rpm -V yum
S.5....T.  d /usr/share/doc/yum-3.4.3/README

[root@localhost ~]# vim /usr/share/doc/yum-3.4.3/README

[root@localhost ~]# rpm -V yum
.......T.  d /usr/share/doc/yum-3.4.3/README
       S file Size differs
       M Mode differs (includes permissions and file type)
       5 digest (formerly MD5 sum) differs
       D Device major/minor number mismatch
       L readLink(2) path mismatch
       U User ownership differs
       G Group ownership differs
       T mTime differs
       P caPabilities differ

从aliyun镜像站下载zsh

[root@localhost ~]# wget -P /root/ https://mirrors.aliyun.com/centos/7.6.1810/os/x86_64/Packages/zsh-5.0.2-31.el7.x86_64.rpm
--2019-02-28 09:04:42--  https://mirrors.aliyun.com/centos/7.6.1810/os/x86_64/Packages/zsh-5.0.2-31.el7.x86_64.rpm
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 223.111.123.230, 112.47.40.226, 112.48.148.105, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|223.111.123.230|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2496072 (2.4M) [application/x-redhat-package-manager]
Saving to: ‘/root/zsh-5.0.2-31.el7.x86_64.rpm’

100%[==============================================================================================>] 2,496,072   2.28MB/s   in 1.0s   

2019-02-28 09:04:45 (2.28 MB/s) - ‘/root/zsh-5.0.2-31.el7.x86_64.rpm’ saved [2496072/2496072]

[root@localhost ~]# wget -P /root/ https://mirrors.aliyun.com/centos/7.6.1810/os/x86_64/RPM-GPG-KEY-CentOS-7
--2019-02-28 09:05:39--  https://mirrors.aliyun.com/centos/7.6.1810/os/x86_64/RPM-GPG-KEY-CentOS-7
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 223.111.123.230, 112.47.40.226, 112.48.148.105, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|223.111.123.230|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1690 (1.7K) [application/octet-stream]
Saving to: ‘/root/RPM-GPG-KEY-CentOS-7’

100%[==============================================================================================>] 1,690       --.-K/s   in 0s      

2019-02-28 09:05:40 (24.1 MB/s) - ‘/root/RPM-GPG-KEY-CentOS-7’ saved [1690/1690]

[root@localhost ~]# ls /root
anaconda-ks.cfg  myfirst.sh  RPM-GPG-KEY-CentOS-7  zsh-5.0.2-31.el7.x86_64.rpm
[root@localhost ~]# ls /etc/pki/rpm-gpg/
RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Debug-7  RPM-GPG-KEY-CentOS-Testing-7
[root@localhost ~]# diff /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 /root/RPM-GPG-KEY-CentOS-7 

  • 安装
[root@localhost ~]# rpm -ivh zsh-5.0.2-31.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating \ installing...
   1:zsh-5.0.2-31.el7                 ################################# [100%]
[root@localhost ~]# rpm -iv --replacepkgs zsh-5.0.2-31.el7.x86_64.rpm 
Preparing packages...
zsh-5.0.2-31.el7.x86_64
  • 升级
[root@localhost ~]# 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
  • 删除
[root@localhost ~]# rpm -e zsh
[root@localhost ~]# rpm -q zsh
package zsh is not installed

3、yum的配置和使用总结以及yum私有仓库的创建。

yum客户端:

  • 配置文件:
    /etc/yum.conf:为所有仓库提供公共配置
    /etc/yum.repos.d/*.repo:为仓库的指向提供配置

  • 仓库指向的定义:
    [repositoryID]
    name=Some name for this repository
    baseurl=url://path/to/repository/
    enabled={1|0}
    gpgcheck={1|0}
    gpgkey=URL
    enablegroups={1|0}
    failovermethod={roundrobin|priority}
      默认为:roundrobin,意为随机挑选;
    cost=
      默认为1000

  • cat /etc/yum.conf

[root@localhost ~]# cat /etc/yum.conf 
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
  • cat /etc/yum.repos.d/CentOS-Base.repo
[root@localhost ~]# cat /etc/yum.repos.d/CentOS-Base.repo 
# CentOS-Base.repo
...
#

[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates 
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  • yum repolist
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.zju.edu.cn
 * extras: mirrors.nju.edu.cn
 * updates: centos.ustc.edu.cn
repo id                                                         repo name                                                         status
!base/7/x86_64                                                  CentOS-7 - Base                                                   10,019
!extras/7/x86_64                                                CentOS-7 - Extras                                                    370
!updates/7/x86_64                                               CentOS-7 - Updates                                                 1,098
repolist: 11,487
  • 挂载cdrom
[root@localhost ~]# mount /dev/sr0 /media/cdrom/
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# ls /media/cdrom/
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL
  • 配置yum源
[root@localhost ~]# vim /etc/yum.repos.d/my-centos7.repo

[root@localhost ~]# cat /etc/yum.repos.d/my-centos7.repo
[myyumrepo]
name=my-centos7-cdrom
baseurl=file:///media/cdrom
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.zju.edu.cn
 * extras: mirrors.nju.edu.cn
 * updates: centos.ustc.edu.cn
base                                                                                                             | 3.6 kB  00:00:00     
extras                                                                                                           | 3.4 kB  00:00:00     
myyumrepo                                                                                                        | 3.6 kB  00:00:00     
updates                                                                                                          | 3.4 kB  00:00:00     
(1/4): myyumrepo/group_gz                                                                                        | 166 kB  00:00:00     
(2/4): extras/7/x86_64/primary_db                                                                                | 180 kB  00:00:00     
(3/4): myyumrepo/primary_db                                                                                      | 3.1 MB  00:00:00     
(4/4): updates/7/x86_64/primary_db                                                                               | 2.4 MB  00:00:00     
repo id                                                         repo name                                                         status
base/7/x86_64                                                   CentOS-7 - Base                                                   10,019
extras/7/x86_64                                                 CentOS-7 - Extras                                                    371
myyumrepo                                                       my-centos7-cdrom                                                   4,021
updates/7/x86_64                                                CentOS-7 - Updates                                                 1,103
repolist: 15,514

4、写一个脚本实现列出以下菜单给用户

[root@localhost ~]# bash /scripts/w5e4.sh
1) disk : show disk info
2) mem : show memory info 
3) cpu : show cpu info
*) quit
input your choice :mem
              total        used        free      shared  buff/cache   available
Mem:           982M        138M        684M        7.5M        159M        672M
Swap:          2.0G          0B        2.0G
[root@localhost ~]# cat /scripts/w5e4.sh
#!/bin/bash
cat << EOF
1) disk : show disk info
2) mem : show memory info 
3) cpu : show cpu info
*) quit
EOF
read -p 'input your choice :' option
if [[ $option == 'disk' ]];then 
    fdisk -l
elif [[ $option == 'mem' ]];then
    free -h
elif [[ $option == 'cpu' ]];then
    lscpu
else
    exit 0
fi

5、sed用法总结并结合实例演示

https://www.jianshu.com/p/95df639d7da9

6、用bash实现统计访问日志文件中状态码大于等于400的IP数量并排序

sed -n -r '/\<40[0-9]\>/p' /var/log/httpd/access_log|cut -d" " -f1 |sort|uniq -c

7、使用自制的yum源安装ftp、openssh、curl、wget、tcpdump等软件包

[root@localhost ~]# yum install ftp openssh curl wget tcpdump

你可能感兴趣的:(week_5_周期计划任务、sed、rpm、yum)