1

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.

Kimyeti
  • 11
  • 2
  • What is the question? What do you expect from an anwer? – Siemkowski Nov 18 '19 at 12:13
  • Can you show the returned ascii hex codes? Assuming that all the configs are already received nicely, it could be that the ascii hex codes could be some terminator and/or message communication of the ssh communication since you are receiving 9999999 bytes? – Jason Chia Nov 18 '19 at 12:24
  • @Siemkowski I would like to remove the ascii hex codes from the output. I will copy them again for you: [?25h<0x1b> [24;27H<0x1b> [2J<0x1b> [?7L<0x1b> – Kimyeti Nov 18 '19 at 13:19
  • @JasonChia These are the ascii hex codes: [?25h<0x1b> [24;27H<0x1b> [2J<0x1b> [?7L<0x1b> – Kimyeti Nov 18 '19 at 13:19
  • I think you can find the solution in what Martin posted. Definitely looks like an escape thingy. Although there probably should be some documentation on what those hex codes do/mean but I couldn't find them. In your current setup, if these are hex "junk" are constant, it might be an idea to remove them in post. Otherwise you should explore the solution from Martin – Jason Chia Nov 18 '19 at 13:33
  • @MartinPrikryl Thanks for the link- I'll check it out – Kimyeti Nov 18 '19 at 13:35
  • @JasonChia Yes from my point of view the hex codes are junk. I'll look into Martin's answer. Thanks for the help nonetheless – Kimyeti Nov 18 '19 at 13:36

0 Answers0