def createUserConf(): # TODO: test """Create the user-level configuration file from the defaults file If the file already exists, do nothing. Otherwise, copy the defaults file to user level. """ try: if not os.path.exists(_userFile): debug("Creating configuration file.") shutil.copyfile(_defaultsFile, _userFile) except (IOError, os.error), why: problem("Unable to create the configuration file: %s" % why, True)
def notify(text, appName, summary): "Trigger a notification with the given text." try: text = text.encode(encoding) except: text = '[<b>Error:</b> Unable to decode text]' try: appName = appName.encode(encoding) except UnicodeEncodeError: appName = app.identifier try: summary = summary.encode(encoding) except UnicodeEncodeError: summary = '' #XXX: Better fallback summary? try: session_bus = dbus.SessionBus() obj = session_bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications') interface = dbus.Interface(obj, 'org.freedesktop.Notifications') interface.Notify(appName, 0, '', summary, text, [], {}, -1) except: problem('Could not trigger notification: %s \n' % sys.exc_info()[1])
def handleMissingSetting(section, keyName): # TODO: test """Handle when a configuration setting is missing. May return the value to use, if it makes sense. """ if section == "Jabber": if keyName == "jid": problem( "There is no configured JID. Please configure a JID and " "a password\nin your configuration file: %s" % _userFile, True, ) elif keyName == "password": problem( "There is no configured jabber password. Please configure " "a JID and a password\nin your configuration file: " "%s" % _userFile, True, ) else: debug("Returning empty value for %s in section [%s]" % (keyName, section), 1)
from common import pause, problem problem(1.1) ''' 1. Create a Python script that has three variables: ip_addr1, ip_addr2, ip_addr3, all representing corresponding IP addresses. Print all of them to standard output using a singular print statement - Terminal output - Variable assignment ''' ip_addr1 = "1.1.100.41" ip_addr2 = "1.1.100.93" ip_addr3 = "1.1.100.52" print(ip_addr1, ip_addr2, ip_addr3) pause() problem(1.2) ''' 2. Prompt a user to enter an IP address from the command line. Break the IP down into individual octets and print each octet as a decimal, binary and hexadecimal. - Taking user input - String manipulation - Type conversion ''' ip = input("Please enter a valid IP Address:\n>>> ") if ip == "": ip = '1' octets = ip.split(".") for i in octets: i = int(i)
from common import pause, problem problem(3.1) ''' 1. Read the "show_vlan.txt" file into your program. Loop through the lines in this file and extract all of the VLAN_ID, VLAN_NAME combinations. From these VLAN_ID and VLAN_NAME construct a new list where each element in the list is a tuple consisting of (VLAN_ID, VLAN_NAME). Print this data structure to the screen. ''' f = open("../resources/show_vlan.txt", 'r') # print(f.read()) text = f.readlines() pairs = [] for i in range(2, len(text) ): # print( text[i] ) words = text[i].split() if len(words) >= 3: pairs.append( ( words[0], words[1] ) ) for i in pairs: print(i) f.close() pause() problem(3.2) ''' 2. Read the contents of the "show_arp.txt" file. Using a for loop, iterate over the lines of this file. Process the lines of the file and separate out the ip_addr and mac_addr for each entry into a separate variable.
from common import pause, problem problem(2.1) ''' 1. Open the "show_version.txt" file for reading. Use the .read() method to read in the entire file contents to a variable. Print out the file contents to the screen. Also print out the type of the variable (you should have a string at this point). Close the file. Open the file a second time using a Python context manager (with statement). Read in the file contents using the .readlines() method. Print out the file contents to the screen. Also print out the type of the variable (you should have a list at this point). - File handling and reading ''' # Entire file as a string f = open("../resources/show_version.txt") fulltext = f.read() print(fulltext) print("\nStored as a {}\n".format(type(fulltext))) f.close() pause() # Breaking file down line by line f = open("../resources/show_version.txt") fulltext = f.readlines() for i in fulltext: print(i) print("\nStored as a {}\n".format(type(fulltext))) f.close() pause()
from common import pause, problem problem(4.1) ''' 1. Create a dictionary representing a network device. The dictionary should have key-value pairs representing the 'ip_addr', 'vendor', 'username', and 'password' fields. Print out the 'ip_addr' key from the dictionary. If the 'vendor' key is 'cisco', then set the 'platform' to 'ios'. If the 'vendor' key is 'juniper', then set the 'platform' to 'junos'. Create a second dictionary named 'bgp_fields'. The 'bgp_fields' dictionary should have a keys for 'bgp_as' 'peer_as', and 'peer_ip'. Using the .update() method add all of the 'bgp_fields' dictionary key-value pairs to the network device dictionary. Using a for-loop, iterate over the dictionary and print out all of the dictionary keys. Using a single for-loop, iterate over the dictionary and print out all of the dictionary keys and values. ''' device = { 'ip_addr': '10.32.55.66', 'vendor': 'juniper', 'username': '******', 'password': '******' } print(device['ip_addr']) if device['vendor'] == 'cisco': device['platform'] = 'ios'
from common import pause, problem problem(5.1) ''' 1a. Create an ssh_conn function. This function should have three parameters: ip_addr, username, and password. The function should print out each of these three variables and clearly indicate which variable it is printing out. Call this ssh_conn function using entirely positional arguments. Call this ssh_conn function using entirely named arguments. Call this ssh_conn function using a mix of positional and named arguments. 1b. Expand on the ssh_conn function from exercise1 except add a fourth parameter 'device_type' with a default value of 'cisco_ios'. Print all four of the function variables out as part of the function's execution. Call the 'ssh_conn2' function both with and without specifying the device_type Create a dictionary that maps to the function's parameters. Call this ssh_conn2 function using the **kwargs technique. ''' def ssh_conn(ip_addr, username, password, device_type="cisco_ios"): print('IP address: ', ip_addr) print('Username: '******'Password: '******'Device Type: ', device_type) print()