how to extract only MAC of wlan0 from ifconfig - linux

How to extract only MAC of wlan0 from ifconfig using CUT(!) in the manner of:
ifconfig eth0 | grep HWaddr | cut -d ' ' -f 11
And why
ifconfig wlan0 | grep HWaddr | cut -d ' ' -f 11
doesn't work in the same manner? Thanks.

ifconfig wlan0 | grep -E -o '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
EDIT (updated to include the cut command):
ifconfig wlan0 | grep -E -o '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | cut -f 1

Related

How to use ssh -t command which includes grep with quotes

I am trying to grep the third octet in IP address to an tap device on remote machine.
ssh -t user#host "/sbin/ifconfig tap0 | grep "inet" | /usr/bin/awk -F'[: ]+' '{ print $4 }' | awk -F'[.]' '{print $3}'"
I am resulting this:
inet addr:10.22.66.77 Bcast:10.22.66.255 Mask:255.255.255.0
When i run the command on the remote machine it shows 66
How to make it working with ssh -t?
Sometimes using perl is simpler:
ssh -t user#host "/sbin/ifconfig tap0" | perl -n -e 'if (/inet\saddr:\d+\.\d+\.(\d+)/) { print "$1\n"}'
it runs regular expression pattern on the local machine match on the third octet following addr: and this is then printed via $1
The pattern match is run on the local machine to avoid problems with escaping " (In your example code the " in the grep inet seems to terminate the ssh...)

bash script interpreting awk correctly

I have a for loop script that needs to log into hosts and get their hostname and the value of eth0 interface. Im using the code below, but the awk command is not being read correctly when running it on a bash script
#!/bin/bash
for i in `cat test.txt`;
do
store_number=$(ssh -q -A -o userknownhostsfile=/dev/null -o stricthostkeychecking=no -o batchmode=yes -o connecttimeout=5 "$i" "hostname | cut -c4-7");
eth0_ip=$(ssh -q -A -o userknownhostsfile=/dev/null -o stricthostkeychecking=no -o batchmode=yes -o connecttimeout=5 "$i" "sudo ifconfig eth0 | awk 'FNR==2 {print $2}'");
output="${store_number}: ${eth0_ip}"; echo $output >> /home/eth0status.txt;
done
The output is as follow:
0021: inet addr:10.1.10.62 Bcast:10.1.10.255 Mask:255.255.255.0
0022: inet addr:10.0.1.74 Bcast:10.0.1.255 Mask:255.255.255.0
0023: inet addr:172.16.16.103 Bcast:172.16.16.255 Mask:255.255.255.0
I need the output to be something like:
0021: addr:10.1.10.62
0022: addr:10.0.1.74
0023: addr:172.16.16.103
Thanks for your help
Quoting is a hard problem. When you write:
eth0_ip=$(ssh ... "$i" "sudo ifconfig eth0 | awk 'FNR==2 {print $2}'");
The internal single quotes '' do nothing to prevent expansion of $2. The shell sees $2 in double quotes. It will perform variable expansion, and it's highly likely your second argument ($2 for the script) is unset. Test it out:
$ echo "sudo ifconfig eth0 | awk 'FNR==2 {print $2}'"
sudo ifconfig eth0 | awk 'FNR==2 {print }'
Either escape $, or use single quotes outside, or as others have recommended, use awk outside the SSH command.

Extracting MAC address from ifconfig output

I am writing a #!bin/bash shell script to automate MAC spoofing. In the script that I have written I make the outputs of ifconfig -a | grep HWaddr equivalent to two different variables. The command ifconfig -a | grep HWaddr returns
eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
and
wlan0 Link uncap: Ethernet HWaddr 00:00:00:00:00:00
but I want the command to return just the MAC address for wlan0.
Try:
[root#linux ~]$ /sbin/ifconfig wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
00:25:90:F0:3F:92
By specifying wlan0 as the first argument to ifconfig, you are telling it you only want information about that particular interface, so you should only get a single line returned.
The grep command then searches for a MAC address in the output and prints only the portion of the ifconfig output which matches.
OR
Just for your script you can try follwong:
ifconfig -a | grep HWaddr | awk '{print $5}'
OSX
ifconfig en1 | awk '/ether/{print $2}'

How to select vendor when using macchanger in linux

Currently, I am using this command to spoof my MAC address:
macchanger eth0 -A
However, I want to be able to select which vendor I use. I know that the switch -l displays a list of all available vendors, but say I want to use one from Cisco, what command do I use to actually use that specific vendor rather than the system choosing a random vendor for me each time?
You could add a function like this to .bashrc
Then you can: macspoof Apple eth0
macspoof () {
Vendor=$(macchanger -l | grep $1 | shuf -n 1 | awk '{print $3}')
Tail=$(echo $RANDOM | md5sum | sed 's/.\{2\}/&:/g' | cut -c 1-8)
Mac="$Vendor:$Tail"
sudo macchanger -m $Mac $2
}

How to cut ifconfig to get eth* details alone?

I'm writing a script to print ethtool details of ifconfig.
Sample output should be from ifconfig is like,
eth0
eth1
eth2
I have tried using below command,
root#bt# ifconfig | cut -d ":" -f 1
But could not able to achieve the same.
Actually, i need to get these eth* and pass in
root#bt# ethtool <arg1> where arg1=eth*
to get results :-) can you please help me to get crop from ifconfig. ?
No.
$ awk -F: '$1 ~ "eth" { print $1 }' /proc/net/dev
eth0
With grep & ifconfig :
ifconfig | grep -o '^eth[0-9]\+'
Or with only grep :
grep -oP '^ *\Keth[0-9]+' /proc/net/dev

Resources