Python与Linux的完美契合:打造你的开发与管理利器

下面是一个封装了连接多个Linux服务器的示例类,可通过三方库paramiko在自己windows机器本机操作linux系统的机器,执行相对应的命令

import paramiko

class MultiLinuxConnection:

    def __init__(self):

        self.connections = []
 
    def add_connection(self, host, port, username, password):

        """

        添加Linux服务器连接。

        """

        connection = paramiko.SSHClient()     # 实例化SSHClient

        connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())    # 自动添加策略,保存服务器的主机名和密钥信息,如果不添加,那么不再本地know_hosts文件中记录的主机将无法连接

        connection.connect(host, port=port, username=username, password=password)  # 连接SSH服务端,以用户名和密码进行认证

        self.connections.append(connection)


    def remove_connection(self, index):

        """

        移除指定索引处的Linux服务器连接。

        """

        if index < len(self.connections):

            connection = self.connections.pop(index)

            connection.close()

    def execute_command(self, index, command):

        """

        在指定索引处的Linux服务器上执行命令并返回输出结果。

        """

        if index < len(self.connections):

            connection = self.connections[index]

            stdin, stdout, stderr = connection.exec_command(command)  #执行命令

            output = stdout.read().decode().strip()

            return output

    def execute_command_all(self, command):

        """

        在所有连接的Linux服务器上执行命令并返回输出结果的字典。

        """

        output_dict = {}

        for index, connection in enumerate(self.connections):

            output_dict[index] = self.execute_command(index, command)

        return output_dict


    def close_all_connections(self):

        """

        关闭所有连接的Linux服务器。

        """

        for connection in self.connections:

            connection.close()

        self.connections = []

if __name__ == '__main__':
    pass
    multi_linuxs = MultiLinuxConnection()
    multi_linuxs.add_connection("hostname1", 22, "username1", "password1")   #索引0 机器
    multi_linuxs.add_connection("hostname2", 22, "username2", "password2")  #索引为1 机器

    output = multi_linuxs.execute_command(0, "df -h")  索引从0开始
    print(output)  # 执行索引为0的连接上的命令并打印输出结果

    output_all = multi_linuxs.execute_command_all("ifconfig")
    print(output_all)  # 执行所有连接上的config命令,并打印输出结果的字典
    multi_linuxs.remove_connection(1) # 移除索引为1的连接
    multi_linuxs.close_all_connections()  # 关闭所有连接的SSH连接

这个示例中的MultiLinuxConnection类有以下几个方法:

add_connection: 添加Linux服务器连接,在给定的主机、用户名和密码下建立SSH连接。

remove_connection: 移除指定索引处的Linux服务器连接,并关闭该SSH连接。

execute_command: 在指定索引处的Linux服务器上执行命令,并返回输出结果。

execute_command_all: 在所有连接的Linux服务器上执行命令,并返回包含输出结果的字典。

close_all_connections: 关闭所有连接的Linux服务器的SSH连接。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

经历一个春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值