Advertisement
Guest User

Untitled

a guest
Jul 29th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. import logging
  2. import ipaddress
  3. import subprocess
  4. import os
  5.  
  6. class mainCode():
  7.  
  8.   #Logging Params
  9.   logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', filename='output.log', level=logging.DEBUG)
  10.   logging.debug('Logging started')
  11.  
  12.   #Define Variables
  13.   loginUser = 'user'
  14.   loginPassword = "'pw'"
  15.   sendCommands = "'display ip int br; system-view; test'"
  16.   sendCommand2 = "'system-view'"
  17.   logging.info('Variables Declared..')
  18.  
  19.   def debug(self):
  20.     print('Using Username: {0}'.format(self.loginUser))
  21.     print('Using Password: {0}'.format(self.loginPassword))
  22.     print('Sending Command: {0}'.format(self.sendCommands))
  23.     print('arlogin', '-u', self.loginUser, '-p', self.loginPassword, '-c', self.sendCommands, self.currentDevice)
  24.  
  25.   #Hosts
  26.   def hosts(self):
  27.     with open ('device-list.txt') as deviceList:
  28.       for i in deviceList:
  29.         currentDevice = i.rstrip('\n')
  30.         logging.info('Currently working on {0}'.format(currentDevice))
  31.         logging.info('Checking if {0} is online..'.format(currentDevice))
  32.         check = os.system('ping -c 1 {0}'.format(currentDevice))
  33.         if check == 0:
  34.           logging.info('{0} responded successfully!'.format(currentDevice))
  35.           os.system("echo 'Device: {0} is online... proceeding to configure' >> logfile.txt".format(currentDevice))
  36.           sendSubprocess = os.system('arlogin -u {0} -p {1} -c {2} {3}'.format(self.loginUser, self.loginPassword, self.sendCommands, currentDevice))
  37.         else:
  38.           logging.info('{0} has failed to respond! Marking as offline..'.format(currentDevice))
  39.           os.system('echo {0} >> offlinedevice.txt'.format(currentDevice))
  40.           os.system("echo 'Device: {0} is offline! Ending.' >> logfile.txt".format(currentDevice))
  41.           pass
  42.  
  43.  
  44. code = mainCode()
  45. #code.debug()
  46.  
  47. logging.info('Function Hosts started..')
  48. code.hosts()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement