通过 Python subprocess.Popen 将带有空格的主机密钥传递给 WinSCP
作为更大任务的一部分,我想移动一些文件,处理它们并将新文件返回到 sftp 服务器。由于对我的限制,我只能使用 Python 3.4,这使我无法使用任何精美的模块,例如 Paramiko 和 pysftp。
与服务器的连接失败,根据标准输出管道,主机密钥不正确。返回的“不正确”密钥与传递给函数的密钥不同。它有一个额外的前导反斜杠并被缩短。最初传递的密钥是“ssh-XXXXXXX 256 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”
。 “不正确”的密钥是“\ssh-XXXXXXX”
。 假设是,subprocess.Popen
以某种方式破坏了字符串。
- 主机密钥、密码和私钥都是必需的
- 主机密钥是由服务器直接生成的,并且与 stdout 管道返回的指纹相匹配
- 在 Windows shell 中完成连接时可以正常工作,但需要在 python 中实现。
- 当通过使用
*
接受任何给定密钥时,连接会起作用,但这显然是不安全的,因此不可行。
代码如下:
array_to_be_joined = ['path/WinSCP.com',
'/ini=nul',
'/command',
'open sftp://sessionURL/ -hostkey="{}" -passphrase={} -privatekey=path/privatekey.ppk'.format(hostkey,passphrase),
'echo helloWorld']
process = subprocess.Popen(
array_to_be_joined,
stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
我尝试过的事情:
将参数写入
subprocess.Popen
函数将参数写入列表并将列表传递给函数
通过
string.format将主机密钥传递到变量中
以明文形式传递主机密钥
尝试了主机密钥:
- 单引号
- 单引号用
\
转义 - 单引号用
`转义
- 两个单引号
- 两个单引号用
\
转义 - 两个单引号用
`转义
- 双引号
- 用
\
转义双引号 - 用
`转义双引号
- 两个双引号
- 两个双引号用
\
转义 - 两个双引号用
`转义
- 不带引号
我已附上输出的图片,请原谅德国人。它翻译为:
Looking for remote computer...
Connecting to remote computer...
Authenticating...
Hostkey does not match konfigured key "\ssh-XXXXXXX"!
Server Key-Fingerprint is ssh-XXXXXXX 256 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Sign-up failed.
我将非常感谢任何帮助。另一种方法是编写和调用 shell 脚本,但这在较大的代码中实现起来会很麻烦且尴尬。 谢谢您的宝贵时间。
As part of a larger task I want to move some files, work on them and return the new files to the sftp server. Due to restrictions laid upon me I am only able to use Python 3.4, which excludes me from any nice and fancy modules like Paramiko and pysftp.
Connection to the server fails, according to the stdout pipe the hostkey is incorrect. The returned "incorrect" key is not identical to the one that was passed to the function. It has an additional, leading backslash and is cut short. The originally passed key is "ssh-XXXXXXX 256 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
. The "incorrect" key is "\ssh-XXXXXXX"
.
The presumption is, that subprocess.Popen
mangles the string in a way.
- hostkey, passphrase, and privatekey are all necessary
- the hostkey has been generated directly by the server and matches the fingerprint returned by the stdout pipe
- the connection works when done in a windows shell, but implementation in python is desired.
- the connection works when accepting any given Key through usage of
*
, but that would obviously be insecure, therefore not viable.
Code is as follows:
array_to_be_joined = ['path/WinSCP.com',
'/ini=nul',
'/command',
'open sftp://sessionURL/ -hostkey="{}" -passphrase={} -privatekey=path/privatekey.ppk'.format(hostkey,passphrase),
'echo helloWorld']
process = subprocess.Popen(
array_to_be_joined,
stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Things I have tried:
wrote the parameters in the
subprocess.Popen
functionwrote the parameters into a list and passed the list to the function
passed the hostkey in a variable through
string.format
passed the hostkey in plaintext
tried the hostkey in:
- single quotes
- single quotes escaped with
\
- single quotes escaped with
`
- two single quotes
- two single quotes escaped with
\
- two single quotes escaped with
`
- double quotes
- double quotes escaped with
\
- double quotes escaped with
`
- two double quotes
- two double quotes escaped with
\
- two double quotes escaped with
`
- without quotes
I have attached a picture of the output, please excuse the German. It translates to:
Looking for remote computer...
Connecting to remote computer...
Authenticating...
Hostkey does not match konfigured key "\ssh-XXXXXXX"!
Server Key-Fingerprint is ssh-XXXXXXX 256 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Sign-up failed.
I would greatly appreciate any help. The alternative would be to write and call shell scripts, but that would be cumbersome and awkward to implement in the larger code.
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这是 Windows Python 问题。
只需自己格式化命令行,而不是让 Popen 为您执行(错误地):
另请参阅 Python 双引号 in subprocess.Popen isn't执行 WinSCP 脚本时工作
另一种选择是以无空格格式将指纹作为会话 URL 的一部分传递:
https://winscp.net/eng/docs/session_url#hostkey
I believe it's Windows Python issue.
Just format the commandline yourself, instead of having Popen doing (incorrectly) it for you:
See also Python double quotes in subprocess.Popen aren't working when executing WinSCP scripting
Another option is passing the fingerprint as part of the session URL in whitespace-less format:
https://winscp.net/eng/docs/session_url#hostkey