ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.59k stars 1.31k forks source link

TextFSM Question #1148

Closed Abed1985 closed 5 years ago

Abed1985 commented 5 years ago

Hi , This is just a question . I am trying to replicate what I see on NTC templates on a huawei routers we use .

In [22]: my_device = { "host":"10.170.17.46","username":"abooda","password":"password","device_type": "huawei",}

In [23]: net_connect2 = Netmiko(**my_device)  

In [24]: output = net_connect2.send_command("display arp",use_textfsm=True) 

In [25]: print(output)
IP ADDRESS      MAC ADDRESS     EXPIRE(M) TYPE        INTERFACE   VPN-INSTANCE 
                                          VLAN/CEVLAN PVC                      
------------------------------------------------------------------------------
10.170.17.46    346a-c214-8503            I -         GE0/0/9
10.170.17.41    6400-f175-d080  3         D-0         GE0/0/9
172.16.7.2      346a-c214-8501            I -         Vlanif7
172.16.7.5      0c8d-dbb9-cbf4  20        D-0         GE0/0/0
                                             7/-
172.16.7.6      0c8d-dbb9-a740  20        D-0         GE0/0/0
                                             7/-
172.16.7.4      0018-0a4f-0001  5         D-0         GE0/0/0
                                             7/-
172.16.7.3      4c00-826a-c77e  5         D-0         GE0/0/0
                                             7/-
------------------------------------------------------------------------------
Total:7         Dynamic:5       Static:0     Interface:2    

In [26]: 

I tried to create a template similar to the hp procurve switch (display arp) , but i am not getting structured data back. Any ideas if what if I am trying to replicate is right or this is not the way TFM works ?

my template :

$ cat huawei_display_arp.template 
Value IPADDRESS (\d+.\d+.\d+.\d+)
Value MAC ADDRESS (\w+-\w+-\w+)
Value EXPIRE(M) (\d+)
Value TYPE (\S+)
Value INTERFACE (\S+)
Value VPN-INSTANCE (\S+)

Start
^${IPADDRESS}\s+${MAC ADDRESS}\s+${EXPIRE(M)}\s+${TYPE}\s+${INTERFACE}\s+${VPN-INSTANCE} -> Record

Actual /similar hp comware tfm template


$ cat hp_comware_display_arp.template 
Value IPADDRESS (\d+.\d+.\d+.\d+)
Value MACADDRESS (\w+-\w+-\w+)
Value VLAN (\S+|\d+)
Value INTERFACE (\S+)
Value AGING (\d+)
Value TYPE (\S+)

Start
  ^${IPADDRESS}\s+${MACADDRESS}\s+${VLAN}\s+${INTERFACE}\s+${AGING}\s+${TYPE} -> Record
carlmontanari commented 5 years ago

Hi @Abed1985 Looks like your template probably failed to snag the data it needed. A quick look at this and I saw three maybe issues;

  1. the "(M)" on your "EXPIRE" value
  2. the "-" in MAC-ADDRESS (instead of an underscore)
  3. since it looks like "vpn-instance" is blank on your sample data you probably want a record line to snag that, take a look at this to see if it helps:
Value IPADDRESS (\d+.\d+.\d+.\d+)
Value MAC-ADDRESS (\w+-\w+-\w+)
Value EXPIRE (\d+)
Value TYPE (\S+)
Value INTERFACE (\S+)
Value VPN_INSTANCE (.*)

Start
  ^${IPADDRESS}\s+${MAC-ADDRESS}\s+${EXPIRE}\s+${TYPE}\s+${INTERFACE}\s+${VPN_INSTANCE} -> Record
  ^${IPADDRESS}\s+${MAC-ADDRESS}\s+${EXPIRE}\s+${TYPE}\s+${INTERFACE} -> Record
ktbyers commented 5 years ago

Also from a workflow perspective, I like to start with a single variable and a rule or rules that process that single variable. I then verify I have a working solution on that single variable case.

I then expand on that to add a second variable and updated rule(s) and make sure that works.

And keep working in an iterative manner to extract all of the fields.

Abed1985 commented 5 years ago

Thanks @ktbyers @carlniger carlniger

is it this what you mean ? I am trying to have only IP and mac as an interesting parameters for me at this point with no luck. I will see if I can work out ip address alone then and build on that ? is the string under the start is right

abooda@bne-mns-dev-01:~/TextFSM/ntc-templates/templates$ cat huawei_display_arp.template Value IPADDRESS (\d+.\d+.\d+.\d+) Value MAC-ADDRESS (\w+-\w+-\w+)

Start ^${IPADDRESS}\s+${MAC-ADDRESS}\s -> Record abooda@bne-mns-dev-01:~/TextFSM/ntc-templates/templates$

In [1]: from netmiko import Netmiko

In [2]: my_device = { "host":"10.170.17.46","username":"abooda","password":"###Yamm3029Sherry","device_type": "huawei",}

In [3]: net_connect2 = Netmiko(**my_device)

In [4]: output = net_connect2.send_command("display arp",use_textfsm=True)

In [5]: print(output) IP ADDRESS MAC ADDRESS EXPIRE(M) TYPE INTERFACE VPN-INSTANCE VLAN/CEVLAN PVC

10.170.17.46 346a-c214-8503 I - GE0/0/9 10.170.17.41 6400-f175-d080 2 D-0 GE0/0/9 172.16.7.2 346a-c214-8501 I - Vlanif7 172.16.7.5 0c8d-dbb9-cbf4 20 D-0 GE0/0/0 7/- 172.16.7.6 0c8d-dbb9-a740 20 D-0 GE0/0/0 7/- 172.16.7.4 0018-0a4f-0001 4 D-0 GE0/0/0 7/- 172.16.7.3 4c00-826a-c77e 4 D-0 GE0/0/0 7/-

Total:7 Dynamic:5 Static:0 Interface:2

In [6]: exit()

Abed1985 commented 5 years ago

This has not worked too.. Is there anything else I need to edit other than creating this template file. I feel that the template is not being used at all. is there any file I should add the use of this template to? please execuse my limited understanding as I am new to using this approach .

abooda@bne-mns-dev-01:~/TextFSM/ntc-templates/templates$ cat huawei_display_arp.template
Value IPADDRESS (\d+.\d+.\d+.\d+)

Value MAC-ADDRESS (\w+-\w+-\w+)

Start ^${IPADDRESS}\s -> Record abooda@bne-mns-dev-01:~/TextFSM/ntc-templates/templates$

carlmontanari commented 5 years ago

Have you updated your index file to point to this template? The last comment looks like it should work assuming index file is updated appropriately.

Also a very quick search didn't find anything to document this, but I believe that dashes/hyphens "-" in Values will fail in TextFSM (sorry I pasted a version with dashes from testing some stuff yesterday) -- obviously this shouldn't affect that last template file you posted just wanted to mention that.

Abed1985 commented 5 years ago

I find out that i have to update the enviroent variable export =.. on the linux machine every once in a while for the netmiko command to accept the tfm usage .. when i try with one of the existing templates for a cisco device for example it works. But also how do i update the index file to point perminantly to the templates location

carlmontanari commented 5 years ago

Sounds like you need to add the appropriate line to the index file:

Template, Hostname, Platform, Command

alcatel_aos_show_vlan.template, .*, alcatel_aos, show vlan

So for you something like:

huawei_show_ip_arp.template, .*, huawei, show ip arp

See the index file/repo for details: https://github.com/networktocode/ntc-templates/blob/master/templates/index

Abed1985 commented 5 years ago

Thanks , its now working fine with the variables I have :

In [6]: print(output) [{'ipaddress': '10.170.17.46', 'mac_address': '346a-c214-8503'}, {'ipaddress': '10.170.17.41', 'mac_address': '6400-f175-d080'}, {'ipaddress': '172.16.7.2', 'mac_address': '346a-c214-8501'}, {'ipaddress': '172.16.7.5', 'mac_address': '0c8d-dbb9-cbf4'}, {'ipaddress': '172.16.7.6', 'mac_address': '0c8d-dbb9-a740'}, {'ipaddress': '172.16.7.4', 'mac_address': '0018-0a4f-0001'}, {'ipaddress': '172.16.7.3', 'mac_address': '4c00-826a-c77e'}]

I have this :

abooda@bne-mns-dev-01:~/TextFSM/ntc-templates/templates$ cat huawei_display_arp.template Value IPADDRESS (\d+.\d+.\d+.\d+) Value MAC_ADDRESS (\w+-\w+-\w+)

Start ^${IPADDRESS}\s+${MAC_ADDRESS}\s -> Record abooda@bne-mns-dev-01:~/TextFSM/ntc-templates/templates$

one last query :) I have to keep inserting setting up the environment with this 👍 xport NET_TEXTFSM=/home/abooda/TextFSM/ntc-templates/templates

everytime the template calls fails , is it something i can solve permenantly and why its happening

carlmontanari commented 5 years ago

netmiko looks in ~/ntc-template/ by default, so you can either update your bashrc/profile so that the env var for "NET_TEXTFSM" is persistent, or move the etc-templates folder up to your home dir.

Abed1985 commented 5 years ago

Thank you vey much all sorted. I will play around with the templates to fetch what I want , as now I think i understand how this works! appreciate your swift response and support on this .

Abed1985 commented 5 years ago

Just one last question :) do you think such output could be parsed with regex on the same approach , I am interested in the device( system description) under each neighbor. probably I need alot of time to get around this output .. thanks I guess i will use the approach advised by Kirk above. Any hint is welcome though

display lldp neighbor GigabitEthernet0/0/0 has 0 neighbors

GigabitEthernet0/0/1 has 1 neighbors:

Neighbor index : 1 Chassis type :macAddress Chassis ID :ecb1-d7e6-e5c0 Port ID type :local Port ID :26 Port description :26 System name :ARCH-ADL-02 System description :HP J9772A 2530-48G-PoEP Switch, revision YA.15.12.0015, ROM YA.15.12 (/ws/swbuildm/YA_rel_knoxville_qaoff/code/build/lakes(swbuildm_YA_rel_knoxville_qaoff_rel_knoxville)) System capabilities supported :bridge System capabilities enabled :bridge Management address type :ipV4 Management address : 10.0.100.4
Expired time :94s

Port VLAN ID(PVID) :1 Protocol identity :

Auto-negotiation supported :Yes Auto-negotiation enabled :Yes
OperMau :speed(1000)/duplex(Full)

Power port class :PD PSE power supported :No PSE power enabled :No PSE pairs control ability:No Power pairs :Unknown Port power classification:Unknown

Link aggregation supported:No Link aggregation enabled :No Aggregation port ID :0 Maximum frame Size :0

MED Device information
Device class :Network Connectivity

HardwareRev :NA FirmwareRev :NA SoftwareRev :NA SerialNum :NA Manufacturer name :NA Model name :NA Asset tracking identifier :NA

Power Type :PSE PoE PSE power source :Unknown Port PSE Priority :Low Port Available power value:130 GigabitEthernet0/0/2 has 0 neighbors

GigabitEthernet0/0/3 has 0 neighbors

Abed1985 commented 5 years ago

just leaving this comment here incase anybody need to know what i eventually did to get a structured format from display lldp neighbor on huawei routers

`I have parsed what I need using the below :

cat huawei_display_lldp_neighbor.template

`Value SYSTEM_NAME (\S+) Value MANAGEMENT_ADDRESS (\d+.\d+.\d+.\d+) Value MANUFACTURER_NAME (.) Value MODEL_NAME (.)

Start ^System name.+:${SYSTEM_NAME} ^Management address.+:\s${MANAGEMENT_ADDRESS} ^Manufacturer name.+:${MANUFACTURER_NAME} ^Model name.+:${MODEL_NAME} -> Record`

Sample output:

python3 test-huawei-lldp.py [{'system_name': 'tsa-2960-sw01', 'management_address': '192.168.0.240', 'manufacturer_name': 'Cisco Systems, Inc.', 'model_name': 'WS-C2960S-48LPS-L'}]`