Last active
August 29, 2015 14:11
-
-
Save coxley/c4f3a03303d7a670670c to your computer and use it in GitHub Desktop.
various helper functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
import logging | |
import socket | |
import Exscript.protocols as protocols | |
from Exscript.util.interact import read_login | |
from Exscript.util.file import get_hosts_from_file | |
from Exscript.protocols.Exception import LoginFailure, TimeoutException | |
def create_logger(log_path, filename): | |
logFormatter = logging.Formatter( | |
"%(asctime)s [%(levelname)-5.5s] %(message)s") | |
rootLogger = logging.getLogger('') | |
rootLogger.setLevel(logging.INFO) | |
fileHandler = logging.FileHandler("{0}/{1}.log".format(log_path, filename)) | |
fileHandler.setFormatter(logFormatter) | |
rootLogger.addHandler(fileHandler) | |
consoleHandler = logging.StreamHandler(sys.stdout) | |
consoleHandler.setFormatter(logFormatter) | |
rootLogger.addHandler(consoleHandler) | |
return rootLogger | |
# logger = create_logger('./', 'add_syslog.log') | |
class MyLogger(object): | |
'''Temporary logger object to get around Exscript overriding logging''' | |
def _write(self, log, file_path='log.txt'): | |
'''Private method to write to log file | |
:param log: Message to log | |
:type log: str | |
:param file_path: Path to file to log | |
:type file_path: str | |
''' | |
with open(file_path, 'a') as f: | |
f.write(log + '\n') | |
def info(self, message): | |
'''Informational log | |
:param message: Message to log | |
:type message: str | |
''' | |
curtime = time.strftime("%Y-%m-%d %H:%M:%S") | |
log = "{0} [{1}]: {2}".format(curtime, 'INFO', message) | |
print log | |
self._write(log) | |
def critical(self, message): | |
'''Critical log | |
:param message: Message to log | |
:type message: str | |
''' | |
curtime = time.strftime("%Y-%m-%d %H:%M:%S") | |
log = "{0} [{1}]: {2}".format(curtime, 'CRITICAL', message) | |
print log | |
self._write(log) | |
logger = MyLogger() | |
def connect_to_host(host, account, driver='ios'): | |
'''Connect and login to host and return connection object | |
:param host: Host in URI form | |
:type host: str | |
:param account: Account for log into devices | |
:type account: Exscript Account object | |
:param driver: Driver string. Defaults to 'ios' | |
:type driver: str | |
''' | |
try: | |
conn = protocols.connect(host) | |
except socket.error, e: | |
logger.critical('({}) - {}'.format(host.address, e)) | |
return False | |
try: | |
conn.set_driver(driver) | |
except TypeError: | |
conn.set_driver('ios') | |
try: | |
conn.login(account) | |
except LoginFailure, e: | |
logger.critical('({}) - {}'.format(host.address, e)) | |
return False | |
except TimeoutException, e: | |
logger.critical('({}) - {}'.format(host.address, e)) | |
return False | |
return conn | |
def add_syslog_host(conn, syslog_host, | |
syntax='ios', ifname='inside'): | |
'''Configure syslog host and global trap severity level. Assumes config lvl | |
:param conn: Exscript connection object | |
:param syslog_host: IP address or hostname of syslog host | |
:type syslog_host: str | |
:param syntax: Configuration syntax to use. 'ios' default. Can use 'asa'. | |
:type syntax: str | |
:param ifname: Used for ASA interface name. Optional. Will default 'inside' | |
:type ifname: str | |
''' | |
if syntax == 'ios': | |
conn.execute('logging host {}'.format(syslog_host)) | |
elif syntax == 'asa': | |
conn.execute('logging host {} {}'.format(ifname, syslog_host)) | |
else: | |
logger.error('ERROR: Incorrect syntax provided.') | |
return False | |
conn.execute('logging trap warnings') | |
logger.info('Syslog host added.') | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example for adding syslog to group of hosts: