使用python3从windows获得SSH和SCP

2024-04-19 15:26:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我被困在这里好几天了。我想把一个文件从我的windows复制到一个远程linux服务器并在那里运行脚本。我有ssh和scp的工具。我可以从中通过命令行调用linux服务器,但当我通过python调用它时,它就挂起了。在

pro=subprocess.Popen('ssh user@server')
pro.communicate()

有一个空白屏幕。我输入的任何内容都会出现在我的屏幕上。 我希望有一个密码提示,但没有。我曾想过使用像paramiko、pexpect、pyssh这样的库,但是python3不支持它们

我们非常感谢您的帮助。在


Tags: 文件工具命令行服务器脚本远程屏幕linux
2条回答

http://docs.fabfile.org/en/1.0.1/index.html

我不确定它能不能用2to3转换

但使用起来相当简单:

from fabric.api import run, env
from fabric.context_managers import hide
from fabric.colors import green

with hide('status', 'running', 'output'):
    print('Apache ' + env.host + ': ' + green(run('wget -q -O /dev/null http://localhost/ && echo OK')))

在环境主机twisted couch来自命令行,是另一种选择,但它还没有移植到py3k

还有一个类似的问题。使用netcat手动nc。使用操作系统()以在客户端和服务器端生成它。

从netcat手册页:

DESCRIPTION

The nc (or netcat) utility is used for just about anything under the sun involving TCP or UDP. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6. Unlike telnet(1), nc scripts nicely, and separates error messages onto standard error instead of sending them to standard output, as telnet(1) does with some.

Common uses include:

  • simple TCP proxies
  • shell-script based HTTP clients and servers
  • network daemon testing
  • a SOCKS or HTTP ProxyCommand for ssh(1)
  • and much, much more

如果知道相关问题(原始问题没有说明“远程”的含义),这对内联网和internet上的本地或远程计算机都非常有用。例如:

至于“但那不是python”的评论:当有非常好的基础实用程序被移植到所有的O/S并且除了底层的基本O/S之外没有其他依赖性时,不要重新设计轮子

相关问题 更多 >