I've seen other posts about this, but no answers.  My desire is to run
an SSH session in a scripted fashion.  As one example, I would like to
be able to do this:

client = paramiko.SSHClient()
client.connect(...)
client.exec_command('cd directory_name')
client.exec_command('ls')

However, due to the way exec_command() works it automatically creates
a new session/channel every time.  That means the results of the first
command (changing the working directory) does not affect the second
command (so the 'ls' wont give me the right directory listings).

So the next step is to look at the transport & channel objects.  Now I
have this:

transport = client.get_transport()
session = transport.open_session()
session.exec_command('cd directory_name')
session.exec_command('ls')

However, in this case I get an error because of this:
"When the command finishes executing, the channel will be closed and
can't be reused. You must open a new channel if you wish to execute
another command."

I know I can do something like this:
client.exec_command('cd directory_name; ls')

However, that means I cannot use the results of one command to
influence another.  For example, if I wanted to 'ls file_name' to test
if a file existed, then do different things depending on if it was
there or not, I wouldn't be able to.

So my question is this: how do I execute multiple commands on a single
session/channel?  I've seen others ask the same question, and so it
seems like there should be an obvious way to do this that I just don't
understand.

Please note: I do not want an interactive shell, unless that is the
preferred way for python to script commands in this scenario.  I don't
want user input from the local computer (which is what I'm assuming
the invoke_shell() command is intended for).  I don't want to run 'cd
directory_name; ls; cmd; cmd; cmd;', since as I stated above that is
not sufficient for what I need.  I would also like to avoid doing
adding 'ls directory_name' in front of every single command, since
that would also have to apply to things like environment variables,
etc.  I'm truly looking for a way to execute multiple commands on a
single channel.

Thanks, any help is appreciated!

_______________________________________________
paramiko mailing list
[email protected]
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko

Reply via email to