daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Jan 25th, 2016 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. __author__ = 'KILLER'
  4.  
  5. import time
  6. import socket
  7. import telnetlib
  8. import getpass
  9. import logging
  10.  
  11. LOG_FILENAME = 'OLT_MP01.log'
  12. logging.basicConfig(filename=LOG_FILENAME,
  13.                             level=logging.DEBUG,
  14.                             format='%(asctime)s - %(levelname)s - %(message)s'
  15.                             )                            
  16.  
  17.  
  18. def telnet_host(host, user, password, commands=[], port=23, time_out=10):
  19.     try:
  20.         whitespace = '\r\n'
  21.         wait = 0.3
  22.         connect = telnetlib.Telnet(host, port, time_out)
  23.         connect.read_until('sername', timeout=time_out)
  24.         logging.debug('Entered username')
  25.         connect.write(user + whitespace)
  26.         connect.read_until('assword', timeout=time_out)
  27.         logging.debug('Entered password')
  28.         connect.write(password + whitespace)
  29.         time.sleep(wait)
  30.         connect.read_very_eager()
  31.         for command in commands:
  32.             connect.write(command + whitespace)
  33.             logging.info('Command : %r' % command)
  34.             time.sleep(wait)
  35.             response = connect.read_very_eager()
  36.             if '(y/n)' in response:
  37.                 logging.info('Command require confirmation')
  38.                 connect.write('Y' + whitespace)
  39.                 time.sleep(wait)
  40.                 logging.info('Confirmed (YES)')
  41.                 connect.read_very_eager()
  42.         connect.close()
  43.     except socket.timeout, e:
  44.         logging.error('%r %s' % (host, e))
  45.         pass
  46.     except socket.error, e:
  47.         logging.error('%r %s' % (host, e))
  48.         pass
  49.     except Exception, e:
  50.         logging.error('%r %s' % (host, e))
  51.  
  52.        
  53. if __name__ == '__main__':
  54.     hosts_file = '/home/SCRIPTS/push_prof/HNI/OLT/hosts_OLT_MP01.txt'
  55.     user = raw_input('Enter username: ')
  56.     user = user.rstrip()
  57.     password = getpass.getpass(prompt='Enter password: ')
  58.     with open(hosts_file, 'r') as f:
  59.         for host in f:
  60.             host = host.split()
  61.             hostname = host[0].strip()
  62.             ip_address = host[1].strip()
  63.             loop_back = host[2].strip()
  64.             commands = [
  65.                 'interface %s' % loop_back,
  66.                 'description ->TACACS',
  67.                 'ip address %s 255.255.255.255' % loop_back,
  68.                 'quit',
  69.                 'ospf 1',
  70.                 'area 0.0.0.2',
  71.                 'network %s 0.0.0.0' % loop_back,
  72.             ]
  73.             logging.info('Connecting to host %s' % host)
  74.             telnet_host(ip_address, user, password, commands)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand