How to Use ANSI for Remote Screen Scraping?

Henry Chang goldspin at gmail.com
Mon Nov 3 01:00:20 CET 2008


After playing with this, it seems the key is to pass objects to the ANSI
terminal via the following:
import ANSI

crt = ANSI.ANSI(25,80)

head_output = ["line 1 ABCDEFGHIJKL", "line 2 ABCDEFGHIJKL",
               "line 3 ABCDEFGHIJKL", "line 4 ABCDEFGHIJKL",
               "line 5 ABCDEFGHIJKL", "line 6 ABCDEFGHIJKL"]

for i in head_output:
  crt.write(i + "\n")

print crt.get_region(2, 6, 5, 10)

Output from ANSI terminal region:
['2 ABC', '3 ABC', '4 ABC', '5 ABC']

On Sun, Nov 2, 2008 at 3:30 PM, Henry Chang <goldspin at gmail.com> wrote:

> I am taking a step back, and just get something simple working with ANSI.
>  The following code works (creates the ANSI terminal object, and inserts a
> few characters, and scrap the characters back).
>
> import ANSI
>
> crt = ANSI.ANSI(25,80)
>
> crt.insert_abs(2,2,"a")
> crt.insert_abs(3,10,"b")
> crt.insert_abs(5,5,"z")
>
> print crt.get_region(1, 1, 5, 10)
>
> But if I try to print something to the screen, the ANSI terminal doesn't
> capture anything.
>
> import ANSI
> import os
>
> crt = ANSI.ANSI(25,80)
>
> crt.insert_abs(2,2,"a")
> crt.insert_abs(3,10,"b")
> crt.insert_abs(5,5,"z")
>
> head_output = os.system("head -n 10 long_text_file.txt")
> print head_output
>
> print crt.get_region(1, 1, 5, 10)
>
> How can I get the head output into the ANSI terminal?  My example code is
> similar to the the usage example from the link below.  Yet ANSI is not
> scraping the screen.  What am I doing wrong?
>
>
> http://coding.derkeiler.com/Archive/Python/comp.lang.python/2005-03/1441.html
>
> Thanks for any help.
>
> On Sat, Nov 1, 2008 at 5:03 PM, Henry Chang <goldspin at gmail.com> wrote:
>
>> Using the below script, I can use pexpect to correctly output the entire
>> remote terminal content to screen.
>> At this point, I am running into difficulty to scrap the screen, for the
>> screen elements that I want.  (Say: the screen region from the complete 3rd
>> line to the 8th line.)  I believe I need to use ANSI module.  I tried a few
>> things, but have run out of ideas.
>>
>> Can anyone please help?  How can the code below be modified to use ANSI?
>>
>> Thanks so much for any help.
>>
>>
>> import pexpect
>>
>> child = pexpect.spawn ('ssh dummy_user at dummy_ip')
>> child.expect ('Password: ')
>>
>> child.sendline ('dummy_password')
>> child.expect ('[513] ')
>>
>> child.sendline ('w2ktt')  # windows 2000 terminal type
>> child.expect ('Command:')
>>
>> child.sendline ('dummy_command')
>> child.expect('non-guest room')
>>
>> # This will output the console to the screen.
>> print child.before
>>
>> child.expect('Command:')
>> child.sendline ('logoff')
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081102/5dfa6f9f/attachment.html>


More information about the Python-list mailing list