SSH连接不是持久的

时间:2015-05-04 18:32:47

标签: python ssh paramiko

我有以下脚本将SSH连接到网络服务器并执行一些命令,由于某种原因SSH连接打开但是在命令执行时它关闭(我认为),结果命令失败,如下所示错误?任何人都可以提供有关如何使SSH连接持久的信息吗?

#!/usr/bin/python
import os
import sys
import json
import fileinput
import pwd
from subprocess import Popen, PIPE, STDOUT
import re
import paramiko
import MySQLdb

resource = r'qca-cdit-01'
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(resource, username='username', password='passwordname')
#chan = ssh.get_transport().open_session()
chan = ssh.invoke_shell()
chan.get_pty()

commandstringlist = \
    ['/local/mnt/workspace/LA_host_builds/AU_LINUX_ANDROID_LA.BF64.1.2.1_RB2.05.01.01.081.031_msm8992',
     'cd frameworks/base',
     'git fetch ssh://cdit@review-android.company.com:29418/platform/frameworks/base refs/changes/21/1260821/2 && git cherry-pick FETCH_HEAD']
for cmd_val in commandstringlist:
    #chan.exec_command(cmd_val)
    chan.send(cmd_val)
    print(chan.recv(1024))

错误:

Traceback (most recent call last):
  File "ssh_test.py", line 21, in <module>
    chan.get_pty()
  File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 60, in _check
    return func(self, *args, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 177, in get_pty
    self._wait_for_event()
  File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 1086, in _wait_for_event
    raise e
paramiko.ssh_exception.SSHException: Channel closed

4 个答案:

答案 0 :(得分:2)

使用exec_command执行的每个命令都有自己的通道,因此也有自己的上下文。该上下文包括工作目录。您在一个上下文中更改工作目录,然后尝试在另一个上下文中使用它。相反,对所有命令使用相同的通道。您可以打开频道并使用它,也可以一次发出所有命令。

commandstringlist = ['cd /local/mnt/workspace/test2 && cd data/log && git fetch ssh://username@review-android.company.com:29418/platform/data/log refs/changes/21/1260821/2 && git cherry-pick FETCH_HEAD']

以下是一些其他问题,可以更详细地解释这一点。

https://unix.stackexchange.com/questions/80821/why-does-cd-command-not-work-via-ssh https://superuser.com/questions/46851/keeping-working-directory-across-ssh https://stackoverflow.com/a/6770272/492773

答案 1 :(得分:2)

诀窍是在执行命令之前切换到目录。您可以轻松地将其集成到execute_command

def execute_command (cmd, pwd=None):
    if pwd:
        cmd = 'cd "%s";%s' % (pwd, cmd)
    print cmd
    si,so,se = ssh.exec_command(cmd) 
    print os.getcwd()
    print "printing so"
    soreadList = so.readlines()
    print soreadList
    print "printing se"
    errList = se.readlines()
    print errList

该命令由远程计算机上的shell运行,因此也可以添加设置环境变量等任何shell技巧。

答案 2 :(得分:0)

  

任何人都可以提供有关如何使SSH连接持久的信息吗?

以下是如何通过尝试执行命令然后在发生错误时重新连接然后执行命令来使SSH连接持久化的示例。

import paramiko

class sshConnection:
  def __init__( self, host, un, pw ):
    self.host = host
    self.un = un
    self.pw = pw

  def connect( self ):
    self.ssh = paramiko.SSHClient()
    self.ssh.load_system_host_keys()
    self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    self.ssh.connect(self.host, username=self.un, password=self.pw)

  def cmd ( self, cmd, tries=0 ):
    self.si, self.so, self.se = None, None, None
    try:
      self.si, self.so, self.se= self.ssh.exec_command(cmd)
    except Exception, e:
      if tries > 3:
        raise
      self.connect( )
      self.cmd(  cmd, tries+1 )
    return self.si, self.so, self.se



conn = sshConnection( "host","username","password" )

si, out, err = conn.cmd("ls -al")
print "\n".join(map ( lambda x: x.strip(), out.readlines()) )

答案 3 :(得分:0)

简单地说:

#!/usr/bin/python
import os
import sys
import json
import fileinput
import pwd
from subprocess import Popen, PIPE, STDOUT
import re
import paramiko
import MySQLdb

resource = r'qca-cdit-01'
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(resource, username='username', password='passwordname')
#chan = ssh.get_transport().open_session()
chan = ssh.invoke_shell()

commandstringlist = \
    ['\n/local/mnt/workspace/LA_host_builds/AU_LINUX_ANDROID_LA.BF64.1.2.1_RB2.05.01.01.081.031_msm8992\n',
     '\ncd frameworks/base\n',
     '\ngit fetch ssh://cdit@review-android.company.com:29418/platform/frameworks/base refs/changes/21/1260821/2 && git cherry-pick FETCH_HEAD\n']
for cmd_val in commandstringlist:
    #chan.exec_command(cmd_val)
    chan.send(cmd_val)
    print(chan.recv(8192))

对我有用。 (你忘了.send()不会自动添加行)