Skip to content

Instantly share code, notes, and snippets.

@xezpeleta
Last active September 28, 2016 14:59
Show Gist options
  • Save xezpeleta/7889723 to your computer and use it in GitHub Desktop.
Save xezpeleta/7889723 to your computer and use it in GitHub Desktop.
Copy 3COM switch configuration file to a TFTP server. It uses Telnet connection (usually enabled by default).
#! /usr/bin/python
'''
This script backups your 3COM
switch configuration data. It
needs a TFTP service running!
Usage: saveconf_3C3800 <host>
It works with:
- 3COM 3870 (3CR17451-91)
- 3COM SuperStack 3824 (?)
- 3COM 4400PWR (3C17205)
'''
import sys
import telnetlib
### EDIT ###
username = "admin"
password = "mypassword"
tftp = '192.168.1.14'
#############
telnet_timeout = 1
def connect(ipaddress, username, password):
try:
tn = telnetlib.Telnet(ipaddress)
try:
#print "Authentication..."
tn.read_until("Login:", telnet_timeout)
tn.write(username + "\r")
tn.read_until("Password:", telnet_timeout)
tn.write(password + "\r")
tn.read_until("Select menu option:", telnet_timeout)
#print "OK!"
return tn
except Exception, e:
print "Incorrect user/password"
print "\nException: %s" % str(e)
tn.close()
except Exception, e:
print "Error connecting to " + ipaddress
print "\nException: %s" % str(e)
def disconnect(tn):
try:
tn.write("logout\r\n")
tn.close()
except Exception, e:
print "Error: connection problem or model not supported"
print "\nException: %s" % str(e)
def summary(tn):
prompt = "Select menu option:"
try:
tn.write("system summary\r\n")
tn.write("\r\n") #some models need it :S
output = tn.read_until(prompt, telnet_timeout)
except Exception, e:
print "Error connection problem or model not supported"
print "\nException: %s" % str(e)
data = {}
for line in output.splitlines():
if ':' in line:
key, value = line.partition(':')[::2]
key = key.strip()
value = value.strip()
if value is not '':
data[key] = value
tn.read_until(prompt, telnet_timeout)
return data
def backup(tn, tftp, filename):
print "TFTP: " + tftp
print "File: " + filename
prompt = "Select menu option"
print "Backing up..."
try:
tn.write("system backupConfig save %s %s\r\n" % (tftp, filename))
tn.write("\r\n")
output = tn.read_until(prompt, 120)
except Exception, e:
print "Error: connection problem or model not supported"
print "\nException: %s" % str(e)
if "bytes transferred" in output:
return True
elif "Save of system configuration Successful" in output:
return True
else:
print "Error #5: can't connect to TFTP server"
return False
if __name__ == "__main__":
if len(sys.argv) > 1:
ipaddress = sys.argv[1]
else:
print "Usage: %s <host>" % sys.argv[0]
sys.exit(1)
tn = connect(ipaddress, username, password)
if not tn:
sys.exit(1)
s = summary(tn)
if s['Product Number'] != '3C17205' and s['Product Number'] != '3CR17451-91':
print "Warning: Model not tested: %s" % s['Product Number']
print "Continuing..."
#sys.exit(1)
filename = s['System Name'] + '.cfg'
if backup(tn, tftp, filename):
print "Backup created succesfully!"
disconnect(tn)
sys.exit(0)
else:
print "Error: Backup failed!"
disconnect(tn)
sys.exit(1)
#! /usr/bin/python
'''
This script backups your 3COM
switch configuration data. It
needs a TFTP service running!
Usage: saveconf_3C4200G.py <host>
It works with:
- 3COM 4200G (PN: 3CR17662-91)
'''
import sys
import re
import telnetlib
### EDIT ###
username = "admin"
password = "mypassword"
tftp = '192.168.1.14'
#############
telnet_timeout = 1
def connect(ipaddress, username, password):
try:
tn = telnetlib.Telnet(ipaddress)
name = None
#print "Connecting..."
#print "OK!"
try:
#print "Authentication..."
tn.read_until("Username:", telnet_timeout)
tn.write(username + "\r")
tn.read_until("Password:", telnet_timeout)
tn.write(password + "\r")
tn.write("\r\n")
output=tn.read_until(">", telnet_timeout)
tn.write("undo terminal logging\r\n")
#print "OK!"
return tn
except Exception, e:
print "ERROR: Incorrect user/password"
print "Exception: %s" % str(e)
tn.close()
except Exception, e:
print "Error connecting to " + ipaddress
print "Exception: %s" % str(e)
def disconnect(tn):
try:
tn.write("\r\n")
tn.write("quit\r\n")
except Exception, e:
print "Error: connection error or model not supported"
print "Exception: %s" % str(e)
return True
def getName(tn):
name = None
try:
#tn.write("\r\n")
output=tn.read_until(">", telnet_timeout)
except Exception, e:
print "Error: connection error or model not supported"
print "Exception: %s" % str(e)
r = re.compile('<(.*?)>')
m = r.search(output)
if m:
name = m.group(1)
return name
def getConfigFile(tn):
prompt = "<%s>" % getName(tn)
filename = None
try:
tn.write("display startup\r\n")
output = tn.read_until(prompt, telnet_timeout)
except Exception, e:
print "Error: connection error or model not supported"
print "Exception: %s" % str(e)
for line in output.splitlines():
if "Current Startup saved-configuration file" in line:
#print line
return line.split()[-1]
def backup(tn, tftp, filename):
print "TFTP: " + tftp
print "File: " + filename
configFile = getConfigFile(tn)
print "Config: " + configFile
print "Backing up..."
try:
tn.write("tftp %s put %s %s\r\n" % (tftp, configFile, filename))
output = tn.read_until("fully", 120)
except Exception, e:
print "Error: connection error or model not supported"
print "Exception: %s" % str(e)
if "File uploaded successfully" in output:
return True
else:
print "Error: can't connect to TFTP server"
return False
if __name__ == "__main__":
if len(sys.argv) > 1:
ipaddress = sys.argv[1]
else:
print "Usage: %s <host>" % sys.argv[0]
sys.exit(1)
tn = connect(ipaddress, username, password)
if not tn:
sys.exit(1)
name = getName(tn)
filename = name + '.cfg'
if backup(tn, tftp, filename):
print "Backup created succesfully!"
disconnect(tn)
sys.exit(0)
else:
print "Error: backup failed"
disconnect(tn)
sys.exit(1)
@csanchez8
Copy link

Funciona con el 3COM SuperStack 3824. Gracias Xabi ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment