gpt4 book ai didi

python - Paramiko 不执行命令或 shell - Python

转载 作者:太空宇宙 更新时间:2023-11-04 09:11:15 25 4
gpt4 key购买 nike

我正在使用 Paramiko在两个服务器之间运行 ssh 命令的库。我的“主” 服务器是一台 Fedora 18 机器,而我的“从” 是一台 Windows 7 机器。为了在 Windows 7 机器上运行 ssh 服务器,我安装了 FreeSSHd .配置freesshd后,我可以从Fedora 18机器ssh进入Windows机器并执行类似dir的命令。但是,当我尝试使用 Paramiko 这样做时,我没有同样的运气:

import paramiko
import os
import errno

"""
written by Philippe Ribeiro

class Connection
stabilishes the ssh connection between two servers
allowing the executing of commands remotely
"""
class Connection:

def __init__( self, hostname, username, password):
"""Create a ssh client using the paramiko library
"""
self.host = hostname
self.user = username
self.password = password
self.port = 23
self.sshclient = paramiko.SSHClient()
self.username = username
self.password = password

try:
self.sshclient.load_system_host_keys()
self.sshclient.set_missing_host_key_policy( paramiko.AutoAddPolicy() )

self.sshclient.connect( hostname=self.host, port=self.port,
username=self.username, password=self.password )

self.sftpclient = self.sshclient.open_sftp()

except SSHException, e:
print e

except AuthenticationException, e:
print e

except BadHostKeyException, e:
print e

def close(self):
"""Closes a client using the Connection class' API
"""
self.sshclient.close()
return

def remove_directory(self, path):
"""Remove remote directory that may contain files
It does not support directories that contain subdiretories
"""
if self.exists(path):
for filename in self.sftpclient.listdir(path):
filepath = os.path.join(path, filename)
self.sfptclient.remove(filepath)
self.sftpclient.rmdir(path)

return

def exists(self, path):
"""Return True if the remote path exists
"""
try:
self.sfptclient.stat(path)

except IOError, e:
if e.errno == errno.ENOENT:
return False
raise
else:
return True

def put_directory(self, localdir, remotedir):
"""Put a directory of files on the remote server
Create the remote directory if it does not exist
Does not support directories that contain subdirectories
Return the number of files transferred
"""
if not self.exists(remotedir):
self.sftp.client.mkdir(remotedir)
count = 0
for filename in os.listdir(localdir):
self.sftpclient.put(
os.path.join(localdir, filename),
os.path.join(remotedir, filename)
)
count += 1
return count

"""
Just for testing purposes
try to connect to a Windows machine from a Unix one
so it can execute commands remotely
"""
cmd = ["cd ..", "dir" ]
client = Connection("hostname", "username", "password")

try:
for c in cmd:
stdin, stdout, stderr = client.sshclient.exec_command(c)
print stdout.readlines()

except SSHException, e:
print e

client.close()

但是当我执行时:

python client.py

我得到:

['Unable to execute command or shell on remote system: Failed to Execute process.\r\n']
['Unable to execute command or shell on remote system: Failed to Execute process.\r\n']

有人可以告诉我为什么不起作用吗?我错过了什么吗?

最佳答案

cmd = ["cd .., "dir"] 替换为 cmd = ['cmd.exe/c "cd C:\&& dir"' ]

关于python - Paramiko 不执行命令或 shell - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14863659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com