Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change directories using Paramiko?

Drush commands not executing using Paramiko

I posted the above question regarding a persistent error message that I receive using Paramiko. I do not think it is related to my next question, but it might be.

I can successfully connect to my server via SSH using Paramiko. I can execute commands like ls or pwd. What I can't seem to do is change directories. I can send the command "cd .." for example, but when I follow up with "pwd" it shows that I haven't changed directories. It just lists the initial directory I am in when I log in.

>>> stdin, stdout, stderr = myssh.exec_command("pwd") >>> stdout.readlines() ['/big/dom/home/myid\n'] >>> stdin, stdout, stderr = myssh.exec_command("cd ../") >>> stdout.readlines() [] >>> stdin, stdout, stderr = myssh.exec_command("pwd") >>> stdout.readlines() ['/big/dom/home/myid\n'] >>> 

Am I misunderstanding what is going on here? Should I not be able to change directories? Or if I can, should I be doing it in some other way than using exec_command?

like image 582
Mike Ryan Avatar asked Jan 19 '12 20:01

Mike Ryan


People also ask

What is Paramiko SSHClient ()?

SSH client & key policies class paramiko.client. SSHClient. A high-level representation of a session with an SSH server. This class wraps Transport , Channel , and SFTPClient to take care of most aspects of authenticating and opening channels.

What is Paramiko transport?

An SSH Transport attaches to a stream (usually a socket), negotiates an encrypted session, authenticates, and then creates stream tunnels, called channels , across the session. Multiple channels can be multiplexed across a single session (and often are, in the case of port forwardings).

What is Paramiko library in Python?

Paramiko is a Python library that makes a connection with a remote device through SSh. Paramiko is using SSH2 as a replacement of SSL to make a secure connection between two devices. It also supports the SFTP client and server model.


2 Answers

This guy had it figured out: http://www.vertigrated.com/blog/2010/02/python-remote-ssh-with-paramiko/

You just have to send multiple commands with one exec_command, such as:

myssh.exec_command('cd ..; pwd') 

Then stdout.readlines() will return the directory that you changed to.

like image 84
Mike Ryan Avatar answered Sep 16 '22 18:09

Mike Ryan


Well paramiko creates an instance of shell and all the commands that you wish to execute in paramiko have to be given in that instance of shell only.

For example: Let us say I have some folder in the directory I am in.

folder1 folder2 folder3 

Now if I want to cd into folder 1 and make a directory there what I would do is:

ssh.exec_command('cd folder1;mkdir folder4') 

if you write it like:

ssh.exec_command('cd folder1') ssh.exec_command('mkdir folder4') 

you would get the result like

folder1 folder2 folder3 folder4 

as those were two different instances of the shell and would be independent in their function.

like image 30
Aklank Jain Avatar answered Sep 16 '22 18:09

Aklank Jain