Question

I am writing a script to ssh into a device with Paramiko, run a command and parse the output from that command. I have played with a few parsers and it seems to get hung up. Here is the code for me to ssh in:

SSH

import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
client.connect('127.0.0.1', username='foobar', password='foobar1')

stdin, stdout, stderr = client.exec_command('foo')
print stdout.read()

client.close()

SAMPLE OUTPUT FROM RUNNING COMMAND FOO

   C Processor:12-13-13 09:53:28 -- A Processor Initiated Reset
# Active Alarms:
   12-13-13 09:53:40 -- App Select Required
# Device Settings - Port 1
   MAC Address 00:00:00:00:0A:DF
   IP Address 127.0.0.1
   SubnetMask 155.155.155.0
   AAAA Server Enabled
   AAAA Server IP Pool Start 127.0.0.1
   AAAA Server IP Pool End 127.0.0.1
   AAAA Server Default Gateway 0.0.0.0
# Device Settings - Port 2
   MAC Address 00:00:00:00:0A:E0
   IP Address 127.0.0.1
   SubnetMask 155.155.155.0
   AAAA Server Enabled
   AAAA Server IP Pool Start 127.0.0.1
   AAAA Server IP Pool End 127.0.0.1
   AAAA Server Default Gateway 0.0.0.0
   Default Gateway 0.0.0.0
# Device Settings - Routing Table
   Route #1 - Disabled
   Route #2 - Disabled
   Route #3 - Disabled
   Route #4 - Disabled
   Route #5 - Disabled
   Route #6 - Disabled
   Route #7 - Disabled
   Route #8 - Disabled
   .....(there is another 50 lines of code same format)

If someone can point me to a post or site with some input on how I can parse this to a text file, this would be great. Thanks in advance!

Was it helpful?

Solution

This should write to file

data = stdout.read()

f = open('file.txt', 'w')
f.write(data)

f.close()
client.close()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top