1

I'm new to shell scripting and here is my problem:
I want to store PID's from output of airmon-ng check to some variables (for ex: $1, $2, $3) so that I can execute kill $1 $2 $3. here is sample output of airmon-ng check:

Found 3 processes that could cause trouble.
If airodump-ng, aireplay-ng or airtun-ng stops working after
a short period of time, you may want to kill (some of) them!

  PID Name
  707 NetworkManager
  786 wpa_supplicant
  820 dhclient

I want to grab numbers 707, 786, 820.
I tried using set 'airmon-ng check' and then using for loop:

set `airmon-ng check`
n=$#
for (( i=0; i<=n; i++ ))
do
        echo $i
done

it outputs 1,2,3,...36 not words or numbers so I couldn't figure out how I should do it.

1
  • I think There will be a better way to do this but I can't figure it out. So any ways are accepted :) Commented Aug 10, 2016 at 15:07

4 Answers 4

1
airmon-ng check | egrep -o '\b[0-9]+\b' | xargs kill

egrep is grep with extended regular expressions (like grep -E), -o says to extract only the matching parts, \b matches word boundaries so you don't get any numbers accidentally occuring in process names or something, [0-9]+ matches one or more decimal digit, xargs kill passes all the matches as arguments to the kill command.

Note that parsing output intended to be read by humans might not always be a good idea. Also, just killing all those processes doesn't sound too smart either, but proper usage of airocrack is beyond this question.

Sign up to request clarification or add additional context in comments.

2 Comments

Not sure if this captures just the pids. You need a tighter check
Yeah, but the approach isn't stable anyway. It's just one more issue on top of thousands of other ;).
0

You can get list of the PIDs separated by spaces e.g. like this (everything from the 1st column after "PID"):

l=`airmon-ng check | awk 'BEGIN { p=0 } { if (p) { print $1" "; } if ($1=="PID") { p=1 } }' | tr '\n' ' '`

1 Comment

Didn't even thought of it, Awesome. Real big thanks :)
0

Why not use grep?

myvar=$(airmon-ng check | grep '[0-9]\{3,6\}')

This assumes a PID of 3 to 6 digits, and will grab anything from the airmon-ng output of a similar length. So this may not work as well if the output includes other strings with digits of a similar length.

3 Comments

Not sure if this will output just the pids. What if the o/p contains numbers other than pids?
@sjsam Unfortunately I am not as well-versed in regexp as I'd like to be and put something together that would fit the typical PID format.
Hmm.. From the output we may assume that the pids appear at the beginning, you just need a bit of googling to do the rest.
0

I would use awk for this and store the output in an array

pids=( $(airmon-ng check | awk '/^[[:blank:]]+[[:digit:]]+[[:blank:]]+/{print $1}') )
#'pids' is an array
kill "${pids[@]}" #killing all the processes thus found.

2 Comments

real nice approach thanks. Unfortunately my rating is so low that I Can't Upvote the answer :)
@Ika : Not a problem pal, you enjoy the life :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.