Skip to content
Snippets Groups Projects
Commit c7739405 authored by David Byers's avatar David Byers
Browse files

Initial commit of public 'release'

parent 6627070e
No related branches found
No related tags found
No related merge requests found
*~
\#*
.#*
aosrancid 0 → 100755
#!/usr/bin/python
#
# Copyright (c) 2019 Linköping university
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import logging
import os
import re
import sys
import time
import paramiko
def run_command(client, command):
if not command.endswith('\n'):
command = command + '\n'
client.send(command)
output = []
errors = []
empty_count = 0
while empty_count < 30:
if client.recv_ready():
output.append(client.recv(8192))
empty_count = 0
continue
if client.recv_stderr_ready():
errors.append(client.recv(8192))
empty_count = 0
continue
time.sleep(0.1)
empty_count += 1
if errors:
sys.stderr.write(''.join(errors))
sys.exit(1)
return ''.join(output).splitlines()
def _is_one_of(line, keywords):
if ':' not in line:
return True
temp = line.strip().split(':')[0]
if temp in keywords:
return True
return False
parser = argparse.ArgumentParser(description="Get configuration from AOS switch")
parser.add_argument('-d', action='store_true', dest='debug', default=False, help="enable debugging")
parser.add_argument('-l', metavar='FILE', dest='log', help="log file")
parser.add_argument('-V', action='store_true', dest='show_version', default=False, help="show version")
parser.add_argument('-F', dest='cloginrc', help='alternate .cloginrc file', default=None)
parser.add_argument('host', metavar='HOST', help="host to get config from")
opts = parser.parse_args()
password = None
user = None
if opts.show_version:
print "aosrancid 0.1"
sys.exit(0)
if opts.debug:
logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger('paramiko').setLevel(logging.DEBUG)
logging.getLogger('paramiko').addHandler(logging.StreamHandler(sys.stderr))
try:
cloginrc = opts.cloginrc or os.path.expanduser('~/.cloginrc')
with open(cloginrc, 'r') as fp:
for line in fp:
m = re.match('add password \* {(?P<pass>[^}]*)}', line, re.I)
if m:
password = m.group('pass')
m = re.match('add user \* {(?P<user>[^}]*)}', line, re.I)
if m:
user = m.group('user')
except IOError as e:
logging.error('unable to open .cloginrc: %s', str(e))
sys.exit(1)
if password is None:
logging.error('unable to find line matching "add password * {...}" in .cloginrc')
sys.exit(1)
if user is None:
logging.error('unable to find line matching "add user * {...}" in .cloginrc')
sys.exit(1)
t = paramiko.Transport((opts.host, 22))
t.connect(None, user, password)
client = t.open_session()
try:
client.get_pty()
client.invoke_shell()
lines = run_command(client, 'show system')
pos = 0
for line in lines:
if line.startswith('System:'):
break
pos += 1
system = [line for line in lines[pos:-2] if _is_one_of(line, ('System',
'Description',
'Contact',
'Name',
'Location'))]
lines = run_command(client, 'show chassis')
pos = 0
for line in lines:
if line.startswith('Chassis'):
break
pos += 1
chassis = lines[pos:-2]
users = []
lines = run_command(client, 'show user')
for line in lines:
match = re.match(r"user name = (?P<user>[^,]*),", line, re.I)
if match and not match.group('user') == 'default (*)':
users.append(match.group('user'))
finally:
client.close()
t.close()
t = paramiko.Transport((opts.host, 22))
t.connect(None, user, password)
try:
sftp = paramiko.SFTPClient.from_transport(t)
fp = sftp.open('working/boot.cfg', 'r')
config = fp.read()
finally:
t.close()
fp.close()
with open(opts.host + ".new", "w") as fp:
fp.write('!RANCID-CONTENT-TYPE: alcatel\n')
fp.write('!\n')
for line in system:
fp.write('! %s\n' % line)
for line in chassis:
fp.write('! %s\n' % line)
for user in users:
fp.write('! user %s\n' % user)
fp.write(config)
sys.exit(0)
#!/usr/bin/python
#
# Copyright (c) 2019 Linköping university
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import logging
import os
import re
import sys
import time
import paramiko
def run_command(client, command):
if not command.endswith('\n'):
command = command + '\n'
client.send(command)
output = []
errors = []
empty_count = 0
while empty_count < 30:
if client.recv_ready():
output.append(client.recv(8192))
empty_count = 0
continue
if client.recv_stderr_ready():
errors.append(client.recv(8192))
empty_count = 0
continue
time.sleep(0.1)
empty_count += 1
if errors:
sys.stderr.write(''.join(errors))
sys.exit(1)
return ''.join(output).splitlines()
parser = argparse.ArgumentParser(description="Get configuration from AOS8 switch")
parser.add_argument('-d', action='store_true', dest='debug', default=False, help="enable debugging")
parser.add_argument('-l', metavar='FILE', dest='log', help="log file")
parser.add_argument('-V', action='store_true', dest='show_version', default=False, help="show version")
parser.add_argument('-F', dest='cloginrc', help='alternate .cloginrc file', default=None)
parser.add_argument('host', metavar='HOST', help="host to get config from")
opts = parser.parse_args()
password = None
user = None
if opts.show_version:
print "omniswitch8 0.1"
sys.exit(0)
if opts.debug:
logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger('paramiko').setLevel(logging.DEBUG)
logging.getLogger('paramiko').addHandler(logging.StreamHandler(sys.stderr))
try:
cloginrc = opts.cloginrc or os.path.expanduser('~/.cloginrc')
with open(cloginrc, 'r') as fp:
for line in fp:
m = re.match('add password \* {(?P<pass>[^}]*)}', line, re.I)
if m:
password = m.group('pass')
m = re.match('add user \* {(?P<user>[^}]*)}', line, re.I)
if m:
user = m.group('user')
except IOError as e:
logging.error('unable to open .cloginrc: %s', str(e))
sys.exit(1)
if password is None:
logging.error('unable to find line matching "add password * {...}" in .cloginrc')
sys.exit(1)
if user is None:
logging.error('unable to find line matching "add user * {...}" in .cloginrc')
sys.exit(1)
t = paramiko.Transport((opts.host, 22))
t.connect(None, user, password)
client = t.open_session()
try:
client.get_pty()
client.invoke_shell()
lines = run_command(client, 'show chassis')
pos = 0
for line in lines:
# Capture config from first occurance of Chassis definition
if line.startswith(('Chassis', 'Local', 'Remote')):
break
pos += 1
chassis = lines[pos:-2]
# Extract Virtual chassis ID from Chassis output
virtualchassis = []
for line in chassis:
match = re.match(r".*Chassis ID (?P<chassisid>[0-9]) \(", line, re.I)
if match:
virtualchassis.append(match.group('chassisid'))
# Extract VCM configuration
vcm = []
for chassisid in virtualchassis:
lines = run_command(client, 'show configuration vcm-snapshot chassis-id ' + chassisid)
vcm.append(lines[1:-1])
users = []
lines = run_command(client, 'show user')
for line in lines:
match = re.match(r"user name = (?P<user>[^,]*),", line, re.I)
if match and not match.group('user') == 'default (*)':
users.append(match.group('user'))
# Get running-directory
lines = run_command(client, 'show running-directory')
confdir = 'working'
for line in lines:
match = re.match(r"^\s+Running\s+configuration\s+:\s+(?P<rundir>.*),", line, re.I)
if match and not match.group('rundir') == 'WORKING':
confdir = match.group('rundir')
finally:
client.close()
t.close()
t = paramiko.Transport((opts.host, 22))
t.connect(None, user, password)
try:
sftp = paramiko.SFTPClient.from_transport(t)
conffile = confdir + '/vcboot.cfg'
fp = sftp.open(conffile, 'r')
config = fp.read()
finally:
t.close()
fp.close()
with open(opts.host + ".new", "w") as fp:
fp.write('!RANCID-CONTENT-TYPE: aos-8\n')
fp.write('!\n')
for line in chassis:
fp.write('! %s\n' % line)
for user in users:
fp.write('! user %s\n' % user)
fp.write('!\n')
for vc in vcm:
for line in vc:
fp.write('%s\n' % line)
fp.write(config)
sys.exit(0)
alcatel;script;/usr/local/bin/aosrancid
omniswitch8;script;/usr/local/bin/omniswitch8
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment