【问题标题】:Changing directory in a Windows SSH connection using Python使用 Python 在 Windows SSH 连接中更改目录
【发布时间】:2015-01-19 11:18:13
【问题描述】:

这是我的 Windows PC 通过 SSH(Cygwin 服务器在 LAN 桌面上运行)连接到 Windows LAN 桌面的代码:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('135.24.237.178',username = 'cyg_server',password = 'sandforce')

我能够成功连接。但是,现在如果我这样做:

command = "cd c:\;dir"
stdin,stdout,stderr = ssh.exec_command(command)
stdout.readlines()

那么 pyscripter 不会输出任何东西。谁能告诉我为什么以及如何使这段代码工作?

【问题讨论】:

标签: python python-2.7 python-3.x ssh windows-shell


【解决方案1】:

我无法复制您的问题。我正在使用此代码:

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username = user, password = pw)
command = "cd c:\;dir"
stdin, out, err = client.exec_command(command)
print "stdout: " + out.read()

输出:

stdout: 

    Directory: C:\


Mode                LastWriteTime         Length Name                                             
----                -------------         ------ ----                                             
d-----        9/12/2016   7:34 AM                Logs                                             
d-----        9/26/2018   1:33 PM                Microsoft                                        
d-----        9/26/2018  11:47 AM                OpenSSH-Win32                                    
d-r---        9/27/2018   3:57 AM                Program Files                                    
d-----        9/26/2018  11:43 AM                Program Files (x86)                              
d-----        9/27/2018   3:56 AM                Python27                                         
d-----        9/26/2018  12:27 PM                support                                          
d-----        9/26/2018  12:27 PM                TEMP                                             
d-r---        9/27/2018   7:40 AM                Users                                            
d-----        9/27/2018   3:58 AM                Windows                                          
-a----        9/26/2018  11:42 AM            435 adrights.log                                     
-a----        9/27/2018   7:40 AM           6532 rshd.log                                         
-a----        9/26/2018  11:42 AM            107 rshdinst.log                                     
-a----        9/26/2018  11:47 AM            849 rshds.log                                        
-a----        9/26/2018  11:43 AM              0 validate.log 

我知道这不是答案,但它不适合 cmets。 cmets 中提供的链接似乎也不是特别有用。这是我处理这类事情的常规代码:

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username = user, password = pw)
channel = client.get_transport().open_session()
command = "cd c:\;dir"
channel.exec_command(command)
out = channel.makefile().read()
err = channel.makefile_stderr().read()
returncode = channel.recv_exit_status()
channel.close()                       # channel is closed, but not the client

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多