Netmiko read_channel() 和 Clear_buffer() 不打印输出
Netmiko read_channel() and Clear_buffer() not printing output
所以,我试图将一些配置自动推送到我的交换机,但这些命令通常 return 交换机中的 (y/n) 提示。因此,send_command 函数在查找提示时将不起作用,因此我使用的是完美运行的 write_channel() 函数。但是,出于某种原因,在我发送命令后,read_channel 和 clear_buffer() 函数打印的输出都不超过 1 行或 2 行。
我尝试过的事情:
将 read_channel/buffer 置于慢循环中。
read_until_prompt/pattern
send_command 与 cmd_verify=False 和 auto_find_prompt=False
打印 read_channel
和更多。 . .
send_command 可以毫无问题地显示输出,它的源代码使用 read_channel() 来推送输出,所以我不确定还能尝试什么。
目标:我需要从 cisco 交换机中的命令获取输出(在 001:00 中重新加载),这样我就可以告诉程序如何对提示做出反应。
这是我为解决主项目中的问题而编写的一些基本代码:
from netmiko import ConnectHandler
import time
Network_Device = {
"ip": "10.251.11.38",
"username": "user",
"password": "pass",
"secret": "secretpass",
"device_type": "cisco_ios",
"fast_cli": False
}
#Connect = ConnectHandler(**Network_Device)
with ConnectHandler(**Network_Device) as ssh:
ssh.enable()
while True:
x = input(ssh.find_prompt())
if x == '':
pass
elif x == 'exit' or x == 'Exit':
ssh.disconnect()
else:
ssh.write_channel(x) # Writes command to cli
#Problem lies on the two functions below: Neither will print out a show command fully.
ssh.clear_buffer(backoff=True)
ssh.read_channel()
Yes, I want to reload dozen's of switches at approximately the same time for a dhcp update.
很明显,现在您想重新加载一堆开关。 reload
使用 Proceed with reload? [confirm]
提示您。所以你可以使用 expect_sting
参数来确认或拒绝重新加载。为此,最好在重新加载之前先保存配置,以免配置丢失。
A quick brief of how send_command
works. send_command
is pattern-based where it checks for the device prompt to know that the command output is done. On the other hand send_command_timing
is delay-based meaning it waits for some time and doesn't check for anything to know if the command is done running or not.
from netmiko import ConnectHandler
device = {
"device_type": "",
"ip": "",
"username": "",
"password": "",
"secret": "",
"conn_timeout": 12,
"fast_cli": False,
}
conn = ConnectHandler(**device)
# Check if not logged into enable mode directly
# and enter enable mode if logged into user mode
if not conn.check_enable_mode():
conn.enable()
conn.save_config() # save configuration first
conn.send_command(command_string="reload", expect_string=r"confirm")
注意这里的 expect_string
通知 send_command
我完成了。
现在您有两个选择之一:
- 发送
y
确认重新加载(不鼓励)
- 调用
conn.disconnect()
确认重新加载并退出(首选)
Option 1 is discouraged because you will not be able to disconnect after confirming the reload and eventually an OSError
or EOFError
exception is raised that you will have to handle.
conn.disconnect()
上面一行先发送一个\n
,然后断开与设备的连接。这使您可以立即处理其他设备的重新加载,而不必处理任何引发的 OSError
或 EOFError
异常。
完整示例
from netmiko import ConnectHandler
device = {
"device_type": "",
"ip": "",
"username": "",
"password": "",
"secret": "",
"conn_timeout": 12,
"fast_cli": False,
}
conn = ConnectHandler(**device)
if not conn.check_enable_mode():
conn.enable()
conn.save_config()
# Notice here the `expect_string` which informs
# the `send_command` I am done.
# I am telling `send_command` function don't look at
# the device prompt but look for `"confirm"` to proceed
# with the next command.
conn.send_command(command_string="reload", expect_string=r"confirm")
conn.disconnect()
print(f'Reloading {device["ip"]}... Please wait for some time for {device["ip"]} to boot up again')
I didn't use the context manager method on purpose to make use of conn.disconnect()
function.
更新
根据 Kirk Byers (netmiko 的作者) 在 中的建议,建议在 [=19= 中添加 r"confirm"
] 并省略方括号。
所以,我试图将一些配置自动推送到我的交换机,但这些命令通常 return 交换机中的 (y/n) 提示。因此,send_command 函数在查找提示时将不起作用,因此我使用的是完美运行的 write_channel() 函数。但是,出于某种原因,在我发送命令后,read_channel 和 clear_buffer() 函数打印的输出都不超过 1 行或 2 行。
我尝试过的事情:
将 read_channel/buffer 置于慢循环中。
read_until_prompt/pattern
send_command 与 cmd_verify=False 和 auto_find_prompt=False
打印 read_channel
和更多。 . .
send_command 可以毫无问题地显示输出,它的源代码使用 read_channel() 来推送输出,所以我不确定还能尝试什么。
目标:我需要从 cisco 交换机中的命令获取输出(在 001:00 中重新加载),这样我就可以告诉程序如何对提示做出反应。
这是我为解决主项目中的问题而编写的一些基本代码:
from netmiko import ConnectHandler
import time
Network_Device = {
"ip": "10.251.11.38",
"username": "user",
"password": "pass",
"secret": "secretpass",
"device_type": "cisco_ios",
"fast_cli": False
}
#Connect = ConnectHandler(**Network_Device)
with ConnectHandler(**Network_Device) as ssh:
ssh.enable()
while True:
x = input(ssh.find_prompt())
if x == '':
pass
elif x == 'exit' or x == 'Exit':
ssh.disconnect()
else:
ssh.write_channel(x) # Writes command to cli
#Problem lies on the two functions below: Neither will print out a show command fully.
ssh.clear_buffer(backoff=True)
ssh.read_channel()
Yes, I want to reload dozen's of switches at approximately the same time for a dhcp update.
很明显,现在您想重新加载一堆开关。 reload
使用 Proceed with reload? [confirm]
提示您。所以你可以使用 expect_sting
参数来确认或拒绝重新加载。为此,最好在重新加载之前先保存配置,以免配置丢失。
A quick brief of how
send_command
works.send_command
is pattern-based where it checks for the device prompt to know that the command output is done. On the other handsend_command_timing
is delay-based meaning it waits for some time and doesn't check for anything to know if the command is done running or not.
from netmiko import ConnectHandler
device = {
"device_type": "",
"ip": "",
"username": "",
"password": "",
"secret": "",
"conn_timeout": 12,
"fast_cli": False,
}
conn = ConnectHandler(**device)
# Check if not logged into enable mode directly
# and enter enable mode if logged into user mode
if not conn.check_enable_mode():
conn.enable()
conn.save_config() # save configuration first
conn.send_command(command_string="reload", expect_string=r"confirm")
注意这里的 expect_string
通知 send_command
我完成了。
现在您有两个选择之一:
- 发送
y
确认重新加载(不鼓励) - 调用
conn.disconnect()
确认重新加载并退出(首选)
Option 1 is discouraged because you will not be able to disconnect after confirming the reload and eventually an
OSError
orEOFError
exception is raised that you will have to handle.
conn.disconnect()
上面一行先发送一个\n
,然后断开与设备的连接。这使您可以立即处理其他设备的重新加载,而不必处理任何引发的 OSError
或 EOFError
异常。
完整示例
from netmiko import ConnectHandler
device = {
"device_type": "",
"ip": "",
"username": "",
"password": "",
"secret": "",
"conn_timeout": 12,
"fast_cli": False,
}
conn = ConnectHandler(**device)
if not conn.check_enable_mode():
conn.enable()
conn.save_config()
# Notice here the `expect_string` which informs
# the `send_command` I am done.
# I am telling `send_command` function don't look at
# the device prompt but look for `"confirm"` to proceed
# with the next command.
conn.send_command(command_string="reload", expect_string=r"confirm")
conn.disconnect()
print(f'Reloading {device["ip"]}... Please wait for some time for {device["ip"]} to boot up again')
I didn't use the context manager method on purpose to make use of
conn.disconnect()
function.
更新
根据 Kirk Byers (netmiko 的作者) 在 r"confirm"
] 并省略方括号。