Skip to content

Instantly share code, notes, and snippets.

@JulienPalard
Last active June 12, 2022 19:10
Show Gist options
  • Save JulienPalard/18b4f6ecfede85644f1998c527a38269 to your computer and use it in GitHub Desktop.
Save JulienPalard/18b4f6ecfede85644f1998c527a38269 to your computer and use it in GitHub Desktop.
Backup UBNT edgeswitch over SSH
"""Usage: ./backup.py login@host
"""
import sys
import pexpect
import getpass
child = pexpect.spawn('ssh ' + sys.argv[1])
while True:
match = child.expect(['password:', '\(.*\) >'])
if match == 0:
password = getpass.getpass('Password: ')
child.sendline(password)
else:
break
child.sendline('enable')
child.expect('Password:')
child.sendline(password)
child.expect('#')
child.sendline('show startup-config')
while True:
match = child.expect(['^[^\r\n]*\r?\n',
'^--More-- or \(q\)uit',
'\r \r',
'^\(.*\) #'])
line = child.match.group(0).decode('utf-8').strip()
if match == 0:
print(line)
elif match == 1:
child.send(' ')
elif match == 2:
pass
else:
break
@jzamanski
Copy link

Thanks for this. Havent tried it, but I'm thinking/hoping you/I can avoid dealing with the pagination by sending a "terminal length 0" after login.

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