I want to grep the output from wvdial while wvdial is running. Actually I want to fire a task when the modem is connected.

I want to do this in the following way: grep string "IP address", if it is available it means modem is connected and then fire another shell script to perform another task.

How could it be made possible?


Strictly speaking, an answer:

Many programs, when piped somewhere, buffer their output—they write multiple lines at once. This increases performance, but of course breaks your attempts to grep the output. GNU Coreutils includes a utility stdbuf which often lets you change this. You'd want to change stdout or stderr to line-buffered. That might work—but is probably not the best solution.

A better way:

If you look in /etc/ppp, you'll see there are several scripts there, including /etc/ppp/ip-up. When the PPP link comes up, pppd runs that script. At least on Debian, that script then runs all scripts in /etc/ppp/ip-up.d/.

So the much simpler solution is to put your shell script in /etc/ppp/ip-up.d/ or to edit (or create if it doesn't exist) /etc/ppp/ip-up. There is similarly an …ipv6-up, and also …-down variants as well. There are a few more; check the pppd manpage’s "Scripts" section for details.

Keep in mind that you can just use the up script to signal your script (or some other) that the link is ready—it doesn't have to do the work itself. E.g., you could signal with something as simple as kill -USR1 other-script-pid and then use trap & sleep to wait for that signal. Or you could open a named pipe ("fifo", e.g., mkfifo) for reading (which will block until there is a writer) and then have the up script just echo alive > /path/to/fifo. Or maybe echo back the IP address, or other useful information. Or your script could kill -STOP $$, and then your up script could kill -CONT script-pid. There are a bunch of ways to accomplish this. (You could even go all out and use dbus.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.