0

In my Bash script I must launch a command (aircrack-ng) to recover the output in a var for using it later. The target is to parse the output of a .cap file in order to know if there is some kind of data (handshake for WPA networks).

The problem is this command sometimes (depending of the file you want to parse), is stopping prompting the user for choosing one option. If the file has only data from one network it doesn't stop. If the file has different network data, it stops.

I already have the output of the command parsed, but the problem is the script stops. I would kill the command immediately after launched and recovered the output to the var and don't want the script stops.

I tried to launch it with & at the end to run in background and then killing it, but it stops anyway.

nets_from_file=$(aircrack-ng /path/to/somefile.cap 2> /dev/null | egrep "WPA \(1" | awk '{ saved = $1; $1 = ""; print substr($0, 2) }' &)

kill -9 $(ps aux | grep aircrack-ng | grep -v "grep" | awk '{print$2}') > /dev/null 2>&1

Is there a way to launch first command what is causing a stop, recover the output and kill that command without stopping script?

Biffen
  • 6,249
  • 6
  • 28
  • 36
OscarAkaElvis
  • 5,384
  • 4
  • 27
  • 51
  • 1
    You could redirect the input of the command to `/dev/null`. Then it will get an immediate EOF when it tries to get an answer from the user. Or you could pipe the `yes` command to feed answers to it. – Barmar Apr 23 '16 at 10:13
  • I tried, but if I put > /dev/null on aircrack-ng command, I lost the output what I want to catch into a var. :/ – OscarAkaElvis Apr 23 '16 at 11:13
  • That's redirecting the output, I said to redirect the **input**. That's `< /dev/null`. – Barmar Apr 23 '16 at 11:15
  • I put the line: nets_from_file=$(aircrack-ng /path/to/somefile.cap < /dev/null | egrep "WPA \(0" | awk '{ saved = $1; $1 = ""; print substr($0, 2) }' and now it stops... nothing happens, and if i press 1 for example, it holds in another line ... maybe to enter another input? i press 2, or 3, and enter, and has no end... not sure what you are referring. – OscarAkaElvis Apr 23 '16 at 11:26
  • It's possible the program is reading from `/dev/tty` instead of standard input. I don't know how that program works. – Barmar Apr 23 '16 at 11:27
  • Do NOT use *SIGKILL* (`kill -9`) to terminate processes. All processes will terminate in response to the default *SIGTERM*. If they don't terminate instantly, that's because they're busy cleaning up. If you interrupt them, you break them. See http://stackoverflow.com/a/690631/347411 and http://turnoff.us/geek/dont-sigkill/ – Rany Albeg Wein Apr 23 '16 at 12:56
  • Thanks for the tip. Finally not even necessary kill the proccess... with the echo before and the pipe, it finishes as I said in another answer . – OscarAkaElvis Apr 24 '16 at 13:15

1 Answers1

0

woow... I found it! amazingly simple after a headache...

nets_from_file=$(echo "1" | aircrack-ng /path/to/somefile.cap 2> /dev/null | egrep "WPA \(0"  | awk '{ saved = $1; $1 = ""; print substr($0, 2) }')

a simple echo before with pipe did the trick! :)

OscarAkaElvis
  • 5,384
  • 4
  • 27
  • 51