python - Use the same SSH object to issue "exec_command()" multiple times in Paramiko -


i want use same ssh object issue exec_command() multiple times in paramiko module in python.

the objective output same session.

is there way it? exec_command() closes channel once completes executing command , thereafter new ssh object needed execute following command .. sessions differ not want.

code

import os, sys,  import connectlibs 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") >>>7 channel.recv(500) 'last login: tue jun  2 23:52:29 2015 xxx.xx.xx.xx\r\r\n\x1b]0;~\x07\r\r\n\x1b[32madmin@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 found.\r\r\n\r\r\n\x1b]0;~\x07\r\r\n\x1b[32madmin@win \x1b[33m~\x1b[0m\r\r\n$ net use' >>>  

an ssh session can have multiple channels indeed (but paramiko possibly not support it).

but session seem imagine "shell session". that's not ssh session is. channel actually, corresponds "shell session".

in other words, if open multiple "exec" channels paramiko on same ssh connection (session) , call exec_command on these, commands executed in different shell session. won't you.


you can test putty ssh client. recent versions support connection sharing, means can have more putty windows (each using own channel) on single ssh connection/session. if execute command in 1 putty window, , commands changes environment (like environment variable or current working directory), change won't reflected other putty window, if share same ssh connection.


so need execute commands in 1 channel. depending on needs (which still not clear), need use "exec" or "shell" channel.

in either case have troubles determining, output of 1 command ends , output of other command starts share same "stream".

you can solve inserting unique separator (string) in between , search in channel output stream.

channel = ssh.invoke_shell() channel.send('ls\n') channel.send('echo unique-string-separating-output-of-the-commands\n') channel.send('pwd\n') 

Comments