bash command to repeatedly emulate keypress on a proceess
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;
}
}
Probably easiest to use expect
.
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'
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 sleep
s? 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.
精彩评论