1

I want to collect data and parse it eventually from an open window in linux.

An example- Suppose a terminal window is open. I need to retrieve all the data that appears on that window. After retrieval, I would parse it to get specific commands entered.

So is it possible to do that? If so, how? I would prefer to use python to code this entire thing.

I am making a guess that first I would have to get some sort of ID for the open window and then use some kind of library to get the content from the window whose ID I have got.

Please help. I am quite a newbie.

5lackp1x3l0x17
  • 349
  • 2
  • 7
  • 14

3 Answers3

6

You can (ab)use the assistive technologies support (for screen readers and such) that exist in the toolkit libraries. Whether it will work is toolkit specific—Gtk and Qt have this support, but others (like Tk, Fltk, etc.) may or may not.

The Linux Desktop Testing Project is a python toolkit for abusing these interfaces for testing GUI applications, so you can either use it or look how it works and do similar thing.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
4

I think the correct answer may be "with some difficulty". Essentially, the contents of a window is a bitmap. This bitmap is drawn on by a whole slew of primitives (including "display this octet-string, using that encoding and a specific font"), but the window contents is still "just pixels".

Getting the "just pixels" is pretty straight-forward, as these things go. You open a session to the X server and say "given me the contents of window W" and it hands it over.

Doing something useful with it is, unfortunately, a completely different matter, as you'd potentially have to (essentially) OCR the bitmap for what you want.

If you decide to take that route, have a look at the source of xwd, as that does, essentially, that.

ThomasH
  • 22,276
  • 13
  • 61
  • 62
Vatine
  • 20,782
  • 4
  • 54
  • 70
1

Do you have some sort of control over the execution of the terminal? In that case, you can use the script command in the terminal session to log all interaction to a file and then read and parse the file.

$ script myfile
Script started, file is myfile
$ ls
...
$ exit
Script done, file is myfile
$ parse_file.py myfile

If the terminal is running inside of screen, you have other options as well. Screen has logging built in, screen -X sends commands to a running screen session (man screen).

codeape
  • 97,830
  • 24
  • 159
  • 188
  • The terminal was just an example. My real purpose is to read the contents of another window (e.g. gedit, webpage etc.) which is open. And yes, I have control over the entire terminal. – 5lackp1x3l0x17 May 11 '11 at 07:19