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 <lua>):
...
postuninstall scriptlet (using <lua>):
-- 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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,692评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,482评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,995评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,223评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,245评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,208评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,091评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,929评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,346评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,570评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,739评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,437评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,037评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,677评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,833评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,760评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,647评论 2 354

推荐阅读更多精彩内容