Extracting Network Parameters

Python 3: Automating Your Job Tasks Superhero Level: Automate Network Tasks with Python 3
12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.10
List Price:  €128.94
You save:  €36.84
£77.67
List Price:  £108.74
You save:  £31.07
CA$138.12
List Price:  CA$193.37
You save:  CA$55.25
A$152.38
List Price:  A$213.34
You save:  A$60.96
S$134.35
List Price:  S$188.10
You save:  S$53.74
HK$780.56
List Price:  HK$1,092.81
You save:  HK$312.25
CHF 88.35
List Price:  CHF 123.69
You save:  CHF 35.34
NOK kr1,098.66
List Price:  NOK kr1,538.17
You save:  NOK kr439.51
DKK kr687.32
List Price:  DKK kr962.28
You save:  DKK kr274.95
NZ$169.58
List Price:  NZ$237.42
You save:  NZ$67.84
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.91
৳11,746.62
List Price:  ৳16,445.74
You save:  ৳4,699.11
₹8,371.76
List Price:  ₹11,720.80
You save:  ₹3,349.03
RM465.70
List Price:  RM652
You save:  RM186.30
₦159,584.04
List Price:  ₦223,424.04
You save:  ₦63,840
₨27,830.93
List Price:  ₨38,964.42
You save:  ₨11,133.48
฿3,601.18
List Price:  ฿5,041.80
You save:  ฿1,440.62
₺3,299.80
List Price:  ₺4,619.86
You save:  ₺1,320.05
B$564.45
List Price:  B$790.25
You save:  B$225.80
R1,825.21
List Price:  R2,555.37
You save:  R730.15
Лв180.14
List Price:  Лв252.21
You save:  Лв72.06
₩138,584.75
List Price:  ₩194,024.19
You save:  ₩55,439.44
₪365.84
List Price:  ₪512.19
You save:  ₪146.35
₱5,843.06
List Price:  ₱8,180.52
You save:  ₱2,337.46
¥15,455.24
List Price:  ¥21,637.96
You save:  ¥6,182.71
MX$1,831.18
List Price:  MX$2,563.73
You save:  MX$732.54
QR364.64
List Price:  QR510.52
You save:  QR145.87
P1,361.94
List Price:  P1,906.78
You save:  P544.83
KSh13,073.69
List Price:  KSh18,303.69
You save:  KSh5,230
E£4,833.36
List Price:  E£6,766.90
You save:  E£1,933.54
ብር5,779.16
List Price:  ብር8,091.05
You save:  ብር2,311.89
Kz87,254.87
List Price:  Kz122,160.31
You save:  Kz34,905.44
CLP$94,704.52
List Price:  CLP$132,590.12
You save:  CLP$37,885.60
CN¥725.11
List Price:  CN¥1,015.19
You save:  CN¥290.07
RD$5,921.37
List Price:  RD$8,290.16
You save:  RD$2,368.78
DA13,436.79
List Price:  DA18,812.04
You save:  DA5,375.25
FJ$225.82
List Price:  FJ$316.16
You save:  FJ$90.33
Q775.39
List Price:  Q1,085.58
You save:  Q310.18
GY$20,916.55
List Price:  GY$29,284.01
You save:  GY$8,367.45
ISK kr13,788.62
List Price:  ISK kr19,304.62
You save:  ISK kr5,516
DH983.22
List Price:  DH1,376.54
You save:  DH393.32
L1,774.76
List Price:  L2,484.74
You save:  L709.97
ден5,676.16
List Price:  ден7,946.86
You save:  ден2,270.69
MOP$803.68
List Price:  MOP$1,125.18
You save:  MOP$321.50
N$1,847.94
List Price:  N$2,587.19
You save:  N$739.25
C$3,679.41
List Price:  C$5,151.32
You save:  C$1,471.91
रु13,388.03
List Price:  रु18,743.78
You save:  रु5,355.75
S/374.11
List Price:  S/523.77
You save:  S/149.65
K386.64
List Price:  K541.31
You save:  K154.67
SAR375.13
List Price:  SAR525.19
You save:  SAR150.06
ZK2,610.12
List Price:  ZK3,654.27
You save:  ZK1,044.15
L457.84
List Price:  L641
You save:  L183.15
Kč2,336.96
List Price:  Kč3,271.83
You save:  Kč934.87
Ft35,994.10
List Price:  Ft50,393.18
You save:  Ft14,399.08
SEK kr1,079.53
List Price:  SEK kr1,511.39
You save:  SEK kr431.85
ARS$92,940.28
List Price:  ARS$130,120.11
You save:  ARS$37,179.83
Bs690.73
List Price:  Bs967.06
You save:  Bs276.32
COP$404,134.11
List Price:  COP$565,803.93
You save:  COP$161,669.81
₡52,857.56
List Price:  ₡74,002.70
You save:  ₡21,145.14
L2,475.36
List Price:  L3,465.60
You save:  L990.24
₲758,175.48
List Price:  ₲1,061,476.01
You save:  ₲303,300.52
$U4,025.98
List Price:  $U5,636.54
You save:  $U1,610.55
zł393.60
List Price:  zł551.06
You save:  zł157.45
Already have an account? Log In

Transcript

Okay, so in the previous lecture, we ran a couple of commands on each of our Arista switches and got back the output of those commands. Now it's time to see how to extract certain parameters from within the output. So let's return to an output we've seen earlier, for example, this one right here, and let's copy this and paste it as a string inside the Python interpreter and see how can we, for example, extract the internet address from this output? Because maybe we want to get the output, extract this IP address, and then use it for the into a more complex application. Okay, so let me copy this and open the Python interpreter. And let's create variable A equals and let's paste this string right here, which is the output we got back from the switch.

Okay, let's see a now As I said, let's assume that we want to extract three dot three dot three dot three automatically from this output, and also consequently extract each of these IP addresses from each switch. So we want to do this in an automatic way. And each time we run the application, we want only this IP address to be returned to the screen. Okay, so first of all, notice that each line in the output is separated by a backslash, our backslash n, which is carriage return and the newline character. So immediately, we think of a way of splitting the string by using backslash r backslash n as a delimiter. And for this, we can use a dot split, and in between its parentheses, we enter the delimiter to use Enter.

And now we got a list where each element in the list is a line of the output that our switch returned. Okay, now it's time to see the end. index of the element containing the IP address we want to extract. So in order to do this, let me copy the element. And then to make our work easier, let's assign a dot split to a new variable called B. Okay, and now B is actually this list right here.

Now from this list, we are interested in the index of the element containing the IP address. So for that, we should do b dot index. And in between the parentheses of index, we based in the element, this way, we find out that the element is positioned at index 14 within this list right here. Great. So another step towards closing in through our IP address, which is the target we have for this lecture. So we have a string and we want to match only this IP address this substring from within the string.

Any idea how to do that based on what we've learned so far in this course, my opinion is that we should use Regular expressions. And in order to do that, we should first import the array module import array. And now we should think of a way to match this IP address within this string right here. First of all, since we know the index, let's create variable c equals b of 14. Let's see C. And now we have the string referenced by a variable called C. Okay, now we can use this variable further for our pattern matching. Now remember from regular expressions that we use this example where we had the art variable pointing to this string containing this IP address right here, and then we use the Find all method in order to match this pattern inside the string.

This pattern was actually made of the backslash D special character which represents a digit zero through nine. Also since an IP address may be of the form 192 dot 168 dot 100 101 in each octet of the IP address, we may have one, two, or three digits. So having that set, we can use, for example, this notation right here, which expects the character class zero through nine. So a digit one, two, or three times by specifying one comma three inside curly braces. Now translating this over to our application, we can now extract the IP address from within the string using for example, variable IP equals r e dot find all and we have raw string and the pattern. Let's better copy this.

So we have this, then we have the escaped dot. Remember the dot between each octet. Since the DOT has a special meaning in the regular expression syntax, it should be escaped whenever we want to match the actual dot itself, as we did right here. And now let's copy this for each octet. backslash dot backslash dots. And finally, this character class once again, comma, and the second argument in between the parentheses of the final method should be our variable C, referencing the string in which to look for the match, Enter.

And now let's check our solution. Let's check IP. And indeed, we got three dot three dot three dot three, a list with a single element, the pattern that was matched inside the string. In order to extract this element further, of course, we can use IP and index zero since this is the only element in the list. And now we got the IP address in the form of a string extracted from the output of the switch. Okay, now let's try to summarize all of this into a single line of code in order to use that line of code inside our application and automatically extract each IP address from each switch.

So let's go back to our variable IP and here instead of seeing Let me first maximize this as well, instead of C, we will have a dot split using the carriage return and the newline character as a delimiter. Next, we enter the index 14 that we use right here to extract this element of the list. So index 14. Okay, let's hit enter, let's see IP. And indeed, we got 32333 again. And finally, of course, IP of zero would return the string, the single element inside this list.

Okay, so this would be the way in which you can go through the output returned by the switch and extract various parameters that you need in your application. Another thing you can do in order to extract the desired IP address, for instance, from this output is to count how many IP addresses do you have in the output itself in the entire output, so that would be one IP address, two IP addresses. And finally, you also have the subnet mask. which follows the same format when it comes to pattern matching. So in total, we will have three IP addresses inside the output. For this, we can also use the final method again, to match all these patterns, the IP similar patterns inside the output, and then return a list of all the IP addresses inside the output.

For this, let's go straight into our application in the SSH connection.py function. And in between the parentheses of the print function that prints out the output. Let's replace STRF rotor output, which prints the output itself the entire output with our pattern match. So let me copy this from the Python interpreter or a dot find all okay, let's paste it in over here. So we will have this array dot find all we're looking for all the matches for this pattern inside the router output variable that we defined up here. Okay, also, let's close the parentheses of the final method.

And now let's save the file. We should get in return a list with all the IP addresses that are found inside the output. So let's run the application Python network app network app.pi user dot txt. Okay, now let's use CMD dot txt for the commands file. And finally, IP dot txt. And now let's wait for the results.

We should obtain three lists. Let's see if we do, it seems like we got a type error. So don't worry, this is life troubleshooting. And troubleshooting is an essential part of your programming tool belt. Let's read the error type error, find all missing one required positional argument string. Okay, let's return to the code.

And indeed, it seems like I have added the parenthesis in the wrong place by mistake. So let's add it in the right place at the end of this line. So let's save the file and run the application once again, user dot txt, the commands file, and finally the IP file. And now let's see the results. Type error cannot use a string pattern on a bytes like object. Okay, let's see what this means.

It seems like we cannot apply a string pattern to a bytes like object, what would that object be? That would be the router output object, right? So let's insert a print statement here for troubleshooting print type of router output to confirm this, okay, and now we should see what type what data type does this variable reference. So, for this, let's run the application once again. Sorry, I used the old Python two way of printing. Okay, now it is correct.

I know this may be boring for you. But I really hope it's not because as I said troubleshooting is essential for every programmer and being able to read errors and identify your mistakes is a crucial skill as well. So let's run the application once again, user CMD and IP. And now let's see what does this print function that we just added return, and we have class bytes. So indeed, now we have a connection with the error we previously got cannot use a string pattern on a bytes like object. So since we have router output as a different data type than a string, then we should simply convert it to a string using the str function.

So str of router output, let's save the file. And now let's run the application once again, and see what happens. Indeed, success. This time, we got a list of all the IP addresses in the output for each of our switches. Of course, we should delete this print function right here because we don't need it anymore. Okay, let's save the file.

And now let's return to our three lists. So remember that we said we want to extract the IP address of the loopback zero interface for each of these switches. Since we have three lists, and the IP address is the second element in the list, then we will print out only the element at index one from each of these lists. So let's go back into our code. And after the qualifying goal statement right here, let's add in the index one, and see if we get the desired result. So let's try running the application one more time.

And indeed, let's congratulate ourselves. We extracted the IP address from each of the Arista switches for the loopback zero interface. And now using this by assigning it to a variable, we can further use it in a more complex application. I hope you really enjoyed this lecture. I know it has been quite long and sprinkled with all kinds of issues, but knowing how to extract some data From an output of a switch or a router, how to play around with that data, how to troubleshoot any potential issues or must have skills for any network automation engineer. Okay, I'll see you in the next lecture.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.