gutmang Absent Member.
Absent Member.
4311 views

HPNA expect, making lists

Jump to solution

I am looking for some help and direction in making advanced scripts in HPNA.

 

I am attempting to do an advanced query of the device(s), make a list of IP addresses, interfaces and descriptions.

 

From those lists, I would then define a set of input commands which would pull from those lists, and make config additions.

 

I bought the books and searched on-line, but still stumped on how to make lists and the syntax required.

 

Any HPNA expect how-to-guides or suggestions?

 

========================================================================

Here is what I have so far for the query and list part of the config.

 

### begin psedo code ###

exp_internal 1   ;#this debug mode for this script

 

puts " show ospf neighbor | match gr-" ;#this displays the ospf links

                get $next_hop {\d+\.\d+\.\d+\.\d+} from output            ;#this caputures the ip from first column to be used as next-hop

                                get $interface {need to define pattern} from output       ;#this captures the interface to be queried again to get source IP

 

//consider using $next_hop and $interface_list//

 

puts "show configuration $interface | display set

                get $ip_source from output        ;#this will be used for source IP for "set services rpm probe ~"

                                $ip_source {\d+\.\d+\.\d+\.\d+} from output    ;#this pattern need to verified

puts "

 

### consider the following to get all GR- and addresses and put them in a list for use later on

### "show configuration | match inet | match family | display set | match gr- | match address"

 

## gr-0/0/0. OUTPUT, needs pattern

 

{\gr-\d\/\d\/\d\.}

 

0 Likes
1 Solution

Accepted Solutions
Chris_Powers Acclaimed Contributor.
Acclaimed Contributor.

Re: HPNA expect, making lists

Jump to solution

OK, you refer to parameters and variables and this makes it more difficult to answer the question. 

 

In general, yes, you can use API commands in the proxy and that will let you look at a config or diagnostic to see if something does or doesn't exist for a given device. 


This part is pretty simple.

 

It's when you want to change something that the process can be a bit more involved. 

 

Let's say you have two cases you want to handle:

 

1) ACL 10 should allow IP address 10.10.10.1

 

2) All Ethernet interfaces should have a description of Hello if it has an IP Address of 172.16.*.*

 

I can easily check to see if a config contains both of these, but in order to fix either if they don't, I have to have additional information.  I can't always just connect to a device and issue a command and it fix the issue.  For the ACL, I'd probably need to know what the ACL is currently in case I removed it and then readded it with the new line.  You can do this with NA, as it keeps track of ACLs, but it's something you need to code for. 

 

Same sort of thing with the Ethernet interfaces.  You need to know what the interface is specifically - Ethernet0, Ethernet0/0, Ethernet 1, etc.  Again, you can get this information in NA, but without knowing what parameters you're trying to set, I can't say how easy or hard they'd be to do in a script.

 

As far as your question on accessing the snapshot data, yes you can do this:

 

show device config -ip $tc_device_ip$

 

This returns a string, you just then need to parse the string and see if what you are interested exists or not. 

 

You could also try show configlet -ip  $tc_device_ip$ -start "start line" -end "stop line" 

 

 

Also, keep in mind, you can have dynamic groups based off of configs, so you can create a group of devices that do | don't have a particular line or lines.  Depends on what you're looking for in a config. 

 

I'd suggest splitting this up at first. 

 

First create a script that gives you the list of devices that have one issue

 

Then once you get that, you can modify it to handle other issues; where each issue is contained in a device group

 

Lastly, you can work on remediation of each issue, as I think that could be the most challenging. 

 

-Chris

View solution in original post

0 Likes
7 Replies
Chris_Powers Acclaimed Contributor.
Acclaimed Contributor.

Re: HPNA expect, making lists

Jump to solution

Hi,

 

I may be able to help a little, but am curious of a few things you are looking to do.

 

so, are you looking to do all the work using the NA proxy and API commands, or are you looking to have create a list and then have each device in the list connect to the (live) device, issue the command(s) and capture the output (in a file?).  It's at this point I'm getting  a bit confused. 

 

This may or may not help, but you could run a diagnostic and issue a command (as an example show ospf neighbor | match gr-") and then in the proxy work with the output of that command.  Or you could run a policy against the diagnostic and then have a remediation script to fix the issue. 

 

I don't have anything in expect that does anything that technical, but do have a few things in Perl, but they just use the proxy / APIs as opposed to connect to devices and pull commands.  I'll look to see if I can find anything else, but if the Perl code would help, let me know.

 

-Chris

 

0 Likes
Highlighted
gutmang Absent Member.
Absent Member.

Re: HPNA expect, making lists

Jump to solution

Chris,

 

Thank you for the reply. The way I understand there is a way to query what is already in HPNA, instead of querying the box.

 

Currently we utilize scripts and we run them against a number of devices, the script logs in and builts a database by querying the box after login. 

 

After working with an expect guy, we have come up with the following. Its not perfect but its a start.

 

#send "show ospf neighbor | match gr\r"

#set i 0

#while {1} {

#    expect -re {\d+\.\d+\.\d+\.\d+\s+gr-\d\/\d\/\d\.\d+} {

#        set NextHop($i) [regexp -inline {\d+\.\d+\.\d+\.\d+} $expect_out(0,string)]

#        set interface($i) [regexp -inline {gr.+} $expect_out(0,string)]

#        incr i

#       

#    } timeout {break}

#}

#parray NextHop

#parray interface

 

#send "show configuration | match inet | match family | display set | match gr- | match address\r"

 

#set i 0

#while 1 {

#    expect -re {\d+\.\d+\.\d+\.\d+} {

#        set SourceIP($i) $expect_out(0,string)

#        set i [expr $i+1]

#    } timeout {break}

#}

Tags (3)
0 Likes
Chris_Powers Acclaimed Contributor.
Acclaimed Contributor.

Re: HPNA expect, making lists

Jump to solution

Something else you may want to take a look at, if for no other reason that to get an understanding of how NA does Expect scripts, you can have NA create an Expect (or Perl) script for you based on a session.

 

So, pick a device that you'd plan to run your script on.

 

Connect to it via NA

 

Run your command

 

Exit the device

 

When you're back in NA, go to that device's page, then select View -> Telnet / SSH Sessions

 

From there, pick the session and click view commands only.

 

In the upper right hand side, you'll see links to Convert to Expect or Convert to Perl. 

 

NA will do it's best effort to turn your commands into code.  I used this when I started out to get an idea of how NA connects to the Proxy, uses it's variables, etc. 

 

When you get a good understanding of the script, you can take your code and the NA-generated code and come up with a script that'll work well for your needs. 

 

Hope this helps.

 

-Chris

0 Likes
gutmang Absent Member.
Absent Member.

Re: HPNA expect, making lists

Jump to solution

Chris 

 

I did try that. However it does not appear to work for me.

 

When I view the device, it just states, "no sessions found for this device"

 

Thank you for that suggestion, I will keep looking into it.

 

I think its due to account setting, I am not the admin, but it appears I dont have the right permissions.

 

 

0 Likes
Chris_Powers Acclaimed Contributor.
Acclaimed Contributor.

Re: HPNA expect, making lists

Jump to solution

Hello,

 

It's possible it's a credentials issue.  If you want to double-check, you can do either of these:

 

1) Go to a device home page for a device that you want to involve in your script

    Click Connect -> then select either telnet or ssh, whatever is required by the device / standards

    log into device if needed

   perform a command - show version would work

    then exit the device

 

I've seen some NA setups where you cannot do this from the server, so I listed another option to connect to proxy from host below:

 

2) In putty or similar application, connect to the NA proxy, you should see a prompt similar to the one below:

NA Login:  {enter your ID}

Password: {enter your password}

HP Network Automation Version 9.22

Type "HELP connect" to see how to connect to a device. Type "HELP" to view a list of available commands.

NA>

 

from the NA> prompt, type "connect {insert ip address | hostname of device}

 log into device if needed

   perform a command - show version would work

    then exit the device

 

Either of these should get you a session to work with if your credentials will let you log in. 

 

-Chris

0 Likes
gutmang Absent Member.
Absent Member.

Re: HPNA expect, making lists

Jump to solution

Chris,

 

Just saw your quesition in previous post.

 

I would like to do the following.

 

  1. querry the HPNA database for parameters
  2. make a set of variables from the matches
  3. have HPNA log into the devices and issue configuration based on the previous matches

 

currently, the work around is at step one, and as a result, this is the process

 

  1. manually query the HPNA list for devices we want to update
  2. manually put those devices in a list
  3. run advanced script against the manual list
  4. script queries the device live and makes lists of variables
  5. script uses the variables to configure the devices

 

I was hoping to find a way to querry the HPNA existing snapshots in the begining of the script instead of collecting the info from the devices in realtime one at a time.

 

Is there a way to do this?

 

 

 

 

0 Likes
Chris_Powers Acclaimed Contributor.
Acclaimed Contributor.

Re: HPNA expect, making lists

Jump to solution

OK, you refer to parameters and variables and this makes it more difficult to answer the question. 

 

In general, yes, you can use API commands in the proxy and that will let you look at a config or diagnostic to see if something does or doesn't exist for a given device. 


This part is pretty simple.

 

It's when you want to change something that the process can be a bit more involved. 

 

Let's say you have two cases you want to handle:

 

1) ACL 10 should allow IP address 10.10.10.1

 

2) All Ethernet interfaces should have a description of Hello if it has an IP Address of 172.16.*.*

 

I can easily check to see if a config contains both of these, but in order to fix either if they don't, I have to have additional information.  I can't always just connect to a device and issue a command and it fix the issue.  For the ACL, I'd probably need to know what the ACL is currently in case I removed it and then readded it with the new line.  You can do this with NA, as it keeps track of ACLs, but it's something you need to code for. 

 

Same sort of thing with the Ethernet interfaces.  You need to know what the interface is specifically - Ethernet0, Ethernet0/0, Ethernet 1, etc.  Again, you can get this information in NA, but without knowing what parameters you're trying to set, I can't say how easy or hard they'd be to do in a script.

 

As far as your question on accessing the snapshot data, yes you can do this:

 

show device config -ip $tc_device_ip$

 

This returns a string, you just then need to parse the string and see if what you are interested exists or not. 

 

You could also try show configlet -ip  $tc_device_ip$ -start "start line" -end "stop line" 

 

 

Also, keep in mind, you can have dynamic groups based off of configs, so you can create a group of devices that do | don't have a particular line or lines.  Depends on what you're looking for in a config. 

 

I'd suggest splitting this up at first. 

 

First create a script that gives you the list of devices that have one issue

 

Then once you get that, you can modify it to handle other issues; where each issue is contained in a device group

 

Lastly, you can work on remediation of each issue, as I think that could be the most challenging. 

 

-Chris

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.