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.26
List Price:  €129.17
You save:  €36.90
£78.58
List Price:  £110.02
You save:  £31.43
CA$136.53
List Price:  CA$191.15
You save:  CA$54.61
A$150.05
List Price:  A$210.08
You save:  A$60.02
S$134.83
List Price:  S$188.77
You save:  S$53.93
HK$780.51
List Price:  HK$1,092.75
You save:  HK$312.23
CHF 91.40
List Price:  CHF 127.97
You save:  CHF 36.56
NOK kr1,068.48
List Price:  NOK kr1,495.92
You save:  NOK kr427.43
DKK kr688.44
List Price:  DKK kr963.84
You save:  DKK kr275.40
NZ$163.35
List Price:  NZ$228.70
You save:  NZ$65.35
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳11,721.12
List Price:  ৳16,410.04
You save:  ৳4,688.92
₹8,326.41
List Price:  ₹11,657.31
You save:  ₹3,330.90
RM469.20
List Price:  RM656.90
You save:  RM187.70
₦143,785.62
List Price:  ₦201,305.62
You save:  ₦57,520
₨27,847.71
List Price:  ₨38,987.91
You save:  ₨11,140.19
฿3,633.91
List Price:  ฿5,087.62
You save:  ฿1,453.71
₺3,217.97
List Price:  ₺4,505.29
You save:  ₺1,287.32
B$512.23
List Price:  B$717.15
You save:  B$204.91
R1,817.89
List Price:  R2,545.11
You save:  R727.22
Лв180.27
List Price:  Лв252.39
You save:  Лв72.11
₩136,618.14
List Price:  ₩191,270.87
You save:  ₩54,652.72
₪367.26
List Price:  ₪514.18
You save:  ₪146.92
₱5,803.01
List Price:  ₱8,124.45
You save:  ₱2,321.43
¥15,641.03
List Price:  ¥21,898.07
You save:  ¥6,257.04
MX$1,661.48
List Price:  MX$2,326.14
You save:  MX$664.66
QR364.73
List Price:  QR510.64
You save:  QR145.90
P1,348.30
List Price:  P1,887.68
You save:  P539.37
KSh13,198.68
List Price:  KSh18,478.68
You save:  KSh5,280
E£4,678.53
List Price:  E£6,550.13
You save:  E£1,871.60
ብር5,747.26
List Price:  ብር8,046.40
You save:  ብር2,299.13
Kz85,041.49
List Price:  Kz119,061.49
You save:  Kz34,020
CLP$88,840.11
List Price:  CLP$124,379.71
You save:  CLP$35,539.60
CN¥709.27
List Price:  CN¥993.01
You save:  CN¥283.74
RD$5,874.72
List Price:  RD$8,224.84
You save:  RD$2,350.12
DA13,454.48
List Price:  DA18,836.81
You save:  DA5,382.33
FJ$226.07
List Price:  FJ$316.51
You save:  FJ$90.44
Q776.68
List Price:  Q1,087.38
You save:  Q310.70
GY$20,942.51
List Price:  GY$29,320.35
You save:  GY$8,377.84
ISK kr13,848.61
List Price:  ISK kr19,388.61
You save:  ISK kr5,540
DH992.77
List Price:  DH1,389.92
You save:  DH397.15
L1,766.77
List Price:  L2,473.55
You save:  L706.77
ден5,684.23
List Price:  ден7,958.15
You save:  ден2,273.92
MOP$804.15
List Price:  MOP$1,125.85
You save:  MOP$321.69
N$1,813.62
List Price:  N$2,539.14
You save:  N$725.52
C$3,682.63
List Price:  C$5,155.83
You save:  C$1,473.20
रु13,330.40
List Price:  रु18,663.10
You save:  रु5,332.69
S/374.77
List Price:  S/524.70
You save:  S/149.92
K388.70
List Price:  K544.20
You save:  K155.49
SAR375.01
List Price:  SAR525.03
You save:  SAR150.02
ZK2,583.64
List Price:  ZK3,617.21
You save:  ZK1,033.56
L459.02
List Price:  L642.65
You save:  L183.62
Kč2,281.25
List Price:  Kč3,193.84
You save:  Kč912.59
Ft35,715.37
List Price:  Ft50,002.94
You save:  Ft14,287.57
SEK kr1,072.36
List Price:  SEK kr1,501.36
You save:  SEK kr428.99
ARS$88,916.93
List Price:  ARS$124,487.26
You save:  ARS$35,570.33
Bs691.33
List Price:  Bs967.89
You save:  Bs276.56
COP$382,975.61
List Price:  COP$536,181.17
You save:  COP$153,205.56
₡51,262.22
List Price:  ₡71,769.17
You save:  ₡20,506.94
L2,472.61
List Price:  L3,461.75
You save:  L989.14
₲751,156.78
List Price:  ₲1,051,649.55
You save:  ₲300,492.76
$U3,841.99
List Price:  $U5,378.94
You save:  $U1,536.95
zł393.60
List Price:  zł551.05
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.