How do I change directories using Paramiko?

后端 未结 4 1516
-上瘾入骨i
-上瘾入骨i 2020-12-08 04:04

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 rela

相关标签:
4条回答
  • 2020-12-08 04:37

    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.

    0 讨论(0)
  • 2020-12-08 04:48

    As of version 2.6, if you use the SFTPClient, the method to change directories is sftp = myssh.open_sftp() sftp.chdir('path/to/directory')

    0 讨论(0)
  • 2020-12-08 04:54

    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.

    0 讨论(0)
  • 2020-12-08 04:56

    A bit late with this one, but its possible to 'invoke_shell' and write to the standard input through a file.

    Please see: https://stackoverflow.com/a/6203877/1861353

    Seems a little bit heavyweight since you can just ';'.join(cmdlist) and send to the exec_command.

    0 讨论(0)
提交回复
热议问题