forked from trigger/trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgong
executable file
·80 lines (59 loc) · 2.25 KB
/
gong
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
gong (go ng) - Command-line client to log in to network devices using TACACS credentials.
An optional .gorc file may be used to specify user preferences.
Partially adapted from conch. See twisted.conch.scripts.conch and
http://twistedmatrix.com/projects/conch/documentation/howto/conch_client.html
"""
__author__ = 'Jathan McCollum, Eileen Tschetter, Michael Shields'
__maintainer__ = 'Jathan McCollum'
__email__ = 'jathan.mccollum@teamaol.com'
__copyright__ = 'Copyright 2006-2012, AOL Inc.'
__version__ = '2.0'
from optparse import OptionParser
import os
import sys
from twisted.python import log
from trigger.netdevices import device_match
# Put this here until the default changes to not load ACLs from redis.
from trigger.conf import settings
settings.WITH_ACLS = False
def parse_args(argv):
parser = OptionParser(usage='%prog [options] [device]', description='''\
Automatically log into network devices using cached TACACS credentials.
''')
parser.add_option('-o', '--oob', action='store_true',
help='Connect to device out of band first.')
opts, args = parser.parse_args(argv)
if len(args) != 2:
parser.print_help()
sys.exit(2)
return opts, args
def connect_to_oob(dev):
"""Lookup out-of-band info and try to connect to the console using telnet."""
tn = "telnet %s %s" % (dev.OOBTerminalServerFQDN, dev.OOBTerminalServerTCPPort)
print "OOB Information for %s:" % dev.nodeName
print tn
print 'Connecting you now...'
os.system(tn)
def main():
global opts
opts, args = parse_args(sys.argv)
if os.getenv('DEBUG') is not None:
log.startLogging(sys.stdout, setStdout=False)
# Exception handling is done in device_match, returns None if no match.
dev = device_match(args[1].lower(), production_only=False)
if dev is None:
return 2
if dev.adminStatus != 'PRODUCTION':
print 'WARNING: You are connecting to a non-production device.'
if opts.oob:
connect_to_oob(dev)
return 0
# Connect to the device... and make sure to capture its exit code
retval = dev.connect()
print '\n' # Return cursor to beginning of line
return retval
if __name__ == '__main__':
sys.exit(main())