I would suggest to check the post Python Basic in Desktop/Laptop first, as that would give you good idea about the command line.
Here I would share how you can practice Python Programming in Telnet Server, Network Engineers usually have the Telnet Server for logging into routers which they manage. So its easy for you to practice whenever you get time also you can test many things in Network Automation.
Usually the telnet servers run on Solaris Unix OS, which you can check by below command.
rselvara@nms4:~$ uname -a
SunOS nms4.lon.osspriv.colt.net 5.11 11.3 i86pc i386 i86pc
Hence, I would walk you through some of then UNIX commands as to how to create python programs and run it.
>> Verify Python is installed and see version running. As you see below my telnet server has got 2.7 version of python.
Note:- You can practise the commands in python prompt however its best practice to create python file and run it to see
the outcomes when a program is executed.
>> Pwd will show the current directory/Folder you’re working and as you see no file is present in my Directory.
To create python program, type below commands. Please practice below commands repeatedly until you remember.
rselvara@nms4:~$ pwd
/home/rselvara
rselvara@nms4:~$ ls -l <— List of files in list format
total 0
rselvara@nms4:~$ vim learn.py
<—- This will write the program.
<—- This will exit the command prompt and back to directory.
rselvara@nms4:~$ ls -l
total 2
-rw-r–r– 1 rselvara staff 55 Mar 22 17:14 learn.py
rselvara@nms4:~$ python learn.py
+++++++++
Hello
+++++++++
rselvara@nms4:~$
Note:- You can edit the program in same way and save it.
Below shown are few programs which will you help you learn the basic usage in network device automation.
Program 1:- How to take the input from users !!
INPUT:- Below given the 3 input methods to take number and string. And can see the type of input if it is Str or Int.
OUTPUT:- Input() always take Integer values only, and other method consider inputs as string only however you can convert string to integer wherever you want to process for logical operation.
Program 2:- How you can split IP, store the values in variable and perform logical operation !!
Program 3:- “If and elsif” condition based on your input given the output will be printed.
Program 4:- I will explain the use of append and extend with examples below:-
>> append() method adds an element to a list/Array
Input:-
List1 = [‘A’, ‘B’, ‘C’]
List2 = [‘1,’ ‘2’, ‘3’]
List2.append(List1)
print List2
Output:- [‘1’, ‘2’, ‘3’, [‘A’, ‘B’, ‘C’]]
So List2[0] = 1 , List2[1] = 2 , List2[2] = 3, List2[3] = [‘A’, ‘B’,’C’]
>> extend() method adds as list/array element inside the List.
Input:-
List1 = [‘A’, ‘B’, ‘C’]
List2 = [‘1’, ‘2’, ‘3’]
List2.extend(List1)
print List2
Output:- [‘1’, ‘2’, ‘3’, ‘A’, ‘B’, ‘C’]
so List2[0] = 1 , List2[1]=2, List2[2]=3, List2[3]=A, List2[4]=B, List2[5]=C
Program 5:- While Logic is similar to FOR logic, however it has more ways to control a loop. The loop will run as long as the expression in while is correct so be careful while using it. The command “Break” inside the ‘while‘ will help to come out of loop. You can use this Break and Continue commands in ‘For‘ Loop as well..
+++++ Example 1:- Here the loop was running till n =2 ++++++++
Input:-
n = 5
while n = > 0:
n = n – 1
if n == 2:
break
print(n)
print(‘Loop ended’)
Output:-
4
3
Loop ended
+++++Example 2:- The loop was running till end n=0 however skipped next statement when n = 2 ++++++++
Input:-
n = 5
while n = > 0:
n = n – 1
if n == 2:
continue
print(n)
print(‘Loop ended’)
Output:-
4
3
1
0
Loop ended
+++++++++++++++++++
Remember ‘while‘ logic has to be carefully used to determine the end first. Here the loop is running till the line doesn’t have ‘exit-address-family’
The motive is to find network statement in complete router config, however ‘while‘ loop helps to search till ‘exit-address-family’ otherwise we end up searching whole config.
>>Print all till particular line:- however in ‘while‘ there is more control of line reading based on expression.
>>Eliminate a line:- Here the continue statement made the Network statement skipped going to next line and made the next print of all lines till loop ends.
>> End loop when statement found:- Here if you see as soon as our statement is found the break ends the loop saving time cycle.
Program 6:- Using For logic, to read high volume of data and printing the necessary data in your output.
As you see , full BGP config is read in the input and stored in array with append of each line in variable ‘A’ by a For loop.
As you see below, based on the input config given AS number and Neighbor IP data is taken out.
Hence you can use complete router config as input and find whatever details you want in the output.
Hope this has given a good idea about basic handling of router config in networking and make your program to generate the desired output, from this learning can you please try to generate a program that would fetch the output as shown in the thumbnail of this post. I would be glad if you would make it, and progress towards further logical way of handling files like text, excel data with python.
I would have more post of some practical uses in this section, where you would get an idea and develop more of it.
Hope you like it, Wish you all the best!!
(3 votes, average: 5.00 out of 5)