1

I'm in a pickle with writing a script that can SSH into device, run a command and parse that data out to a file. I've written this using Pyparsing and Exscript then I found out that the device I'm going to be using this on is using Python 2.4.4 and Debian 4.1.1 so the modules will not work on this. Now I am back to the drawing board trying to find out how to do this with NO modules. Anyone have any reference or point me in the right direction for this? Thank you in advance.

Here is my code:

from Exscript.util.interact import read_login
from Exscript.protocols import SSH2
import uuid
from pyparsing import *
import re
import yaml

account = read_login()              
conn = SSH2()                       
conn.connect('172.0.0.1')     
conn.login(account)           

conn.execute('foobar')
data = conn.response
conn.send('exit\r')               
conn.close()

###### PARSER ######

date_regex = re.compile(r'\d\d-\d\d-\d\d')
time_regex = re.compile(r'\d\d:\d\d:\d\d')
pairs = [{'category': 'General Information',
          'kv': Group(Word(alphanums) + Word(alphanums))},
         {'category': 'Last Reset:',
          'kv': Group(Word(alphas, max=1) + Word(alphas)) + Literal(':').suppress()
                + Group(Regex(date_regex) + Regex(time_regex)
                + Optional(SkipTo(LineEnd())))
          }
         ]
# build list of categories with associated parsing rules
categories = [Word("# ").suppress() + x['category']
               + OneOrMore(Group(x['kv']))
              for x in pairs]
# account for thing you don't have specific rules for
categories.append(Word("#").suppress() + Optional(SkipTo(LineEnd())) +
                  Group(OneOrMore(Combine(Word(alphanums) + SkipTo(LineEnd()))))
                  )
# OR all the categories together
categories_ored = categories[0]
for c in categories[1:]:
    categories_ored |= c
configDef = OneOrMore(categories_ored)
suppress_tokens = ["show all", "SSH>", "Active System Configuration"]
suppresses = [Literal(x).suppress() for x in suppress_tokens]
for s in suppresses:
    configDef.ignore(s)

result = configDef.parseString(data)
for e in result:
    print(e)
with open('/Users/MyMac/development/data.yml', 'w') as outfile:
        outfile.write( yaml.dump(e))

UPDATE

I have followed the advice below and now have Pexpect installed and found a older version of Python-Pyparsing that I have also installed. So I'm on my way again to getting my scripts to work with modules. Thanks!

5
  • How about using subprocess to talk to a system ssh utility? pexpect looks like an early pure Python module that only needs pty. Commented Jan 17, 2014 at 21:19
  • lag.net/paramiko/legacy.html paramiko is another early pure Python SSH library. Commented Jan 17, 2014 at 21:29
  • @hpaulj I was looking into using subprocess, but having issues with that connecting and running a command. I looked at paramiko before I wrote the script above but had some issues, which is why I went with Exscript. I'll look at the early releases of paramiko and see if that may work. The other thing is the debian is so old and won't let me install modules very easily. Commented Jan 18, 2014 at 16:22
  • 1
    pexpect-2.4 is compact enough (I think) to 'install' by just adding one or two files to your own directory. It doesn't have to be in a system directory. paramiko might need to be in its own directory without yours. Commented Jan 18, 2014 at 22:26
  • @hpaulj thanks! I added pexpect and have the ability to ssh into the device, now hopefully I can find a solution with pyparsing or another module. Commented Jan 20, 2014 at 15:29

1 Answer 1

1

Looks like this is already solved, but...

As long as your SSH is configured for this host (or the host doesn't require you to log-in), you should be able to do the following.

import os

""" This will execute foobar on the remote host 
and store the command output to a text file
on your machine.""" 
os.system("ssh 172.0.0.1 foobar > ~/data.txt")

""" Commence processing """
data = open("data.txt", mode='r')
# and so on and so on

You can also use the subprocess library, but os.system for these types of tasks is the simplest IMO.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.