You are not logged in.

#1 2018-12-11 12:13:04

philipW
Member
Registered: 2017-03-21
Posts: 145

[SOLVED]zshrc function

I Wrote a function in my zshrc to put my wireless interface in monitor mode.
It is supposed to "toggle" monitor mode. So when i run it it is supposed to put the interface in monitor mode and when i run it again it puts the interface back in managed mode. I tried to do this with a simple IF statement.
But when i run the function it only turns it on when i run it again it doesn't turn the interface back in managed mode.

function wifimon() {
 if [ "wlp3s0mon:" = "$(ifconfig wlp3s0mon | head -n1 | cut -d " " -f1)" ]
  then
   sudo airmon-ng check kill
   sudo airmon-ng stop wlp3s0mon
  else
   sudo airmon-ng check kill
   sudo airmon-ng start wlp3s0
 fi
}

export wifimon

Does anyone know what i did wrong ?

Last edited by philipW (2018-12-12 09:05:44)

Offline

#2 2018-12-11 12:28:08

ayekat
Member
Registered: 2011-01-17
Posts: 1,300
Website

Re: [SOLVED]zshrc function

Maybe the condition always returns true. What's the output of this?

ifconfig wlp3s0mon

(both in managed and monitor mode)

Offline

#3 2018-12-11 12:48:20

philipW
Member
Registered: 2017-03-21
Posts: 145

Re: [SOLVED]zshrc function

This is the output in managed mode:

ifconfig wlp3s0mon
wlp3s0mon: error fetching interface information: Device not found

This is the output in monitor mode:

ifconfig wlp3s0mon         
wlp3s0mon: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        unspec 60-67-20-FD-86-90-A0-B8-00-00-00-00-00-00-00-00  txqueuelen 1000  (UNSPEC)
        RX packets 59  bytes 7530 (7.3 KiB)
        RX errors 0  dropped 59  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

This is the output of "ifconfig wlp3s0mon | head -n1 | cut -d " " -f1"

wlp3s0mon:

Offline

#4 2018-12-11 13:02:41

Koatao
Member
Registered: 2018-08-30
Posts: 6

Re: [SOLVED]zshrc function

In both stderr et stdout, you get the string (i.e. always true as said before)

The only thing you need to do is to use exit code of your command.

Last edited by Koatao (2018-12-11 13:03:37)

Offline

#5 2018-12-11 13:09:49

philipW
Member
Registered: 2017-03-21
Posts: 145

Re: [SOLVED]zshrc function

Koatao wrote:

In both stderr et stdout, you get the string (i.e. always true as said before)

The only thing you need to do is to use exit code of your command.

I'm sorry i do not quite understand since i'm still a bit of a noob at shell scripting. What do you mean with using the exit code?

Offline

#6 2018-12-11 13:50:14

ayekat
Member
Registered: 2011-01-17
Posts: 1,300
Website

Re: [SOLVED]zshrc function

As already written above, the output of your command is always the same, and the if condition there is always truefalse (thanks @Trilby for pointing out).

philipW wrote:

What do you mean with using the exit code?

Each process returns a numeric value when it finishes executing. It is used as an indicator for the parent process, typically to inform it whether it has terminated successfully or an error has occured. The convention is to return 0 on success, and a non-zero value if an error occurred.

  • You can test this by running e.g. ls, then echo $? (to print the exit code of the previous command): this will print 0, since ls completed successfully.

  • OTOH, if you run ls -E (which fails, because -E is not a valid option), then echo $? will print 2, which is ls' error code to indicate "invalid command line option" (I assume).

The same is true for most (well written) programs: they will return a non-zero exit code if an error occurred (such as in your case, ifconfig if it can't find the device).

This can be used in shell scripts: if and while run a given clause only if the command given as argument returns 0:

if {command}; then
    # code
fi

This will only execute #code if {command} returns 0. In your case, you could use your ifconfig command there, and if it returns success, you know that the device exists.

Side note: The [] often used in conjunction with conditionals like if and while is really also just a program. [ is an alternative command name for test, which takes some arguments and returns success or failure depending on whether a given condition is met. So

[ a = a ]    # returns 0, same as `test a = a`
[ b = c ]    # returns 1, same as `test b = c`

Furthermore, true and false are just commands that always return 0 or 1, respectively; the following will thus run forever (since true is supposed to always return 0):

while true; do
    # code
done

Last edited by ayekat (2018-12-11 15:49:36)

Offline

#7 2018-12-11 15:31:09

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 21,074
Website

Re: [SOLVED]zshrc function

Let's take a step back.  The error message IS on stderr, and it does not match:

$ x=$(ifconfig wlp3s0mon | head -n1 | cut -d " " -f1)
ifconfig: wlp3s0mon: error fetching interface information: Device not found

$ echo $x

This fits the symptoms the OP descrbed: the function always tries to turned managed mode ON.  This means the condition NEVER passes (i.e. is never evaluated as true).

All the previous responses in this thread are tracking the wrong problem.

What is the output of the following when in monitoring mode:

[ "wlp3s0mon:" = "$(ifconfig wlp3s0mon | head -n1 | cut -d " " -f1)" ] && echo matched

In any case, the first step in debugging would be to simplify the needlessly complex pipeline:

if ifconfig | grep -q wlp3s0mon
   then
      #...

Last edited by Trilby (2018-12-11 15:36:00)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2018-12-11 17:29:31

philipW
Member
Registered: 2017-03-21
Posts: 145

Re: [SOLVED]zshrc function

Trilby wrote:

What is the output of the following when in monitoring mode:

[ "wlp3s0mon:" = "$(ifconfig wlp3s0mon | head -n1 | cut -d " " -f1)" ] && echo matched

It says matched.

Trilby wrote:

In any case, the first step in debugging would be to simplify the needlessly complex pipeline:

if ifconfig | grep -q wlp3s0mon
   then
      #...

I changed the code a bit. Now with grep instead of head and cut (Why didnt i think of this). To find out if the IF statement works i added "echo".

function wifimon() {
 if ifconfig | grep -q wlp3s0mon
  then
   echo "hoi"
   sudo airmon-ng check kill
   sudo airmon-ng stop wlp3s0mon
  else
   echo "doei"
   sudo airmon-ng check kill
   sudo airmon-ng start wlp3s0
 fi
}

export wifimon

When my interface is in monitor mode it echoes hoi. And when its in managed mode it echoes doei.
But when i run the command in managed mode i get this:

wifimon
doei

Killing these processes:

  PID Name
 9736 NetworkManager
 9766 wpa_supplicant



PHY	Interface	Driver		Chipset

phy0	wlp3s0		iwlwifi		Intel Corporation Centrino Advanced-N 6205 [Taylor Peak] (rev 34)

Error setting channel: command failed: Device or resource busy (-16)
Error -16 likely means your card was set back to station mode by something.
Removing non-monitor wlp3s0mon interface...

WARNING: unable to start monitor mode, please run "airmon-ng check kill"

When i run the function when my interface is in monitor mode it turns it back in managed mode. So that part works

Last edited by philipW (2018-12-11 17:31:49)

Offline

#9 2018-12-11 17:33:35

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 21,074
Website

Re: [SOLVED]zshrc function

I have no idea how airmon-ng works / is supposed to work, but is the second command really correct?  You start wlp3s0 rather than wlp3s0mon?  Does that command work from the command line (when not in monitor mode)?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2018-12-11 17:48:12

philipW
Member
Registered: 2017-03-21
Posts: 145

Re: [SOLVED]zshrc function

Trilby wrote:

I have no idea how airmon-ng works / is supposed to work, but is the second command really correct?  You start wlp3s0 rather than wlp3s0mon?  Does that command work from the command line (when not in monitor mode)?

Yes, when you run "airmon-ng" start you start managed mode on a interface and that interface turns into a "mon" interface. When my interface is in managed mode and i run sudo airmon-ng start wlp3s0 in the command line it goes into monitor mode.

Offline

#11 2018-12-11 18:37:25

eschwartz
Trusted User/Bug Wrangler
Registered: 2014-08-08
Posts: 2,853

Re: [SOLVED]zshrc function

Can we please not use ifconfig, which is not installed by default due to being deprecated literally everywhere, and instead use "ip" which is?

if ip addr show wlp3s0mon > /dev/null 2>&1; then
    echo "the device wlp3s0mon currently exists"
fi

Last edited by eschwartz (2018-12-11 18:38:18)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#12 2018-12-12 09:05:04

philipW
Member
Registered: 2017-03-21
Posts: 145

Re: [SOLVED]zshrc function

eschwartz wrote:

Can we please not use ifconfig, which is not installed by default due to being deprecated literally everywhere, and instead use "ip" which is?

if ip addr show wlp3s0mon > /dev/null 2>&1; then
    echo "the device wlp3s0mon currently exists"
fi

My function doesn't work for some reason if i use

if ip addr show wlp3s0mon > /dev/null 2>&1; then

But i managed to get it work with a "sleep" in between.

function wifimon() {
 if ifconfig | grep -q wlp3s0mon
  then
   sudo airmon-ng check kill &
   sleep 1 &
   sudo airmon-ng stop wlp3s0mon
  else
   sudo airmon-ng check kill &
   sleep 1 &
   sudo airmon-ng start wlp3s0
 fi
}

Offline

#13 2018-12-12 10:03:15

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 6,228

Re: [SOLVED]zshrc function

Why are you suddenly backgrounding the commands in the first place? You'd probably not need the sleep (which you also background hence the "working" part is pure coincidence) if you didn't do that.

Offline

#14 2018-12-12 10:58:34

philipW
Member
Registered: 2017-03-21
Posts: 145

Re: [SOLVED]zshrc function

V1del wrote:

Why are you suddenly backgrounding the commands in the first place? You'd probably not need the sleep (which you also background hence the "working" part is pure coincidence) if you didn't do that.

I did some testing and i found out airmon-ng only works with the sleep command in front of it. As for the "&" the person who helped me debugging this morning told me it was the "proper" way of ending a line. It would be helpful if you showed me the correct way of writing this script.

Offline

#15 2018-12-12 12:03:15

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 6,228

Re: [SOLVED]zshrc function

A & at the end of a line will lead to the previous command being backgrounded which means the shell will immediately resume parsing the next line without waiting for a given command to finish. This is intended for long running commands that shouldn't halt further execution of the script, which isn't the case here. If you remove them, there is unlikely to be a need for the sleep.

Offline

#16 2018-12-12 12:17:12

philipW
Member
Registered: 2017-03-21
Posts: 145

Re: [SOLVED]zshrc function

Ah oke i didnt know that. it is indeed unnecessary to use a "&" in this case.

Offline

Board footer

Powered by FluxBB