SSH 使用 paramiko 和 exec_command
SSH using paramiko and exec_command
尝试使用 SSH 和 paramiko 从 windows 服务器通过 SSH 连接到 Linux 远程连接,仅 'pwd' 此服务器使用以下代码(我已经更改了 user/password 和来自真实代码的服务器 IP):
import paramiko
from paramiko import SSHClient, AutoAddPolicy
LOCAL_IP=IP
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(LOCAL_IP,username=username, password=password)
client.exec_command('pwd')
我得到以下结果:
(<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=24576 -> <paramiko.Transport at 0x6716710L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>,
<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=24576 -> <paramiko.Transport at 0x6716710L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>,
<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=24576 -> <paramiko.Transport at 0x6716710L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>)
SSH 连接已打开,我可以通过 linux 服务器上的 netstat 得知我正在尝试通过 SSH 连接到....
client.exec_command returns stdin、stdout 和 stderr 流的元组 - 您需要分配这些流并在其上调用 read() 以获得输出:
import paramiko
from paramiko import SSHClient, AutoAddPolicy
LOCAL_IP=IP
PORT=your_port
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(LOCAL_IP, PORT, username="username", password="password")
stdin, stdout, stderr = client.exec_command("pwd")
print(stdout.read())
尝试使用 SSH 和 paramiko 从 windows 服务器通过 SSH 连接到 Linux 远程连接,仅 'pwd' 此服务器使用以下代码(我已经更改了 user/password 和来自真实代码的服务器 IP):
import paramiko
from paramiko import SSHClient, AutoAddPolicy
LOCAL_IP=IP
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(LOCAL_IP,username=username, password=password)
client.exec_command('pwd')
我得到以下结果:
(<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=24576 -> <paramiko.Transport at 0x6716710L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>,
<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=24576 -> <paramiko.Transport at 0x6716710L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>,
<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=24576 -> <paramiko.Transport at 0x6716710L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>)
SSH 连接已打开,我可以通过 linux 服务器上的 netstat 得知我正在尝试通过 SSH 连接到....
client.exec_command returns stdin、stdout 和 stderr 流的元组 - 您需要分配这些流并在其上调用 read() 以获得输出:
import paramiko
from paramiko import SSHClient, AutoAddPolicy
LOCAL_IP=IP
PORT=your_port
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(LOCAL_IP, PORT, username="username", password="password")
stdin, stdout, stderr = client.exec_command("pwd")
print(stdout.read())