跳转到主要内容

远程系统和服务管理工具

项目描述

远程资源管理

简介

此工具帮助您管理运行在远程机器上的远程机器和服务。它针对基于Linux的机器。所有操作都通过SSH连接完成,这意味着必须在该机器上已运行SSH服务器。

from rrmngmnt import Host, RootUser

h = Host("10.11.12.13")
h.users.append(RootUser('123456'))

exec = h.executor()
# Run with sudo
exec = h.executor(sudo=True)

print exec.run_cmd(['echo', 'Hello World'])

使用SSH密钥进行身份验证

from rrmngmnt import Host, UserWithPKey

h = Host("10.11.12.13")
user = UserWithPKey('user', '/path/to/pkey'))

h.executor(user).run_cmd(['echo', 'Use pkey for auth instead of password'])

使用禁用算法的SSH密钥与paramiko SSHClient连接(在连接到使用旧SSH的机器时使用)

from rrmngmnt import Host, UserWithPKey, RemoteExecutorFactory

h = Host("10.11.12.13")
h.executor_factory = RemoteExecutorFactory(disabled_algorithms=dict(pubkeys=['rsa-sha2-256', 'rsa-sha2-512'])
user = UserWithPKey('user', '/path/to/pkey'))

h.executor(user).run_cmd(['echo', 'Use pkey and disabled algorithms for old openSSH connection'])

使用SSH ProxyCommand .. code:: python

proxy_command = ‘some proxy command’ h = Host(hostname=”hostname”) host.executor_factory = ssh.RemoteExecutorFactory(sock=proxy_command) h.executor(user).run_cmd([‘echo’, ‘Use SSH with ProxyCommand’])

功能

提供用于管理机器上资源接口的列表和示例。

文件系统

基本文件操作,您可以在这里找到与文件相关的 Python 'os' 模块的子集。

print h.fs.exists("/path/to/file")
h.fs.chown("/path/to/file", "root", "root")
h.fs.chmod("/path/to/file", "644")
h.fs.unlink("/path/to/file")

此外,还有方法可以从/向远程系统或从本地系统获取/放置文件。

h.fs.get("/path/to/remote/file", "/path/to/local/file/or/target/dir")
h.fs.put("/path/to/local/file", "/path/to/remote/file/or/target/dir")

有一种特殊的方法允许在主机之间传输文件。

h1.fs.transfer(
    "/path/to/file/on/h1",
    h2, "/path/to/file/on/h2/or/target/dir",
)

您还可以挂载设备。

with h.fs.mount_point(
    '//example.com/share', opts='ro,guest',
    fstype='cifs', target='/mnt/netdisk'
) as mp:
    h.fs.listdir(mp.target) # list mounted directory
    mp.remount('rw,sync,guest') # remount with different options
    h.fs.touch('%s/new_file' % mp.target) # touch file

防火墙

允许管理防火墙配置。检查主机上正在运行的防火墙服务(firewalld/iptables)并配置此服务。

h.firewall.is_active('iptables')
h.firewall.chain('OUTPUT').list_rules()
h.firewall.chain('OUTPUT').add_rule('1.1.1.1', 'DROP')

网络

它允许管理网络配置。

print h.network.hostname
h.network.hostname = "my.machine.org"
print h.network.all_interfaces()
print h.network.list_bridges()

软件包管理

它封装了各种软件包管理工具。它能够确定要使用哪种软件包管理工具。您仍然可以明确指定软件包管理工具。

已实现的管理

  • APT

  • YUM

  • DNF

  • RPM

# install htop package using implicit management
h.package_management.install('htop')
# remove htop package using rpm explicitly
h.package_management('rpm').remove('htop')

系统服务

您可以根据需要切换系统服务,它封装了各种服务管理。在大多数情况下,它能够确定要使用哪种服务管理。

已实现的管理

  • Systemd

  • SysVinit

  • InitCtl

if h.service('httpd').status():
    h.service('httpd').stop()
if h.service('httpd').is_enabled():
    h.service('httpd').disable()

操作系统信息

主机提供了 os 属性,允许获取基本操作系统信息。请注意,os.release_info 依赖于 systemd 初始化系统。

print h.os.distribution
# Distribution(distname='Fedora', version='23', id='Twenty Three')

print h.os.release_info
# {'HOME_URL': 'https://fedoraproject.org/',
#  'ID': 'fedora',
#  'NAME': 'Fedora',
#  'PRETTY_NAME': 'Fedora 23 (Workstation Edition)',
#  'VARIANT': 'Workstation Edition',
#  'VARIANT_ID': 'workstation',
#  'VERSION': '23 (Workstation Edition)',
#  'VERSION_ID': '23',
#  ...
# }

print h.os.release_str
# Fedora release 23 (Twenty Three)

存储管理

它目前处于进行中状态。计划包括NFS & LVM服务。

电源管理

让您有机会控制主机的电源状态,您可以重新启动、开机、关机主机并获取主机的电源状态。

已实现的管理

  • SSH

  • IPMI

ipmi_user = User(pm_user, pm_password)
ipmi_params = {
    'pm_if_type': 'lan',
    'pm_address': 'test-mgmt.testdomain',
    'user': ipmi_user
}
h.add_power_manager(
    power_manager.IPMI_TYPE, **ipmi_params
)
# restart host via ipmitool
h.power_manager.restart()

需求

  • paramiko

  • netaddr

  • six

安装

python setup.py devop

测试

tox

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分发

python-rrmngmnt-0.1.32.zip (84.8 kB 查看哈希)

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面