Last active
June 12, 2022 19:10
-
-
Save JulienPalard/18b4f6ecfede85644f1998c527a38269 to your computer and use it in GitHub Desktop.
Backup UBNT edgeswitch over SSH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.