How can I screen-scrape output from telnet in Perl?

后端 未结 6 359
死守一世寂寞
死守一世寂寞 2021-01-18 00:13

I can setup a telnet connection in Perl no problems, and have just discovered Curses, and am wondering if I can use the two together to scrape the output from the telnet ses

6条回答
  •  粉色の甜心
    2021-01-18 00:25

    I would vote also for the Expect answer. I had to do something similar from a gui'ish application. The trick (albeit tedious) to get around the control characters was to strip all the misc characters from the returned strings. It kind of depends on how messy the screen scrape ends up being.

    Here is my function from that script as an example:

    # Trim out the curses crap
    sub trim {
        my @out = @_;
        for (@out) {
            s/\x1b7//g;
            s/\x1b8//g;
            s/\x1b//g;   # remove escapes
            s/\W\w\W//g;
            s/\[\d\d\;\d\dH//g;  # 
            s/\[\?25h//g;
            s/\[\?25l//g;
            s/\[\dm//g;
            s/qq//g;
            s/Recall//g;
            s/\357//g;
            s/[^0-9:a-zA-Z-\s,\"]/ /g;
            s/\s+/ /g;    # Extra spaces
    
        }
        return wantarray ? @out : $out[0];
    }
    

提交回复
热议问题