Issuing commands using "exec_command()" of paramiko AND also sending commands together

KR kavithabhaskaran2013 at gmail.com
Wed Jun 3 09:07:16 EDT 2015


Hi Sreenathan Nair:
import os, sys, 
import connectlibs as ssh
s = ssh.connect("xxx.xx.xx.xxx", "Admin", "Admin")
channel = s.invoke_shell()
channel.send("net use F: \\\\xyz.xy.xc.xa\\dir\n")
>>>32
channel.send("net use\n")
>>>7
channel.recv(500)
'Last login: Tue Jun  2 23:52:29 2015 from xxx.xx.xx.xx\r\r\n\x1b]0;~\x07\r\r\n\x1b[32mAdmin at WIN \x1b[33m~\x1b[0m\r\r\n$ net use F: \\\\xyz.xy.xc.xa\\dir\r\nSystem error 67 has occurred.\r\r\n\r\r\nThe network name cannot be found.\r\r\n\r\r\n\x1b]0;~\x07\r\r\n\x1b[32mAdmin at WIN \x1b[33m~\x1b[0m\r\r\n$ net use'
>>> 

Above is the what I have for code which is what I am encountering on my setup.

A. I need to be able to parse the o/p of 2 commands 1. "net use F: \\\\xyz.xy.xc.xa\\dir" and 2. "net use". 

B. When I use chan.recv(x) -> with certain 'x' bytes. What if I dont know what kind of a response I am expecting - I mean its not a usual response - if I give "x" bytes whereas in reality "x" could be 10000 bytes more than the ACTUAL number of bytes in which case Python hangs.

What do people use in such cases.

On Wednesday, June 3, 2015 at 12:10:08 AM UTC-7, Sreenathan Nair wrote:
> Hi,
> 
> 
> Could you be more specific about your problem? Perhaps an example of something similar to what you're trying to do would be helpful.
> 
> 
> Usually the process is to instantiate paramiko.SSHCLIENT, use the connect() method with desired parameters and execute commands using the exec_command(). If you'd like to process the output of the command execution, then you would store the result of exec_command() into three variables (it return a 3-tuple of Channel objects).
> 
> 
> i.e com_stdin, com_stdout, com_stderr = my_ssh_client_instance.exec_command("<command>")
> 
> 
> The instance of SSHCLIENT is live until the close() method is called. Meaning subsequent commands can be executed the same way.
> 
> 
> 
> 
> On Wed, Jun 3, 2015 at 8:07 AM, Pythonista <kukki.k... at gmail.com> wrote:
> Using paramiko's exec_command(), i would like to send a command, process its output and do it for several other commands. I notice that its not quick enough or something like that.
> 
> How would I handle that scenario AND also providing multiple commands together (there is 1 post on stackoverflow for issuing multiple commands but I am not sure if someone has tried it. It didnt work for me!
> 
> Thanks!
> --
> https://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list