python network programming

Download Python Network Programming

Post on 13-Jan-2017

656 views

Category:

Software

2 download

Embed Size (px)

TRANSCRIPT

  • Python Network Programming - Build 7 Apps

    TrendElearning Copyright Material

    Mihai Catalin Teodosiu

    1

    Python Network Programming - Build 7 Apps

  • Python Network Programming - Build 7 Apps

    TrendElearning Copyright Material

    Mihai Catalin Teodosiu

    2

    Whats this all about?

    Note: This e-book is a comprehensive preview of my bestselling course: Python

    Tutorial: Python Network Programming - Build 7 Apps. More details on the last page!

    Do you want to become a Python Developer without having to spend a lot of money

    on books and boring theoretical courses?

    Are you a network professional who wants to start automating network tasks?

    Or maybe you're seeking a raise or even a career change?

    Join thousands of successful students who have decided to learn Python,

    upgrade their networking skills and boost their careers using this 100% hands-on

    course!

    Python (2.7.3) programming course aimed not only at network professionals, but at

    anyone having little or no experience in coding or automation and a great desire to start

    learning Python from scratch.

    Save Time And Money By Writing Your Own Python Programs To Automate Daily

    Network Tasks!

    During this course you will learn Python concepts which are relevant to your networking

    job and build some amazing network tools:

    Python App#1: Subnet calculator.

    Python App#2: Configuring multiple network devices concurrently via SSH or

    Telnet.

    Python App#3: DHCP client simulator for testing a DHCP server in the local

    network.

    Python App#4: Collecting information from routers and storing it in a MySQL

    database.

    Python App#5: OSPF network discovery via SNMP. Building the OSPF

    topology.

  • Python Network Programming - Build 7 Apps

    TrendElearning Copyright Material

    Mihai Catalin Teodosiu

    3

    Python App#6: Basic network sniffer.

    Python App#7: Configuration file comparator.

    Sounds unbelievable given your current programming experience? Well, it's true! How?

    First, you will learn and practice every Python key concept, which is explained in one or

    more video lectures, followed by a short quiz. Each video is filled with relevant

    examples, in a learn-by-doing fashion and the quizzes will help you consolidate the

    main ideas behind each Python topic.

    After laying the foundation (and also exploring some advanced Python topics), you will

    dive right into the real-life network scenarios and apply your knowledge to build 7

    great network tools.

    Equipped with working files, network topologies and Python code samples (in .pdf and

    .py formats), you will be able to work alongside me on each lecture and each

    application. I will provide a virtual machine with all the Python modules already installed

    and also the full code for each application, so you can save time and start coding

    and testing on the spot.

    We will use emulated routers in GNS3 to test our Python apps in a network

    environment, so you can see the actual results of running your code.

    I encourage you to learn Python, an amazingly beginner-friendly programming

    language and take your (networking) job to a higher level of automation.

    IMPORTANT BEFORE YOU CONTINUE!

    Next, please find some working code samples from my bestselling course: Python

    Tutorial: Python Network Programming - Build 7 Apps. All the code samples are

    explained, turned into working applications and tested inside the course.

    More details about enrolling in the course are provided on the last page of this e-book!

  • Python Network Programming - Build 7 Apps

    TrendElearning Copyright Material

    Mihai Catalin Teodosiu

    4

    Telnet with Python

    #Open telnet connection to devices

    def open_telnet_conn(ip):

    #Change exception message

    try:

    #Define telnet parameters

    username = 'teopy'

    password = 'python'

    TELNET_PORT = 23

    TELNET_TIMEOUT = 5

    READ_TIMEOUT = 5

    #Logging into device

    connection = telnetlib.Telnet(ip, TELNET_PORT, TELNET_TIMEOUT)

    output = connection.read_until("name:", READ_TIMEOUT)

    connection.write(username + "\n")

    output = connection.read_until("word:", READ_TIMEOUT)

    connection.write(password + "\n")

    time.sleep(1)

  • Python Network Programming - Build 7 Apps

    TrendElearning Copyright Material

    Mihai Catalin Teodosiu

    5

    #Setting terminal length for entire output - no pagination

    connection.write("terminal length 0\n")

    time.sleep(1)

    #Entering global config mode

    connection.write("\n")

    connection.write("configure terminal\n")

    time.sleep(1)

    #Open user selected file for reading

    selected_cmd_file = open(cmd_file, 'r')

    #Starting from the beginning of the file

    selected_cmd_file.seek(0)

    #Writing each line in the file to the device

    for each_line in selected_cmd_file.readlines():

    connection.write(each_line + '\n')

    time.sleep(1)

    #Closing the file

    selected_cmd_file.close()

  • Python Network Programming - Build 7 Apps

    TrendElearning Copyright Material

    Mihai Catalin Teodosiu

    6

    #Test for reading command output

    #output = connection.read_very_eager()

    #print output

    #Closing the connection

    connection.close()

    except IOError:

    print "Input parameter error! Please check username, password and file name."

    #Calling the Telnet function

    open_telnet_conn(ip)

  • Python Network Programming - Build 7 Apps

    TrendElearning Copyright Material

    Mihai Catalin Teodosiu

    7

    SSH with Python

    #Open SSHv2 connection to devices

    def open_ssh_conn(ip):

    #Change exception message

    try:

    #Define SSH parameters

    selected_user_file = open(user_file, 'r')

    #Starting from the beginning of the file

    selected_user_file.seek(0)

    username = selected_user_file.readlines()[0].split(',')[0]

    #Starting from the beginning of the file

    selected_user_file.seek(0)

    password = selected_user_file.readlines()[0].split(',')[1]

    #Logging into device

    session = paramiko.SSHClient()

  • Python Network Programming - Build 7 Apps

    TrendElearning Copyright Material

    Mihai Catalin Teodosiu

    8

    session.set_missing_host_key_policy(

    paramiko.AutoAddPolicy())

    session.connect(ip, username = username, password = password)

    connection = session.invoke_shell()

    #Setting terminal length for entire output - no pagination

    connection.send("terminal length 0\n")

    time.sleep(1)

    #Entering global config mode

    connection.send("\n")

    connection.send("configure terminal\n")

    time.sleep(1)

    #Open user selected file for reading

    selected_cmd_file = open(cmd_file, 'r')

    #Starting from the beginning of the file

    selected_cmd_file.seek(0)

  • Python Network Programming - Build 7 Apps

    TrendElearning Copyright Material

    Mihai Catalin Teodosiu

    9

    #Writing each line in the file to the device

    for each_line in selected_cmd_file.readlines():

    connection.send(each_line + '\n')

    time.sleep(2)

    #Closing the user file

    selected_user_file.close()

    #Closing the command file

    selected_cmd_file.close()

    #Checking command output for IOS syntax errors

    output = connection.recv(65535)

    if re.search(r"% Invalid input detected at", output):

    print "* There was at least one IOS syntax error on device %s" % ip

    else:

    print "\nDONE for device %s" % ip

    #Test for reading command output

    #print output + "\n"

  • Python Network Programming - Build 7 Apps

    TrendElearning Copyright Material

    Mihai Catalin Teodosiu

    10

    #Closing the connection

    session.close()

    except paramiko.AuthenticationException:

    print "* Invalid username or password. \n* Please check the username/password

    file or the device configuration!"

    print "* Closing program...\n"

    #Calling the SSH function

    open_ssh_conn(ip)

  • Python Network Programming - Build 7 Apps

    TrendElearning Copyright Material

    Mihai Catalin Teodosiu

    11

    SNMP with Python

    #!/usr/bin/env python

    from pysnmp.entity.rfc3413.oneliner import cmdgen

    #SNMP function

    def snmp_get(ip):

    #Creating command generator object

    cmdGen = cmdgen.CommandGenerator()

    #Performing SNMP GETNEXT operations on the OSPF OIDs

    #The basic syntax of nextCmd: nextCmd(authData, transportTarget, *varNames)

    #The nextCmd method returns a tuple of (errorIndication, errorStatus, errorIndex,

    varBin

Recommended

View more >