如何使用Paramiko更改目录?

时间:2021-12-01 03:45:00

Drush commands not executing using Paramiko

Drush命令没有使用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.

我发布了上述有关使用Paramiko收到的持久性错误消息的问题。我不认为这与我的下一个问题有关,但可能是。

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.

我可以使用Paramiko通过SSH成功​​连接到我的服务器。我可以执行像ls或pwd这样的命令。我似乎无法做的是更改目录。我可以发送命令“cd ..”,但是当我跟进“pwd”时,它表明我没有更改目录。它只列出了我登录时的初始目录。

>>> 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?

我误解了这里发生了什么吗?我应该无法更改目录吗?或者,如果可以的话,我应该以除了使用exec_command之外的其他方式来做这件事吗?

--

I can't answer my own question for another 7 hours, so here's the answer:

再过7个小时我无法回答自己的问题,所以答案是:

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

这个家伙弄清楚了: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:

您只需使用一个exec_command发送多个命令,例如:

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

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

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

然后stdout.readlines()将返回您更改的目录。

4 个解决方案

#1


31  

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

这个家伙弄清楚了: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:

您只需使用一个exec_command发送多个命令,例如:

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

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

然后stdout.readlines()将返回您更改的目录。

#2


4  

As of version 2.1+ the method to change directories is sftp.chdir('path/to/directory')

从版本2.1+开始,更改目录的方法是sftp.chdir('path / to / directory')

#3


3  

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.

好吧,paramiko创建了一个shell实例,你希望在paramiko中执行的所有命令都只能在shell的实例中给出。

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:

现在,如果我想进入文件夹1并在那里创建一个目录,我会做的是:

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.

因为那些是shell的两个不同实例,并且它们的功能是独立的。

#4


2  

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

这个有点晚了,但它可以'invoke_shell'并通过文件写入标准输入。

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

请参阅:https://*.com/a/6203877/1861353

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

看起来有点重量级,因为你可以';'。join(cmdlist)并发送到exec_command。

#1


31  

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

这个家伙弄清楚了: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:

您只需使用一个exec_command发送多个命令,例如:

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

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

然后stdout.readlines()将返回您更改的目录。

#2


4  

As of version 2.1+ the method to change directories is sftp.chdir('path/to/directory')

从版本2.1+开始,更改目录的方法是sftp.chdir('path / to / directory')

#3


3  

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.

好吧,paramiko创建了一个shell实例,你希望在paramiko中执行的所有命令都只能在shell的实例中给出。

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:

现在,如果我想进入文件夹1并在那里创建一个目录,我会做的是:

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.

因为那些是shell的两个不同实例,并且它们的功能是独立的。

#4


2  

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

这个有点晚了,但它可以'invoke_shell'并通过文件写入标准输入。

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

请参阅:https://*.com/a/6203877/1861353

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

看起来有点重量级,因为你可以';'。join(cmdlist)并发送到exec_command。