Extracting MAC address from ifconfig output - linux

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}'

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...)

Printing IP address of Nic card to bash using script

currently I am working on a school assignment on Linux using Vim where I have to write a script that displays the user that is currently logged in, the time and date, and list only the IP address of the Nic card. I have everything working except for the IP address part. If anyone could help I would appreciate it greatly.
Edit to include the code I have at the moment.
#!/bin/bash
Time=$(date)
IP=$(ifconfig ens33)
echo "The following user is currently logged in $USER"
echo ""
echo "The current time is $Time"
echo ""
echo "The IP information is $IP"
You can filter the result of ifconfig using awk like this (IPv4):
$ ifconfig ens33 | awk '/inet addr/{print substr($2, 6)}'
Result:
10.10.xx.xx
inet addr: represents IPv4 address.
inet6 addr: represents IPv6 address.
this line
IP=$(ifconfig ens33| grep inet | sed 's/ */ /' | cut -d" " -f3)

Programatically activate and deactivate network interfaces using nmcli

I wrote a script that deactivates all the interfaces available using nmcli commands such as:
for iface in `(nmcli -f GENERAL dev list 2>/dev/null || nmcli -f GENERAL dev show) | grep 'GENERAL.DEVICE' | awk '{print $2}'`
do
nmcli dev disconnect iface $iface
done
I would now like to reactivate these interfaces; there is no nmcli dev connect iface … command, and I'm stuck. In particular, my problem is to reactivate the Ethernet interface.
Thanks in advance for your help!
Use this command:
nmcli -p con up id "interface name" iface eth0
You can also use uuid instead of id.
-p is just for human readable output and can be ignored.
iface eth0 can also be omitted.
Take a look here for more information.

how to extract only MAC of wlan0 from ifconfig

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

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