- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试运行 scp
(安全复制)命令使用 subprocess.Popen
。登录需要我发送密码:
from subprocess import Popen, PIPE
proc = Popen(['scp', "user@10.0.1.12:/foo/bar/somefile.txt", "."], stdin = PIPE)
proc.stdin.write(b'mypassword')
proc.stdin.flush()
这会立即返回一个错误:
user@10.0.1.12's password:
Permission denied, please try again.
我确定密码是正确的。我通过在 shell 上手动调用 scp
轻松验证它。那为什么这行不通呢?
请注意,有许多与此类似的问题,询问关于 subprocess.Popen
并发送自动 SSH 或 FTP 登录的密码:
How can I set a users password in linux from a python script?
Use subprocess to send a password
这些问题的答案不起作用和/或不适用,因为我使用的是 Python 3。
最佳答案
这是一个使用 pexpect
和密码进行 ssh
的函数:
import pexpect
import tempfile
def ssh(host, cmd, user, password, timeout=30, bg_run=False):
"""SSH'es to a host using the supplied credentials and executes a command.
Throws an exception if the command doesn't return 0.
bgrun: run command in the background"""
fname = tempfile.mktemp()
fout = open(fname, 'w')
options = '-q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oPubkeyAuthentication=no'
if bg_run:
options += ' -f'
ssh_cmd = 'ssh %s@%s %s "%s"' % (user, host, options, cmd)
child = pexpect.spawn(ssh_cmd, timeout=timeout) #spawnu for Python 3
child.expect(['[pP]assword: '])
child.sendline(password)
child.logfile = fout
child.expect(pexpect.EOF)
child.close()
fout.close()
fin = open(fname, 'r')
stdout = fin.read()
fin.close()
if 0 != child.exitstatus:
raise Exception(stdout)
return stdout
使用 scp
应该可以实现类似的东西。
关于python - 使用 subprocess.Popen 通过 SSH 或 SCP 发送密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15166973/
我想知道使用 SCP 传输文件时使用的默认模式是什么(ASCII 或二进制,如 FTP)。 SCP 中是否有任何文件模式概念?它是如何工作的 ? 最佳答案 FTP中的文件传输方式用于解决不同操作系统行
我想知道是否有一种方法可以让我通过remote1主机直接从本地计算机对remote2主机的文件进行SCP。 网络仅允许从远程 1 主机连接到远程 2 主机。另外,remote1 主机和remote2
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 6 年前。 此问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-topic在这里
我正在寻找一个多平台解决方案,可以让我检查 scp命令可用。 问题是 scp 没有 --version命令行,当不带参数调用时,它将返回退出代码 1 (错误)。 更新:如果不清楚,多平台我的意思是一个
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
我在 Windows A 上运行 freeSSHD,需要通过 scp 传输文件。运行 freesshd 的 Windows 可以通过 ssh 客户端连接。但是 Windows 上的 WinSCP 和
我正在我的 Windows 机器上使用腻子通过 ssh 访问 ubuntu 服务器,并尝试将单个文件下载到我的本地 Windows 机器 我的 Windows 用户名是 Mark,每个 cmd 的主机
所以我发现我可以通过标准输入对 scp 执行以下操作: 目录创建 scp -tr . stdin -> D0755 0 stdin -> \x00 文件写入 scp -tr . stdin -> C
请为我在工作中面临的问题提供您的专家选项。 Ant SCP 任务不起作用 - 因为我可以使用 cygwin 进行 scp。 本地环境:windows 7 64 位、Jdk 1.6.0.24、Ant-1
我需要重新安装我们的一台服务器,作为预防措施,我想移动 /home、/etc、/opt和 /Services 到备份服务器。 但是,我有一个问题:由于存在大量符号链接(symbolic link),因
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我有一个小型应用程序,它试图执行十几个并行“scp”运行,从远程系统中提取文件。通常,它运行良好。有时,一两个 scp 悄悄地死掉了。(如果从 Linux 中拉出,则为“安静”。如果从 HP-UX 中
我有一个脚本可以将文件从一台服务器传输到另一台服务器,但它给出了一个错误: Net::SCP::Error (SCP did not finish successfully ()): 有谁能够帮助我?
下面是我的 shell 脚本片段,我在其中执行 scp 命令将文件从 machineB 复制到 machineA。 for element in ${x[$key]}; do # no quote
我有一个适用于不同客户端的脚本,需要将 SCP 文件发送到不同的主机。根据客户端和服务器的组合,我可能需要使用密码身份验证或公钥身份验证。我真的无法提前知道使用哪一个。 我使用的 SCP 有 2 个
我正在尝试使用 libssh2 scp 从实例中检索文件。 为了确保我的用户名、密码和 key 正确无误,我做了: sudo scp -v -P #port -i /home/username/.ss
我正在尝试使用 scp 在两台 Mac (OS 10.6.8) 之间传输文件。但它失败了,因为我的目录/文件名中有空格。我无法更改目录/文件名。 在我的 Mac 的终端中工作时,我经常使用\符号来表示
我需要一些帮助来在 xml 中构建这一行。对于谷歌内容 API 我想要得到的结果是 25 目前我的代码输出这一行 25这是不正确的。 这是我的代码 b = Nokogiri::XML::Builder
我正在使用命令: docker-machine scp 管理器:/home/docker/foo.txt 。 但是,Windows PowerShell 返回“您必须在本地拥有 scp 二进制文件的副
我们知道 String将被存储在SCP(字符串常量池)区域: 1.以同样的方式String[]也存储在SCP中?我的意思是每个数组包含一个 String ,那么这又会被存储在 SCP 中吗? 2.那么
我是一名优秀的程序员,十分优秀!