Skip to main content

这是一个Linux管理脚本的基础库,通过对Linux基本功能进行封装,实现快速开发的效果

Project description

PythonLinuxBasicModule

由于工作中需要编写大量的Linux平台管理脚本、而其中大部分代码都是重复的,所以为了更好的开发效率,我决定将日常Linux管理脚本中用到的基础功能集合起来并使用开源都方式共享,同时也希望有更多人能够一起完善。

简介

PythonLinuxBasicModule Gitee项目(Python Linux基础模块: plbm)是使用Python3进行编写的一个开源系统管理工具, 通过封装Linux系统都软件包管理、磁盘管理、文件管理、网络管理、安全管理、服务管理等内容从而实现快速开发的效果。

特色

  • 使用全中文注释,即使小白也能轻松上手
  • 完全使用内置模块进行开发,拿来即用
  • 使用Python基础语法进行编写,兼容新旧版本Python3,告别语法冲突(例如3.5及以下版本无法使用f"{}"语法)
  • 完全开源、永久免费

更新内容

1.2.9

  • 默认关闭权限验证文本日志功能

使用方法

安装

具体可以访问Pypi项目地址https://pypi.org/project/plbm

安装-作为系统/用户模块

pip3 install --upgrade plbm ColorInfo -i https://pypi.tuna.tsinghua.edu.cn/simple

安装-作为项目模块

直接进入你的项目根目录,然后执行下面的命令即可

git clone https://gitee.com/liumou_site/PythonLinuxBasicModule.git
cd PythonLinuxBasicModule
python3 install.py

Demo

包管理

root@l:~/data/git/PythonLinuxBasicModule/demo# cat package.py 
# -*- encoding: utf-8 -*-
"""
@File    :   demo.py
@Time    :   2022-10-24 22:45
@Author  :   坐公交也用券
@Version :   1.0
@Contact :   faith01238@hotmail.com
@Homepage : https://liumou.site
@Desc    :   当前文件作用
"""
from plbm import PackageManagement
from ColorInfo import ColorLogger


class ServiceManager:
	def __init__(self, pac, password):
		self.package = pac
		self.pac = PackageManagement(password=password, package="vsftpd")
		self.logger = ColorLogger(class_name=self.__class__.__name__)

	def remove(self):
		if self.pac.installed():
			self.logger.info(f"已安装: {self.package}")
			if self.pac.uninstall():
				self.logger.info(f"卸载成功: {self.package}")
			else:
				self.logger.error(f"卸载失败: {self.package}")

		else:
			self.logger.warning(f"未安装: {self.package}")


if __name__ == "__main__":
	demo = ServiceManager(pac="vsftpd", password="demo")
	demo.remove()
root@l:~/data/git/PythonLinuxBasicModule/demo# python3 package.py 
/usr/lib/python3/dist-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.26.12) or chardet (3.0.4) doesn't match a supported version!
  RequestsDependencyWarning)
2023-01-06 22:42:31 Jurisdiction.py  line: 79 - Class: Jurisdiction Function: verification - INFO : 已处于root权限
2023-01-06 22:42:31 Cmd.py  line: 188 - Class: ComMand Function: getout - DEBUG : dpkg -s vsftpd
2023-01-06 22:42:31 Package.py  line: 141 - Class: PackageManagement Function: installed - INFO : 已安装: vsftpd
2023-01-06 22:42:31 package.py  line: 23 - Class: ServiceManager Function: remove - INFO : 已安装: vsftpd
2023-01-06 22:42:31 Dpkg.py  line: 113 Function: uninstall - INFO : UnInstalling UnInstall vsftpd ...
(Reading database ... 92009 files and directories currently installed.)
Removing vsftpd (3.0.3-12) ...
Purging configuration files for vsftpd (3.0.3-12) ...
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for systemd (241-7~deb10u8) ...
[ UnInstall vsftpd ] 执行成功
2023-01-06 22:42:36 package.py  line: 25 - Class: ServiceManager Function: remove - INFO : 卸载成功: vsftpd

服务管理-Demo

root@l:~/data/git/PythonLinuxBasicModule# cat demo.py 
# -*- encoding: utf-8 -*-
"""
@File    :   demo.py
@Time    :   2022-10-24 22:45
@Author  :   坐公交也用券
@Version :   1.0
@Contact :   faith01238@hotmail.com
@Homepage : https://liumou.site
@Desc    :   当前文件作用
"""
from plbm import ServiceManagement
from ColorInfo import ColorLogger


class ServiceManager:
	def __init__(self, services, password):
		self.services = services
		self.manager = ServiceManagement(service=services, password=password)
		self.logger = ColorLogger(class_name=self.__class__.__name__)

	def restart(self):
		if self.manager.restart(reload=True):
			self.logger.info("服务重启成功")
		else:
			self.logger.warning("服务重启失败")


if __name__ == "__main__":
	demo = ServiceManager(services="vsftpd", password="demo")
	demo.restart()
root@l:~/data/git/PythonLinuxBasicModule# python3 demo.py 
/usr/lib/python3/dist-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.26.12) or chardet (3.0.4) doesn't match a supported version!
  RequestsDependencyWarning)
[ 重载服务配置 ] 执行成功
[ Restart vsftpd ] 执行成功
2023-01-06 22:36:41 Service.py  line: 141 - Class: ServiceManagement Function: restart - INFO : 重启成功: vsftpd
2023-01-06 22:36:41 demo.py  line: 23 - Class: ServiceManager Function: restart - INFO : 服务重启成功
root@l:~/data/git/PythonLinuxBasicModule# 

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plbm-1.3.4.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

plbm-1.3.4-py3-none-any.whl (36.3 kB view details)

Uploaded Python 3

File details

Details for the file plbm-1.3.4.tar.gz.

File metadata

  • Download URL: plbm-1.3.4.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.3

File hashes

Hashes for plbm-1.3.4.tar.gz
Algorithm Hash digest
SHA256 f2d08ff43417dae2d7f6436958edbb4f02892d3da9b482d6a54ef8ef372b5808
MD5 255c4a4d066093c4592f270f350284a7
BLAKE2b-256 8c330719b759d3f94f0b697d2e59d422ce2a025ded8c4b16566fcaf3269ac9c6

See more details on using hashes here.

File details

Details for the file plbm-1.3.4-py3-none-any.whl.

File metadata

  • Download URL: plbm-1.3.4-py3-none-any.whl
  • Upload date:
  • Size: 36.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.3

File hashes

Hashes for plbm-1.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9d789c1930a67618294db590cdc569491c67da6ad5596650e6f0ee8685087d9f
MD5 3a1574b53987759e7abbfd449a6e0104
BLAKE2b-256 af048f1bed84583032aa8bdf6f1b40bac2eac1a32bca3e2ea109b58e6c811b3d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page