I'm trying to automate my workflow with a python SSH script. The purpose is to take backup of the configuration files and save it locally on my computer.
The script works in the sense that it can connect and save a copy of the configuration file, but it also returns several lines with weird ascii hex codes. I've tried to modify it with different decoding and using other commands in the script but it does not work unfortunately.
The script:
import sys
import time
import paramiko
import os
import cmd
import datetime
now = datetime.datetime.now()
user = "alan"
password = "rickman"
port=22
f0 = open('output-only-1-line.txt')
for ip in f0.readlines():
ip = ip.strip()
filename_prefix ='C:/Users/allan-rickman/Nextcloud/Aruba_Backup_Scripts/configbackups' + ip
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port, user, password, look_for_keys=False)
chan = ssh.invoke_shell()
time.sleep(5)
chan.send('\x0d')
time.sleep(2)
chan.send('term length 70\x0d')
time.sleep(2)
chan.send('sh run\x0d')
time.sleep(10)
chan.send('\x20')
time.sleep(5)
output = chan.recv(999999)
filename = "backup.cfg"
f1 = open(filename, 'a')
f1.write(output.decode("utf-8"))
f1.close()
ssh.close()
This is the output the script returns with the ascii hex codes:
[?25h<0x1b> [24;27H<0x1b> [2J<0x1b> [?7L<0x1b>
The above hex codes comes every time the script has to send a command. Thanks for the help.