Paramiko未执行命令或shell-Python
我正在使用库在两台服务器之间运行Paramiko未执行命令或shell-Python,python,ssh,Python,Ssh,我正在使用库在两台服务器之间运行ssh命令。 我的“主”服务器是一个Fedora18盒子,我的“从”是一台Windows7机器。除了在windows7机器上运行ssh服务器,我安装了。配置freeshd后,我可以ssh从Fedora18机器进入Windows机器,并执行类似dir的命令。 然而,当我尝试使用Paramiko这样做时,我的运气就不一样了: import paramiko import os import errno """ written by Philippe Ribeiro
ssh
命令。
我的“主”
服务器是一个Fedora18
盒子,我的“从”
是一台Windows7
机器。除了在windows7
机器上运行ssh
服务器,我安装了。配置freeshd
后,我可以ssh
从Fedora18机器进入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”]
用cmd=[“cd….”,“dir”]
替换cmd=['cmd.exe/c“cd c:\&&dir”]
好例子@Yevgen。只是澄清一下-例如,如果您当前的目录位于带有字母D的驱动器上,并且您希望cd指向C:\,那么cmd=['cmd.exe/C“C:&&dir”']
。没有cd和反斜杠,这是一个很好的例子@Yevgen。只是澄清一下-例如,如果您当前的目录位于带有字母D的驱动器上,并且您希望cd指向C:\,那么cmd=['cmd.exe/C“C:&&dir”']
。不带cd和反斜杠