Skip to content

Instantly share code, notes, and snippets.

@Napat
Created October 7, 2015 06:44
Show Gist options
  • Save Napat/bc00e9fc4242dd991939 to your computer and use it in GitHub Desktop.
Save Napat/bc00e9fc4242dd991939 to your computer and use it in GitHub Desktop.
python messy try except block
def td_apply_cmd(credential):
global err_count
ms = 0
try:
(ip, username, password, cmd_file, ms_str) = re.findall(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(.+):(.+):(.+):(.+)", credential)[0]
ms = int(ms_str)
except IndexError:
try:
(ip, username, password, cmd_file) = re.findall(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(.+):(.+):(.+)", credential)[0]
cmd_file = cmd_file.rstrip(":")
ms = 500
except IndexError: #from re.findall()
print "Bad credential file"
print "\t expect newline or bad credential pattern: \'%s\'" % credential
err_count = err_count+1
except ValueError: # from int()
print "Bad credential file type ValueError"
print "\t Bad credential pattern: \'%s\'" % credential
err_count = err_count+1
tn_connect = telnet_connect(ip, username, password)
if tn_connect == None:
err_count = err_count+1
return
print "\n* Sending command(s) to device: %s\n" % (ip)
apply_cmd(tn_connect, cmd_file, fname_out=("%s.log"%ip), waitoutput_ms=int(ms))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment