简体   繁体   English

CISCO IOS-XR,Python3.7,无法在 Cisco 路由器上运行“ls”和“df”等命令

[英]CISCO IOS-XR, Python3.7, Not able to run commands like 'ls' and 'df' on Cisco router

Cisco ios-xr router using CLI:使用 CLI 的 Cisco ios-xr 路由器:

RP/0/RP0#show version RP/0/RP0#显示版本

Thu Nov 25 07:53:59.103 UTC 11 月 25 日星期四 07:53:59.103 UTC

Cisco IOS XR Software, Version 6.5.32.11I思科 IOS XR 软件,版本 6.5.32.11I

Copyright (c) 2013-2020 by Cisco Systems, Inc.版权所有 (c) 2013-2020 思科系统公司。

RP/0/RP0#run RP/0/RP0#运行

Thu Nov 25 07:54:05.231 UTC 11 月 25 日星期四 07:54:05.231 UTC

[xr-vm_node0_RP0_CPU0:~]$df [xr-vm_node0_RP0_CPU0:~]$df

Filesystem 1K-blocks Used Available Use% Mounted on文件系统 1K-blocks Used 可用 Use% Mounted on

rootfs 3966080 1332040 2412860 36% /根文件系统 3966080 1332040 2412860 36% /

76892 11848320 43% /mnt/ecu/vdd 76892 11848320 43% /mnt/ecu/vdd

[xr-vm_node0_RP0_CPU0:~]$ [xr-vm_node0_RP0_CPU0:~]$

Using python: I am able to run show commands using Connecthandler.send.command:使用 python:我可以使用 Connecthandler.send.command 运行 show 命令:

from netmiko import ConnectHandler
import subprocess
Network_Device = {"host": "10.111.22.333", "username": "USER123", "password": "Pass123",     "device_type": "cisco_xr",}

Connect = ConnectHandler(**Network_Device)
Connect.enable()
version1 = "show version"
print(Connect.send_command(version1))

But not able to run 'df' or 'ls' commands, as not able to reach bash prompt i reach by running 'run' command on router.但无法运行“df”或“ls”命令,因为无法通过在路由器上运行“运行”命令到达 bash 提示符。

I tried:我试过了:

disk1files = subprocess.run("df", stdout=subprocess.PIPE)
print(disk1files.stdout.decode())

But seems its wrong.但似乎是错误的。 Please suggest the right library or code I can use here.请建议我可以在这里使用的正确库或代码。

This is my first question here, so bear some silly questions or mistakes done in code这是我在这里的第一个问题,所以请承担一些愚蠢的问题或代码中的错误

if on DF you are referring to "Don't fragment" then it is posible to send it like如果在 DF 上您指的是“不要分段”,那么可以像这样发送它

Connect.send_command("ping 192.168.10.10 df-bit size 1600")

where 1600 represents MTU, and for ls commands is link command,其中 1600 代表 MTU,对于 ls 命令是链接命令,

Connect.send_command("ls-active") 
Connect.send_command("ls-active-enabled") 

but if you are referring to df and ls in linux (disk free and list files..) then you can use os module for sending commands:但是如果您在 linux 中引用 df 和 ls (磁盘空闲和列表文件..),那么您可以使用 os 模块发送命令:

import os
os.system("ls -l")

or use call from subprocess module:或使用来自子进程模块的调用:

from subprocess import call
call(["ls", "-l"])

If you need to acccess cisco bash:如果需要接入cisco bash:

switch# configure terminal
switch(config)# feature bash-shell
switch# run?
  run         Execute/run program
  run-script  Run shell scripts

switch# run bash?
  bash  Linux-bash

switch# run bash 
bash-4.2$ whoami
admin
bash-4.2$ pwd
/bootflash/home/admin
bash-4.2$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM