2

I have the following program using Paramiko:

#!/usr/bin/env python

import paramiko

hostname = '192.168.1.12'
port = 22
username = 'root'
password = 'whatl0ol'

if __name__ == "__main__":
    paramiko.util.log_to_file('paramiko.log')
    ssh = paramiko.SSHClient()
    ssh.load_system_host_keys()
    ssh.connect(hostname, port, username, password)             

while True:
      pick = raw_input("sshpy: ")
      stdin, stdout, stderr = ssh.exec_command(pick)
      print stdout.readlines() 

But when I connect and try to use cd, it doesn't work. How can I fix this?

jww
  • 97,681
  • 90
  • 411
  • 885
tornike
  • 23
  • 1
  • 7

3 Answers3

3

It looks like you are implementing some kind of interactive program that allows executing a sequence of commands on the server.

The SSHClient.exec_command executes each command in a separate "exec" channel. The individual commands run in their own environment. So if you execute cd command, it has no effect at all on subsequent commands. They will again start in user's home directory.

If you want to implement an interactive shell session, use SSHClient.invoke_shell.
For an example, see how to interact with Paramiko's interactive shell session?

See also Execute multiple commands in Paramiko so that commands are affected by their predecessors.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

Paramiko SSH_Client opens a new session and executes the command in that session and once command execution gets completed, the session channel is closed.

Executing 'cd' command would have been done in the first session and later on, for the next command the session would start again from home directory.

If you want to hold the session, use invoke_shell for an interactive session.

  • Starting an interactive session is somewhat overboard -- for the simple case, `exec_command('cd /foo && command_to_run_in_foo')` (ideally with `shlex.quote()` or `pipes.quote()` used to make the directory name safe to substitute into a string parsed as a shell command) suffices. – Charles Duffy Dec 21 '18 at 01:36
0

I needed to change directories and run an executable. I have to do this all in one command. The client unit was a windows 10 machine. The cmd shell in windows is soo problematic! Commands are different. ';' between commands doesn't work. You need to use '&'. cd d:/someDirectory doesn't work. You need '/d'. 'pwd' doesn't work. Also, echo%cd% to pwd doesn't work reliably. 'cd' with no parameters for pwd does work reliably. I was hoping the not working list would save you time. This is where it landed.

cmd = 'cd /d D:\someDirectory & SomeExecutable.exe 
      someParameter'
ssh_stdin, ssh_stdout, ssh_stderr = 
ssh.exec_command(cmd_1_to_execute)

To check directory change use the following:

cmd = 'cd /d D:\someDirectory & cd'
ssh_stdin, ssh_stdout, ssh_stderr = 
ssh.exec_command(cmd_1_to_execute)

output = ssh_stdout.readline()
error = ssh_stderr.readline()
print("output: " + output)
print("error: " + error)
OW-52
  • 1