使用 Junos PyEZ 访问 Junos 设备上的 Shell
使用 Junos PyEZ 连接到 Junos 设备上的 shell 并执行命令。
StartShell 概述
Junos CLI 有许多操作模式命令,这些命令返回的信息与许多 shell 命令返回的信息类似。因此,通常不需要访问 Junos 设备上的 UNIX 级别 shell。但是,在某些情况下,用户或应用程序可能需要访问 shell 并执行 shell 命令或从 shell 执行 CLI 命令。
Junos PyEZ jnpr.junos.utils.start_shell 模块定义了类 StartShell ,它使 Junos PyEZ 应用程序能够启动与 Junos 设备的 SSH 连接并访问 shell。这些 StartShell 方法使应用程序能够通过连接执行命令并检索响应。
StartShell open()和 close() 方法建立并终止与设备的 SSH 连接。因此,如果客户端应用程序只需要访问 shell,它可以省略对 and Device open() close() 方法的调用。
在 Junos PyEZ 2.6.7 及更低版本中, StartShell 实例连接到默认 SSH 端口 22。从 Junos PyEZ 2.6.8 版开始, StartShell 实例连接到实例中 Device 定义的同一端口,但以下情况除外,其中 shell 连接仍使用端口 22:
-
Devicehost设置为“localhost” -
Deviceport设置为 830。 -
Deviceport是未定义的。
从 Shell 执行命令
该StartShell run() 方法执行 shell 命令并等待响应。默认情况下,该方法在返回命令输出之前等待默认 shell 提示符之一(%、#、> 或 $)。或者,您可以将this="string"参数设置为特定字符串,该方法在返回命令输出之前等待预期的字符串或模式。
返回值是一个元组。第一项是 True 退出代码是否为 0, False 否则。第二项是命令的输出。
以下示例连接到主机,并从 shell 执行两个操作模式命令。该脚本首先执行 request support information 命令并将输出保存到文件中。然后,脚本执行 show version 命令,将输出存储在变量中 version ,然后打印变量的内容。
from jnpr.junos import Device
from jnpr.junos.utils.start_shell import StartShell
dev = Device(host='router1.example.net')
ss = StartShell(dev)
ss.open()
ss.run('cli -c "request support information | save /var/tmp/information.txt"')
version = ss.run('cli -c "show version"')
print (version)
ss.close()
返回的元组包括与命令的退出代码和命令输出 show version 相对应的布尔值。为简洁起见,此示例中的输出被截短。
(False, '\r \rHostname: router1\r\nModel: mx104\r\nJunos: 17.1R8\r\nJUNOS Base OS boot [17.1R1.8]\r\n ...)
该 StartShell 类的实例也可以用作上下文管理器。在这种情况下,无需显式调用 StartShell open() and close() 方法。例如:
from jnpr.junos import Device
from jnpr.junos.utils.start_shell import StartShell
dev = Device(host='router1.example.net')
with StartShell(dev) as ss:
ss.run('cli -c "request support information | save /var/tmp/information.txt"')
version = ss.run('cli -c "show version"')
print (version)
如何指定壳体类型
从 Junos PyEZ 2.6.4 版开始, StartShell 支持 shell_type 在远程脚本中使用参数来指定 shell 类型。 StartShell 支持以下 shell 类型:
-
C 壳 (csh)
-
伯恩式贝壳(白蜡木)
默认情况下, StartShell 实例为类型 C Shell (csh)。还可以指定 shell_type="sh" 启动伯恩式壳体(灰烬)。例如:
from jnpr.junos import Device
from jnpr.junos.utils.start_shell import StartShell
dev = Device(host='router1.example.net')
with StartShell(dev, shell_type="sh") as ss:
version = ss.run('cli -c "show version"')
print (version)
如何指定超时
您可以包含参数来StartShelltimeout指定实用程序在超时之前必须等待预期字符串或模式的持续时间(以秒为单位)。如果未指定超时,则默认值为 30 秒。
预期的字符串是参数中定义的 this 值。如果未定义 this,则预期的字符串是默认 shell 提示符之一。如果改为设置特殊值 this=None,设备将等待超时一段时间后再捕获命令输出,如 执行非返回 Shell 命令中所述。
from jnpr.junos import Device
from jnpr.junos.utils.start_shell import StartShell
dev = Device(host='router1.example.net')
with StartShell(dev) as ss:
ss.run('cli -c "request support information | save /var/tmp/information.txt"', timeout=60)
version = ss.run('cli -c "show version"')
print (version)
如何错开命令执行
有时,您可能希望执行或循环对该 run() 方法的多次调用。为了帮助稳定输出,可以指定 sleep 参数。该 sleep 参数指示设备在从缓冲区接收数据之前等待指定的秒数。您可以定义 sleep 为亚秒级精度的浮点数。
from jnpr.junos import Device
from jnpr.junos.utils.start_shell import StartShell
from pprint import pprint
dev = Device(host='router1.example.net')
tables = ['inet.0', 'inet6.0']
with StartShell(dev) as ss:
for table in tables:
command = 'cli -c "show route table ' + table + '"'
rsp = ss.run(command, sleep=5)
pprint (rsp)
执行不返回的 shell 命令
在某些情况下,您可能需要执行不返回 shell 命令,如 monitor traffic 显示在本地路由引擎上发起或终止的流量的命令。在 Junos CLI 中,命令 monitor traffic 会实时显示信息,直到用户发送 Ctrl+c 键盘序列以停止数据包捕获。
您可以通过包含参数 this=None来使用该方法执行StartShellrun()非返回 shell 命令。包含参数时this=None,该方法会等待到指定的超时值,以检索并返回 shell 中的所有命令输出。在这种情况下,返回元组的第一项是True执行的 shell 命令的结果返回内容时,第二项是命令输出。如果省略参数this或将其设置为等于特定字符串或模式,则如果方法在命令输出中遇到默认提示或指定的字符串模式,则该方法可能会返回非返回命令的部分输出。
以下示例代码执行 monitor traffic interface fxp0 命令,等待 15 秒,然后检索并返回命令输出。
from jnpr.junos import Device
from jnpr.junos.utils.start_shell import StartShell
from pprint import pprint
dev = Device(host='router1.example.net')
with StartShell(dev) as ss:
pprint(ss.run('cli -c "monitor traffic interface fxp0"', this=None, timeout=15))
变更历史表
是否支持某项功能取决于您使用的平台和版本。使用 功能资源管理器 确定您的平台是否支持某个功能。
StartShell 实例连接到实例中
Device 定义的同一端口,除非主机设置为 localhost 或端口为 830 或未定义。