Scope
I am working on a bash-script for handling repetitive tasks with putting wlan interfaces on, of, in and out monitor mode and displaying relevant information thru each step.
I started out something like this:
wx="wlan${1}"
wxm="${wx}mon"
# $wx
echo "$wx"
echo -n "stop $wxm if up....";airmon-ng stop $wxm &> /dev/null; echo " done.";
echo -n "bringing down $wx if up....";ifconfig $wx down &> /dev/null; echo " done.";
echo -n "changing mac to random....";macchanger -r $wx &> /dev/null; echo " done.";
echo "mac-adresses randomized"
echo -n "bringing up $wx....";ifconfig $wx up&> /dev/null; echo " done.";
echo -n "putting $wx in monitor mode....";airmon-ng start $wx &> /dev/null; echo " done.";
echo "$wxm successfully put in monitor mode"
wait
I tried to different ways to import available wlan-nics and with some help from a friend, ended up with this function using find
function setup_if() {
local wx="${1}"
local wxm="${wx}mon"
echo "setting up: $wx..."
run_cmd airmon-ng stop $wxm &&
run_cmd ifconfig $wx down &&
run_cmd macchanger -r $wx &&
run_cmd ifconfig $wx up &&
run_cmd airmon-ng start $wx
wait
}
for interface in $(find /sys/class/net -name 'wlan*'); do
interface=$(basename ${interface})
setup_if ${interface} || echo "FAILED: ${interface} $previous_command"
done
Where I got stuck
Now for the next step i want to print some useful information about the status of the wlans like cat /sys/class/net/wlan*/address and iwconfig |grep --regexp wlan
Finally i would like to export my operating nics to another script for capturing traffic with airodump-ng like airodump-ng -w /root/pcap/air_capture -c 1,4,6,8,11 $all_operating_wlans
Questions:
How do i make unique variables for all detected wlans, like $w1, $w2 etc. I haven't figured out how to make it thru
find /sys/class/net -name 'wlan?mon'since it is several lines of output that requires several variables.How to make those variables accessible to my other script that will capture packets.
Are there better ways to achieve the same results still in a bash-script context?
This is my first question on stackoverflow, and i'm happy to receive feedback on how my next question could be better!
In programming in general it is common to create an array variable when there is a need to store a dynamic collection of values. In Bash 4, it is possible to create an indexed array using
mapfilebuilt-in function:In this example,
mapfilecreates an indexed arrayifacesby reading lines from a here string. The content of the here string is built using the command substitution. Trailing newlines are removed with-toption.Note the use of
-mindepth 1option. The option excludes the/sys/class/netdirectory itself.Instead of invoking
basename, the command uses the-printfaction (in terms of find) with%fformat specifier:The
ifacesvariable will be available in the current scope. The array can be traversed as follows:Note,
mapfilewas introduced in Bash version 4.Process Substitution
The here string can be replaced with process substitution:
It's just a matter of preference.
Using mapfile in a Pipeline
It is possible to use mapfile in a pipeline, but the value of the array will be available only in the scope of the piped command. For example, the following script prints zero:
In order to use the
ifacesvariable within the pipe context, use the grouping commands, e.g.:Obviously, in this case, the scope of the
ifacesvariable will be limited to the group.Using Functions
There is a number of ways to return a value from a function. The simplest way is to set, or modify a global variable:
But it is a bad practice, since it is possible to overwrite an existing variable unintentionally.
Another way is to write a function that accepts a variable name:
Ah, I must write this, as I have
evalin the sample: warning, warning, warning! You should avoid usingevalas much as you can, as it may evaluate more than expected See this discussion, for instance. But in this particular example, I don't see any risks of usingeval.