如何使用 Paramiko 更改目录? [英] How do I change directories using Paramiko?

查看:98
本文介绍了如何使用 Paramiko 更改目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未使用 Paramiko 执行的 Drush 命令

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

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

<预><代码>>>>stdin, stdout, stderr = myssh.exec_command("pwd")>>>标准输出.readlines()['/big/dom/home/myid\n']>>>stdin, stdout, stderr = myssh.exec_command("cd ../")>>>标准输出.readlines()[]>>>stdin, stdout, stderr = myssh.exec_command("pwd")>>>标准输出.readlines()['/big/dom/home/myid\n']>>>

我是否误解了这里发生的事情?我应该不能更改目录吗?或者,如果可以,我应该以其他方式而不是使用 exec_command 吗?

解决方案

这家伙想通了:http://www.vertigrated.com/blog/2010/02/python-remote-ssh-with-paramiko/

你只需要用一个 exec_command 发送多个命令,例如:

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

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

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?

解决方案

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.

这篇关于如何使用 Paramiko 更改目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆