0

I have a very simple use case that is stumbling me for reasons unknown. Kindly help.

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

Now I am doing:

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

That Yields:

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

I am not able to understand why this error is coming. Kindly help.

May
  • 1,158
  • 2
  • 13
  • 24

0 Answers0