【问题标题】:how to run custom commands using sftp or ssh or paramiko如何使用 sftp 或 ssh 或 paramiko 运行自定义命令
【发布时间】:2015-04-30 12:13:58
【问题描述】:

t 我有以下代码,它使用 ssh 和 sftp ssh 到网络上的构建服务器并运行本机 mkdir 和 chdir 命令,如何使用我在构建服务器上本地安装的一些可执行文件运行任何自定义命令?

import paramiko

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect('buildservername', username='yadomi', password='password')

sftp = ssh.open_sftp()
sftp.chdir('/local/mnt/workspace/newdir')
commandstring = 'repo init -u git://git.company.com/platform/manifest -b branchname --repo-url=git://git.company.com/tools/repo.git --repo-branch=caf/caf-stable'
si,so,se = ssh.exec_command(commandstring) 
readList = so.readlines()
print readList
print se

错误:-

<paramiko.ChannelFile from <paramiko.Channel 1 (closed) -> <paramiko.Transport at 0x2730310L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>

【问题讨论】:

标签: python ssh sftp paramiko


【解决方案1】:

1) 您打开一个 SFTP 通道并设置其工作目录,但您从未对该通道执行任何其他操作。也许您认为这会为后续的exec_command 设置工作目录?没有。

2) 您执行print se,但无法识别输出。也许您认为从调用的命令打印错误流?没有。

试试这个:

import paramiko

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect('buildservername', username='yadomi', password='password')

commandstring = 'cd /local/mnt/workspace/newdir ; repo init -u git://git.company.com/platform/manifest -b branchname --repo-url=git://git.company.com/tools/repo.git --repo-branch=caf/caf-stable'
si,so,se = ssh.exec_command(commandstring) 
readList = so.readlines()
errList = se.readlines()
print readList
print errList

您的字面问题“如何使用我在构建服务器上本地安装的一些可执行文件运行任何自定义命令?”的答案是“通过将适当的命令行传递给 @987654324 @"。

【讨论】:

  • 运行ssh.exec_command(commandstring)后python脚本挂了,我可以看到命令成功,因为我可以看到目录中的.repo文件,如何调试它为什么卡住?
  • “谢谢,这有效”和“python 脚本挂起”之间有什么变化?如果您有一个工作脚本和一个非工作脚本,调试它们的一种方法是对其中一个进行小的增量更改,直到它开始工作或两个脚本相同为止。
  • 或者,准确地确定 Python 脚本挂起的位置。将print 语句大量添加到您的脚本中。找到永远不会结束的精确线。
  • 什么都没有改变,我太早了,我看到.repo被创建并认为它有效,我添加了打印语句,发现脚本卡在readList = so.readlines(),知道为什么那个?可能没有so and se
  • 这意味着远程shell由于某种原因还没有退出。我不知道为什么会这样。尝试将您的命令更改为“cd /whatever/path/you/need ; ls”并查看是否返回。如果是这样,那么您的 repo 命令正在做一些奇怪的事情。如果没有,那么其他东西正在保持连接打开。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2016-07-08
  • 2014-04-30
  • 2012-07-08
  • 2020-10-02
相关资源
最近更新 更多