views:

228

answers:

3

The nmap tool has such a feature - when you're performing a scan [#nmap -A -T4 localhost] and press "Enter" - it displays kind of status information "Timing: About 6.17% done"

Question - how can I force this keypress to happen repeatedly without touching a keyboard in bourne shell?

ps: just trying to find a work-around for a bug in php's proc_open function, when stdout of a process is returned only after closing stdout pipe, and php's pty emulation doesn't work on fbsd.

Question closed. Problem solved with the "expect" utility

#!/usr/local/bin/expect

spawn /usr/local/bin/nmap -A -T4 -p 21-100 localhost
expect arting {sleep 3; send \r}
while {1} {
        expect eof {
            send_user "finished\n";
            exit;
        } "done;" {
            sleep 3;
            send \r;
            continue;
        }

}
+3  A: 

Probably easiest to use expect.

Douglas Leeder
A: 

Maybe the ultimate 'yes man' program will do what you need - the program is called 'yes' and repeatedly generates the same input line over and over.

yes ok | recalcitrant.php 

This will send 'ok' plus newline to the recalcitrant PHP frequently. It is rate-limited by the speed at which the receiving program reads its inputs. It is available in the GNU utilities, and on most other Unix-based platforms.

If you need any intelligence in the processing, though, then the Tcl-based 'expect'

Jonathan Leffler
nope - this one doesn't work either. I'm now trying now to dig deeper into the "expect" world
johnrembo
@johnrembo: in all honesty, I'm not very surprised that it doesn't do what you need. Indeed, I don't think I've often found it useful. Nevertheless it exists and _if_ it does what you need, there isn't anything much simpler. But if you need any adaptability in the output, then 'expect' is probably what you need.
Jonathan Leffler
and still - I found "yes" utility pretty usefull (well, not for this particular case) - can't imagine how I didn't knew of existance of such until now (yes, was using a cup of coppe placed over the "enter" key)
johnrembo
+1  A: 

Note, you can get rid of the infinite loop:

spawn /usr/local/bin/nmap -A -T4 -p 21-100 localhost
expect arting {sleep 3; send \r}
expect {
    "done;" {
        sleep 3
        send \r
        exp_continue
    }
    eof
}
puts "finished"

Are you sure you need the sleeps? They can usually be avoided by using -regexp matching with the expect command.

Helpful Expect tip: while developing, use exp_internal 1 to verbosely see how your patterns are matching the command output.

glenn jackman
glenn, thanx for the tips
johnrembo