Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Python Network Programming for Network Architects and Engineers Vince Kelly TSA Session ID: DEVNET-1040 Agenda • Python Basics • The Python Socket Module • Security • ‘Higher Layer’ Protocols & APIs: XMLRPC • Higher Layer’ Protocols: REST & HTTP • Conclusion Python Basics • Working with the Command Line • Python Basics: Working With files, Directories and Strings DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 4 Sockets: • What Can We Get Just From the Python Standard Library Alone? • Python Socket Library • Starting Out With UDP • What Are Sockets? • B R I E F Primer on OOP versus POP • The ‘Big Five’ Questions to Answer • Socket Objects • Socket Names DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 5 Sockets: • Moving on To TCP • Parsing Directories and Files with OS.Walk • Simple Message Passing Using TCP • Simple File Transfer Using TCP • Simple File Transfer With Directory Search Using TCP DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 6 SECURITY • One-way Hash Creation • Display All Available Hash Algorithms • Generate a One-way Hash for a File • Verifying a File Has Not Been Tampered With – Step 1: Client Side, Server Side • Verifying a File Has Not Been Tampered With – Step 2 Client Side, Server Side • Establish Client/Server Connection Over SSL – Client Side, Server Side DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 7 ‘Higher Layer’ Protocols & APIs • Building A Spreadsheet Primer • XMLRPC DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 8 ‘Higher Layer’ Protocols: REST & HTTP • REST and HTTP • TCP Send a REST Request to Google • ‘Screen Scraping’ using Python URLLIB • APIC REST Interface DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 9 WHAT’S THE POINT of ALL THIS?? “Good Programmers write code, GREAT Programmers steal code” DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 10 Python Basics Follow Along: followCmd_line.py Parsing command line followFiles.py Working with files, directories and strings followFile_Info.py Retrieves file size, creation, modified and accessed on a file followFile_Encrypt.py Encodes content and writes to disk followFile_Decrypt.py Decodes file and prints to screen followPing.py Detect what OS we are on, Make system call to execute ping (windows) DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 11 Python Basics: Working with the Command Line Start python Interpreter (python.exe) Find & execute this program (Program_xyz.py) Save whatever comes after the program name as command line argumentS (www.cisco.com, 192.168.10.1) argv = [ ‘Program_xyz.py’, ‘www.cisco.com’, ‘192.168.10.1’ ] print argv[0] ‘Program_xyz.py’ print argv[1] ‘www.cisco.com’ DEVNET-1040 print argv[2] ‘192.168.10.1’ © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 12 Python Basics: Working with the Command Line followCmd_Line.py - Uses built-in SYS module: sys.argv() and len() built-in methods Displays: - The programs (empty) command line, number of arguments, etc - Appends ‘Hello’ ‘World’ to the command line - The programs (non-empty command line, number of arguments, etc - Removes the last entry from the cmd line - Example of Type Casting a string value entered by user into an integer DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 13 Python Basics: Working With files, Directories and Strings DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 14 Python Basics: Working With files, Directories and Strings followFiles.py - Uses built-in os module: os.getcwd() built-in method and .lower() string method Concatenates IP address, User Name and Password entered by user Calls a function with the concatenated message as a parameter Takes directory and file name from user (or defaults to CWD) Opens file in append mode Loops waiting forever on more input from user DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 15 DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 16 Python Basics: Working With files, Directories and Strings followFile_Info.py - Uses built-in getsize(), .stat() built-in methods Uses time.ctime() to return file creation, modification and last access DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 17 Python Basics: Working With files, Directories and Strings followEncrypt.py - Uses built-in codecs and os modules: Encodes user input with ROT-13 Loops forever on user input DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 18 Python Basics: Working With files, Directories and Strings followDecrypt.py - Uses built-in codecs and os modules: Decodes user input using ROT-13 Loops forever on user input DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 19 Python Basics: Working With files, Directories and Strings followPing.py - - Uses built-in os module: os.name() returns the operating system ('posix', 'nt', 'mac', 'os2', 'ce', 'java', 'riscos‘) Makes system call to execute ping and display the results Displays warning if host doesn’t respond after timeout Loops forever on user input DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 20 Network Communications SERVER Data Warehou se CLIENT App Pytho n Enviro nment Web Service s Operating System Web Browse r Operating System Pytho n Enviro nment Communications Stack(s) CLIENT Pytho n Enviro nment App Web Browse r Operating System DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 22 Sockets: What Can We Get Just From the Python Standard Library Alone? TCP/IP PROTOCOL STACK Python Socket Library ACI APIC Firefox Browser XMLRPC “Higher Layer” Libraries HTTP, etc. Web 80 FTP 20,21 Telnet 23 SMTP 25 SNMP 161 TCP DNS 53 UDP IP Device Drivers Tx Buffers LAYER 1 B U S Framer/ Protocol decode Rx Buffers NIC DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 23 Python Socket Library Follow Along: followSocket_Examples.py Displays local socket information followSock_Scan1.py Scans remote services followDisplay_SocketProtocols.py Displays basic IPv4 and IPv6 stack information followUDP_Client.py Basic UDP Client followUDP_Server.py Basic UDP Server followUDP_ClientEncoded.py Receives an encoded ROT-13 message and decodes it followUDP_CountDownServer.py Broadcasts an encoded message DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 24 gethostname() return the current hostname gethostbyname() map a hostname to its IP number 'gethostbyname_ex() IPv4 Only gethostbyaddr() -- map an IP number or hostname to DNS info getservbyname() map a service name and a protocol name to a port number getprotobyname() map a protocol name (e.g. 'tcp') to a number getaddrinfo() getfqdn() getnameinfo() getservbyport() socket.setdefaulttimeout() set the default timeout value socket.getdefaulttimeout() get the default timeout value 'has_ipv6', DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 25 Sockets: Let’s Start with UDP TCP/IP PROTOCOL STACK Python Socket Library ACI APIC Firefox Browser XMLRPC “Higher Layer” Libraries HTTP, etc. Web 80 FTP 20,21 Telnet 23 SMTP 25 SNMP 161 TCP DNS 53 UDP IP Device Drivers Tx Buffers LAYER 1 B U S Framer/ Protocol decode Rx Buffers NIC DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 26 What Are Sockets? • Is an abstract concept that represents an end point. Programs use sockets to communicate with other programs which may or may not be on the same computer. • A socket is defined by IP address, the port it listens on and the protocol used • Client/Server sockets: represent endpoints of a conversation. Server sockets just produce more client sockets e.g., Web Browser uses a client socket, Web server uses a server socket to ‘listen’ on.. • To create a client/server connection you: • • • Create/”spin up” a socket object. This contains all the methods needed to communicate Bind to the port you want. If successful, the socket exists for the length of the session.. Client sockets are normally only used for one exchange (or a small set of sequential exchanges). They are created and torn down frequently. DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 27 socket.getaddrinfo("192.168.252.253", 80, 0, 0, socket.SOL_TCP) [ (2, Family 0, 6, '', ('192.168.252.253', 80)) ] Socket Protocol Canonical Type Type Name socket.getaddrinfo('2a04:4e42:5::223',80, 0, 0, socket.SOL_TCP) [(23, IPv6 0, 6, 1 STREAM, 2 DATAGRAM, '', Socket Name: 6 Tuple ('2a04:4e42:5::223', 80, Canonical TCP Socket Name: 2 Tuple 0, Flow Label 0))] Scope Name 3 RAW, 4 RDM, 5 SEQPACKET DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 28 But Before We Jump In, a B R I E F Primer on OOP versus POP……. DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 29 The ‘Big Five’ Questions to Answer: What kind of network do we want to talk to? 1) • What ‘address family’? IPv4 (AF_INET) or IPv6 (AF_INET6), etc. What type of connection do we want? 2) • Do we want a datagram service (UDP) or a connection oriented stream service (TCP)? What kind of protocol do we need? 3) • Answers 1 and 2 already narrowed this down 4) What IP address to use? 5) What UDP or TCP port number should we use? DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 30 Socket Objects Retrieve (and use) the Python socket module import socket Python socket module’s METHOD c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) New socket object What Protocol Stack Version? What Part of the Protocol Stack Do You Want ? INET: SOCK_STREAM: use TCP SOCK_DGRAM: use UDP use IPv4 INET6: use IPv6 What do we mean by a ‘socket object’? DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 31 Sockets client import socket c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) Socket object Socket object c.connect((‘www.cisco.com’,80)) ‘c’ - Gets instantiated/’spun up’ (on each side) through the python socket module - Handles setting up the session - Handles sending & receiving data - Handles Error recovery - Handles ending/tearing down the session DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 32 Sockets server import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serverAddr = S.gethostname() s.bind((serverAddr,12345)) s.listen(5) while 1: client, cleintAddr = s.accept() 10.255.88.76 NIC DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 36 Sockets server import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serverAddr = ‘10.255.88.76’ client s.bind((serverAddr,12345)) import socket s.listen(5) c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client 192.168.13.5 c.connect((‘10.255.88.76’,80) while 1: c.send(‘Hello Server!!!’) Socket object client, clientAddr = s.accept() Socket object c.shutdown(socket.SHUT_RDWR)) ‘c’ print “I just got a connection request from: “, clientAddr ‘client’ client.close() 10.255.88.76 192.168.19.5 Hello Server!!! TCP | IP NIC NIC DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 37 Sockets server import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serverAddr = s.gethostname() client s.bind((serverAddr,12345)) import socket s.listen(5) c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) c.connect((‘www.cisco.com’,80)) Socket object3 c.send(‘Hello Server!!!’) Socket object while 1: Socket object2 client 192.168.13.5 client2 10.23.11.254 client3 17.255.10.1 client, clientAddr = s.accept() Socket object c.shutdown(socket.SHUT_RDWR) ‘c’ ‘client’ ‘client’ ‘client’ print “I just got a connection request from: “, clientAddr client.close() 10.255.88.76 192.168.19.5 Hello Server!!! TCP | IP NIC NIC DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 38 El-cheapo_UDP_server.py: Sockets import socket,sys sp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sp.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) count = 0 port = 5678 dest = ('<broadcast>', port) print 'Sending message...' while True: msg = 'Hello World' sp.sendto(msg, dest) # send message out on port 5678 to address 'ffffff' DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 39 El-cheapo_UDP_client.py: Sockets import socket,time s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # spin up socket object s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) # tell it to use broadcasts s.bind(('',5678)) # 'glue' this program onto any address using port 5678 print "waiting..." data,addr = s.recvfrom(1024) print '\r', data, addr # get a message – up to to 1024 bytes - and where it came from # display the message DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 40 Thank You Q&A Complete Your Online Session Evaluation • Please complete your Online Session Evaluations after each session • Complete 4 Session Evaluations & the Overall Conference Evaluation (available from Thursday) to receive your Cisco Live T-shirt • All surveys can be completed via the Cisco Live Mobile App or the Communication Stations Don’t forget: Cisco Live sessions will be available for viewing on-demand after the event at CiscoLive.com/Online DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 43 Continue Your Education • Demos in the Cisco campus • Walk-in Self-Paced Labs • Lunch & Learn • Meet the Engineer 1:1 meetings • Related sessions DEVNET-1040 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 44 Thank You