Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- __author__ = 'KILLER'
- import time
- import socket
- import telnetlib
- import getpass
- import logging
- LOG_FILENAME = 'OLT_MP01.log'
- logging.basicConfig(filename=LOG_FILENAME,
- level=logging.DEBUG,
- format='%(asctime)s - %(levelname)s - %(message)s'
- )
- def telnet_host(host, user, password, commands=[], port=23, time_out=10):
- try:
- whitespace = '\r\n'
- wait = 0.3
- connect = telnetlib.Telnet(host, port, time_out)
- connect.read_until('sername', timeout=time_out)
- logging.debug('Entered username')
- connect.write(user + whitespace)
- connect.read_until('assword', timeout=time_out)
- logging.debug('Entered password')
- connect.write(password + whitespace)
- time.sleep(wait)
- connect.read_very_eager()
- for command in commands:
- connect.write(command + whitespace)
- logging.info('Command : %r' % command)
- time.sleep(wait)
- response = connect.read_very_eager()
- if '(y/n)' in response:
- logging.info('Command require confirmation')
- connect.write('Y' + whitespace)
- time.sleep(wait)
- logging.info('Confirmed (YES)')
- connect.read_very_eager()
- connect.close()
- except socket.timeout, e:
- logging.error('%r %s' % (host, e))
- pass
- except socket.error, e:
- logging.error('%r %s' % (host, e))
- pass
- except Exception, e:
- logging.error('%r %s' % (host, e))
- if __name__ == '__main__':
- hosts_file = '/home/SCRIPTS/push_prof/HNI/OLT/hosts_OLT_MP01.txt'
- user = raw_input('Enter username: ')
- user = user.rstrip()
- password = getpass.getpass(prompt='Enter password: ')
- with open(hosts_file, 'r') as f:
- for host in f:
- host = host.split()
- hostname = host[0].strip()
- ip_address = host[1].strip()
- loop_back = host[2].strip()
- commands = [
- 'interface %s' % loop_back,
- 'description ->TACACS',
- 'ip address %s 255.255.255.255' % loop_back,
- 'quit',
- 'ospf 1',
- 'area 0.0.0.2',
- 'network %s 0.0.0.0' % loop_back,
- ]
- logging.info('Connecting to host %s' % host)
- telnet_host(ip_address, user, password, commands)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement