Python连接多个Linux服务器建立 SSH 连接,打开会话 Channel

该代码示例展示了如何使用Python的Paramiko库连接到多台Linux服务器,并通过SSH执行ping命令。程序创建SSH客户端,打开通道,执行命令并打印结果,最后关闭连接。这在自动化运维场景中非常有用。
摘要由CSDN通过智能技术生成



import paramiko
import select
import time

# 定义要连接的Linux服务器及账号信息
servers = [
    {"hostname": "IP1", "username": "root", "password": "root"},
    {"hostname": "IP2", "username": "root", "password": "root"}
]

# 定义要执行的命令
command = "ping www.baidu.com"

# 创建SSH客户端对象
ssh_clients = []
for server in servers:
    ssh = paramiko.Transport((server["hostname"], 22))
    ssh.connect(username=server["username"], password=server["password"])
    ssh_clients.append(ssh)

# 开始执行命令
channel0 = ssh_clients[0].open_channel(kind='session')
channel0.exec_command(command)

time.sleep(5)

channel1 = ssh_clients[1].open_channel(kind='session')
channel1.exec_command(command)

# 打印命令执行结果
result0 = channel0.recv(1024)
print(result0.decode())

result1 = channel1.recv(1024)
print(result1.decode())

# 关闭通道和连接
channel0.close()
channel1.close()


# 关闭所有连接
for ssh in ssh_clients:
    ssh.close()

'''
PING www.a.shifen.com (220.181.38.150) 56(84) bytes of data.
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=1 ttl=49 time=2.90 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=2 ttl=49 time=2.82 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=3 ttl=49 time=2.92 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=4 ttl=49 time=2.97 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=5 ttl=49 time=2.86 ms

PING www.a.shifen.com (110.242.68.3) 56(84) bytes of data.
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=1 ttl=49 time=71.5 ms

'''

ssh.open_channel(kind='session') 方法可以在已经建立 SSH 连接的基础上打开一个新会话 Channel。以下是一个使用方式的例子:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('hostname', username='username', password='password')

# 打开一个新的会话 Channel
channel = ssh.open_channel(kind='session')

# 在新的 Channel 中执行命令
channel.exec_command('ls -l')

# 获取命令的输出和错误信息
output = channel.recv(1024).decode()
error = channel.recv_stderr(1024).decode()

print(output)
print(error)

# 关闭 Channel 和 SSH 连接
channel.close()
ssh.close()

在该例子中,我们首先使用 Paramiko 库建立 SSH 连接。然后使用 open_channel(kind='session') 方法打开一个新的会话 Channel。接着,我们使用 exec_command 方法在新的 Channel 中执行了一个 ls -l 命令。最后,我们可以通过 recvrecv_stderr 方法获取命令的输出和错误信息。

需要注意的是,在使用 exec_command 方法执行命令之前,我们需要确保该命令不需要交互式输入信息。如果该命令需要交互式输入信息,那么我们需要使用 Paramiko 库提供的其他方法来完成这个需求。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Paramiko 库来实现 PythonSSH 连接,并且可以通过 `invoke_shell()` 方法来获取 SSH 会话的返回值。具体的步骤如下: 1. 安装 Paramiko 库: ```python pip install paramiko ``` 2. 导入 Paramiko 库: ```python import paramiko ``` 3. 创建 SSHClient 对象,并指定连接的主机名、用户名和密码: ```python ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='example.com', username='username', password='password') ``` 4. 打开 SSH 会话,并发送命令: ```python stdin, stdout, stderr = ssh.exec_command('ls -l') ``` 5. 获取命令执行结果: ```python output = stdout.read().decode('utf-8') ``` 6. 关闭 SSH 连接: ```python ssh.close() ``` 如果需要一直获取返回值,可以使用 `invoke_shell()` 方法来打开 SSH 会话的伪终端,然后通过 `recv()` 方法来获取返回值。具体的代码可以参考下面的示例: ```python import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='example.com', username='username', password='password') channel = ssh.invoke_shell() channel.send('ls -l\n') output = '' while not channel.recv_ready(): pass while channel.recv_ready(): output += channel.recv(1024).decode('utf-8') print(output) ssh.close() ``` 这个示例中,我们使用 `invoke_shell()` 方法打开 SSH 会话的伪终端,并发送 `ls -l` 命令。然后通过循环来等待返回结果,并将结果输出。最后关闭 SSH 连接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值