Python exec_command抛出未知命令:`ls'

时间:2018-05-30 13:09:12

标签: python-2.7 paramiko

我有一个非常简单的用例,因为未知原因而绊倒了我。请帮助。

def CustomRunSsh(cmd,vc,user,passw,timeout=120):
   exit_status = None
   stdout0 = None
   stderr0 = None
   try:
      ssh = paramiko.SSHClient()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect(vc, username=user, password=passw)
      chan = ssh.get_transport().open_session()
      chan.settimeout(10800)
      try:

         print ("Debug: The Command to be Run is "+cmd) #Deliberately put here to check the command passed to exec
         chan.exec_command(cmd)
         contents = StringIO.StringIO()
         error = StringIO.StringIO()
         while not chan.exit_status_ready():
            if chan.recv_ready():
               data = chan.recv(1024)
               # print "Indside stdout"
               while data:
                  contents.write(data)
                  data = chan.recv(1024)

            if chan.recv_stderr_ready():
               error_buff = chan.recv_stderr(1024)
               while error_buff:
                  error.write(error_buff)
                  error_buff = chan.recv_stderr(1024)

         exit_status = chan.recv_exit_status()
      except socket.timeout:
         raise socket.timeout

      stdout0 = contents.getvalue()
      stderr0 = error.getvalue()
   except Exception, e4:
      print("Error while connecting to remote session: %s" % str(e4))
   finally:
      return exit_status,stdout0,stderr0

现在我在做:

my_cmd = "ls -ltr"
(ret, stdout, stderr) = CustomRunSsh(my_cmd , host, username, password, timeout=120)

收益率:

Debug: The Command to be Run is ls -ltr
Unknown command: `ls'

我无法理解为什么会出现此错误。请帮助。

0 个答案:

没有答案