如何在Python Fabric中运行EdgeOS配置命令

问题描述 投票:0回答:1

我正在尝试使用 python 脚本以编程方式更新 EdgeOS 中的配置。我使用 Fabric 作为 ssh 客户端。 https://docs.fabfile.org/en/latest/index.html

下面的“---原始问题---”描述了问题,但我认为我已经找到了问题的根源,但没有找到解决方案。 Fabric ssh 以非交互模式进入路由器,因此无法获得完整的 bash 配置。

configure
命令不可用,
show
或其他路由器管理命令也不可用。路由器中的.bashrc文件是这样的:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# enable completion
source /etc/bash_completion.d/vyatta-cfg
source /etc/bash_completion.d/vyatta-op

unset HISTFILE

所以我可以看到我没有获取 bash_completion 文件。我尝试在 python 中手动执行此操作:

c.run("source /etc/bash_completion.d/vyatta-cfg")
,但似乎不起作用。我的下一个最佳解决方案是找到这些命令的硬路径,但我不确定在哪里寻找。

---原来的问题---

我想做的是创建小的配置更改,例如:

configure
set interfaces ethernet eth2 vif 20 disable
commit
save

但是,当我通过结构运行时,

configure
命令失败。我注意到,当我处于 ssh 会话中时,
configure
命令会创建一个新的子会话或其他内容。我不确定这个词。

$ configure
[edit]
#

到目前为止的脚本:

from fabric import Connection
c = Connection('routerip')
c.run("configure")

错误:

vbash: configure: command not found
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/me/opt/anaconda3/lib/python3.8/site-packages/decorator.py", line 231, in fun
    return caller(func, *(extras + args), **kw)
  File "/Users/me/opt/anaconda3/lib/python3.8/site-packages/fabric/connection.py", line 23, in opens
    return method(self, *args, **kwargs)
  File "/Users/me/opt/anaconda3/lib/python3.8/site-packages/fabric/connection.py", line 763, in run
    return self._run(self._remote_runner(), command, **kwargs)
  File "/Users/me/opt/anaconda3/lib/python3.8/site-packages/invoke/context.py", line 113, in _run
    return runner.run(command, **kwargs)
  File "/Users/me/opt/anaconda3/lib/python3.8/site-packages/fabric/runners.py", line 83, in run
    return super().run(command, **kwargs)
  File "/Users/me/opt/anaconda3/lib/python3.8/site-packages/invoke/runners.py", line 395, in run
    return self._run_body(command, **kwargs)
  File "/Users/me/opt/anaconda3/lib/python3.8/site-packages/invoke/runners.py", line 451, in _run_body
    return self.make_promise() if self._asynchronous else self._finish()
  File "/Users/me/opt/anaconda3/lib/python3.8/site-packages/invoke/runners.py", line 518, in _finish
    raise UnexpectedExit(result)
invoke.exceptions.UnexpectedExit: Encountered a bad command exit code!

Command: 'configure'

Exit code: 127

Stdout: already printed

Stderr: already printed
linux bash ssh python
1个回答
0
投票

使用绝对路径进行配置(如果是命令)

/path/to/configure
set interfaces ethernet eth2 vif 20 disable
commit
save

您可以从互动会话中获取

type configure

如果它进入交互式会话,您应该需要以某种方式提供输入。

© www.soinside.com 2019 - 2024. All rights reserved.