在Linux下进行脚本开发,以及系统维护大多少不了rpm命令的身影。常见的yum命令更是基于rpm命令之上的,接下来就让我简单介绍一下rpm命令,并用rpm命令简单开发一个刷取访问量的脚本程序。
rpm命令rpm命令是RPM软件包的管理工具,rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎并逐渐受到其他发行版的采用。
语法:rpm 【参数】 【包名】
参数 | 功能 |
---|---|
-a | 查询所有套件 |
-c | 只列出组态配置文件,本参数需配合"-l"参数使用 |
-d | 只列出文本文件,本参数需配合"-l"参数使用 |
-e【套件档】 | 删除指定的套件 |
-f【文件】+ | 查询拥有指定文件的套件 |
-h | 套件安装时列出标记 |
-i | 显示套件的相关信息 |
-i【套件档】 | 安装指定的套件档 |
-l | 显示套件的文件列表 |
-p【套件档】+ | 查询指定的RPM套件档 |
-q | 使用询问模式,当遇到任何问题时,rpm指令会先询问用户 |
-R | 显示套件的关联性信息 |
-s | 显示文件状态,本参数需配合"-l"参数使用 |
-U【套件档】 | 升级指定的套件档 |
-v | 显示指令执行过程 |
-vv | 详细显示指令执行过程,便于排错 |
示例:
首先找到目标rpm包,然后利用 -i 命令安装该目标软件
[root@localhost x86_64]# ls
myhello-1.0-0.el7.x86_64.rpm
[root@localhost x86_64]# rpm -i myhello-1.0-0.el7.x86_64.rpm
通过 -ql 组合命令产看软件安装情况,最后用 -e 命令卸载
[root@localhost x86_64]# rpm -ql myhello
/usr/bin/myhello
[root@localhost x86_64]# rpm -e myhello
[root@localhost x86_64]# rpm -ql myhello
package myhello is not installed
[root@localhost x86_64]#
图示:
将脚本代码书写如下:
1 #!/usr/bin/python3
2 import requests
3 import time
4 url = ['https://blog.csdn.net/qq_42452450/article/details/104617518','https://blog.csdn.net/qq_42452450/article/details /104719646'] #访问链接可以多个
5 headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3 683.103 Safari/537.36'}
6 num = 0 #定义刷取次数
7 countUrl = len(url) #获取链接数量
8 while num < 100:
9 try:
10 for i in range(countUrl): #按照链接数量访问
11 response = requests.get(url[i], headers=headers)
12 if response.status_code == 200:
13 num = num + 1
14 print('成功访问 ' + str(num), '次')
15 time.sleep(60) #由于网站cookie针对同一限制需要隔一段时间(60s)
16 except Exception:
17 print('访问失败请稍后!')
18 time.sleep(60)
脚本书写完毕可以利用 pyinstall -F命令编译运行,也可以利用chmod u+x增加执行权限运行(编译运行可执行文件为为二进制文件且代码不可见,增加权限执行为普通可执行文件且代码可见)
注:如果运行出现requests模块未安装问题请点击下列链接查看https://blog.csdn.net/qq_42452450/article/details/104781623
请利用下列命令安装下列软件:
命令 | 软件功能 |
---|---|
pip3 install pyinstaller | 编译python程序 |
yum install rpmdevtools | 创建rpm制作空间的工具软件 |
yum install rpm-bulid | 创建rpm包软件 |
首先利用pyinstaller编译python文件
[root@localhost practicec]# ls
myhello
[root@localhost practicec]# pyinstaller -F myhello
794 INFO: PyInstaller: 3.6
794 INFO: Python: 3.6.8
814 INFO: Platform: Linux-3.10.0-1062.12.1.el7.x86_64-x86_64-with-centos-7.7.1908-Core
816 INFO: wrote /root/practicec/myhello.spec
820 INFO: UPX is not available.
840 INFO: Extending PYTHONPATH with paths
['/root/practicec', '/root/practicec']
841 INFO: checking Analysis
841 INFO: Building Analysis because Analysis-00.toc is non existent
841 INFO: Initializing module dependency graph...
882 INFO: Caching module graph hooks...
。。。。。。
17458 INFO: Appending archive to ELF section in EXE /root/practicec/dist/myhello
17648 INFO: Building EXE from EXE-00.toc completed successfully.
[root@localhost practicec]# ls
build dist myhello myhello.spec
[root@localhost practicec]# cd dist/
[root@localhost dist]# ls
myhello
[root@localhost dist]#
编译后会在原目录下生成许多附属文件,编译后的二进制文件就在disk目录下
然后我们将编译后的文件打包备用
[root@localhost practicec]# cd dist/
[root@localhost dist]# ls
myhello
[root@localhost dist]
rpm软件包制作
首先利用rpmdev-setuptree命令创建rpm制作空间,创作空间就会在用户home目录下产生(rpmbuild)
[root@localhost dist]# rpmdev-setuptree
[root@localhost dist]# cd ~
[root@localhost ~]# ls
anaconda-ks.cfg get-pip.py get-pip.py.1 practicec rpmbuild Shell test
[root@localhost ~]#
进入到rpmbuild目录下,将刚刚打包好的 . tar文件移动到rpmbuild/SOUECE目录下
[root@localhost ~]# cd rpmbuild/
[root@localhost rpmbuild]# ls
BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
[root@localhost rpmbuild]# cd SOURCES/
root@localhost SOURCES]# mv ../../practicec/dist/myhello.tar ./
[root@localhost SOURCES]# ls
myhello.tar
[root@localhost SOURCES]#
进入到rpmbuild/SPECS目录下,创建myhelllo.spec文件
[root@localhost ~]# cd rpmbuild/
[root@localhost rpmbuild]# ls
BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
[root@localhost rpmbuild]# cd SPECS/
[root@localhost SPECS]# touch myhello.spec
[root@localhost SPECS]# ls
myhello.spec
在myhello.spec文件里写入下列代码:
1 Name:myhello
2 Version:1.0
3 Release:0%{?dist}
4 Summary: myhello
5 Group:myhello
6 License:GPL
7 Source0:myhello.tar
8 BuildRoot:%(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
9 %description -l zh_CN
10 "this is my first build rpm about myhello"
11 %prep
12 echo "hello world!"
13 %setup -c -n myhello
14 %install
15 mkdir -p %{buildroot}/%{_usr}/bin
16 install myhello %{buildroot}/%{_usr}/bin/myhello
17 %files
18 %doc
19 /%{_usr}/bin/myhello
图示:
结束了上面的准备工作就可以将软件打包成rpm包
[root@localhost rpmbuild]# ls
BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
[root@localhost rpmbuild]# cd SPECS/
[root@localhost SPECS]# ls
myhello.spec
[root@localhost SPECS]# rpmbuild -ba myhello.spec
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.0B9fPK
+ umask 022
+ cd /root/rpmbuild/BUILD
+ echo 'hello world!'
hello world!
+ cd /root/rpmbuild/BUILD
+ rm -rf myhello
+ /usr/bin/mkdir -p myhello
+ cd myhello
+ /usr/bin/tar -xvvf /root/rpmbuild/SOURCES/myhello.tar
-rwxr-xr-x root/root 7209432 2020-03-15 05:17 myhello
+ /usr/bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0
Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.xpvVac
+ umask 022
+ cd /root/rpmbuild/BUILD
+ '[' /root/rpmbuild/BUILDROOT/myhello-1.0-0.el7.x86_64 '!=' / ']'
+ rm -rf /root/rpmbuild/BUILDROOT/myhello-1.0-0.el7.x86_64
++ dirname /root/rpmbuild/BUILDROOT/myhello-1.0-0.el7.x86_64
+ mkdir -p /root/rpmbuild/BUILDROOT
+ mkdir /root/rpmbuild/BUILDROOT/myhello-1.0-0.el7.x86_64
+ cd myhello
+ mkdir -p /root/rpmbuild/BUILDROOT/myhello-1.0-0.el7.x86_64//usr/bin
+ install myhello /root/rpmbuild/BUILDROOT/myhello-1.0-0.el7.x86_64//usr/bin/myhello
+ '[' '%{buildarch}' = noarch ']'
+ QA_CHECK_RPATHS=1
+ case "${QA_CHECK_RPATHS:-}" in
+ /usr/lib/rpm/check-rpaths
+ /usr/lib/rpm/check-buildroot
+ /usr/lib/rpm/redhat/brp-compress
+ /usr/lib/rpm/redhat/brp-strip /usr/bin/strip
+ /usr/lib/rpm/redhat/brp-strip-comment-note /usr/bin/strip /usr/bin/objdump
+ /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip
+ /usr/lib/rpm/brp-python-bytecompile /usr/bin/python 1
+ /usr/lib/rpm/redhat/brp-python-hardlink
+ /usr/lib/rpm/redhat/brp-java-repack-jars
Processing files: myhello-1.0-0.el7.x86_64
Provides: myhello = 1.0-0.el7 myhello(x86-64) = 1.0-0.el7
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Requires: libc.so.6()(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) libdl.so.2()(64bit) libdl.so.2(GLIBC_2.2.5)(64bit) libz.so.1()(64bit) rtld(GNU_HASH)
Checking for unpackaged file(s): /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/myhello-1.0-0.el7.x86_64
Wrote: /root/rpmbuild/SRPMS/myhello-1.0-0.el7.src.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/myhello-1.0-0.el7.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.ZrqouK
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd myhello
+ /usr/bin/rm -rf /root/rpmbuild/BUILDROOT/myhello-1.0-0.el7.x86_64
+ exit 0
图示:
利用rpm命令安装刚刚创建的包检验成果
[root@localhost SPECS]# cd ..
[root@localhost rpmbuild]# ls
BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
[root@localhost rpmbuild]# cd RPMS/
[root@localhost RPMS]# ls
x86_64
[root@localhost RPMS]# cd x86_64/
[root@localhost x86_64]# ls
myhello-1.0-0.el7.x86_64.rpm
[root@localhost x86_64]# rpm -i myhello-1.0-0.el7.x86_64.rpm
[root@localhost x86_64]# rpm -ql myhello
/usr/bin/myhello
[root@localhost x86_64]#
安装完成后尝试运行
[root@localhost x86_64]# myhello
成功访问1 次
成功访问2 次
成功访问3 次
成功访问4 次
成功访问5 次