Printing IP address of Nic card to bash using script - linux

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)

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

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

Shell script to find the IP address of an Virtual machine created by using KVM/virsh command

I am looking for the shell script to find the IP address of a virtual machine created by using KVM/VIRSH.
I used the following steps to get it so, but couldn´t able to find it.
Ping the IP addresses in range
2.Use Virsh list command to list all the active VM
3.use Virsh dumpxml domainname and project the xml of vm
use grep command and fetch the Hardware address of vm
5.Display the hardware address of each vm
Now I would like to add one more step like fetching the IP address for that particular Hardware address using ¨arp -ne¨
I couldn´t able to figure out how to add the part.
could any one help me on this.
for i in {1..150}
do
ping -c 1 -n -q -r -t 1 -s 1 -W 1 192.168.1.$i > /dev/null &
done
for name in `virsh list | grep running | awk '{ print $2 }'`
do
# printf "\n$name\n "
arp -e | grep "`virsh dumpxml $name | grep "mac address"|sed "s/.*'\(.*\)'.*/\1/g"`" |
awk '{ printf $1 ; printf " " ; printf $3 "\n" }'
done
current output:
$ ./virshshell.sh
vm2 52:54:00:4b:7f:41
vm3 52:54:00:0e:4c:42
The output I am expecting is
$ ./virshshell.sh
vm2 52:54:00:4b:7f:41 192.*.*.*
vm3 52:54:00:0e:4c:42 192.*.*.*
Use nmap to do network discovery instead of ping. It can do what ping does but also much more, plus it runs way faster and takes care of the network-scope scanning that you're doing in your for loop.
$ nmap -T5 -n -PE 192.168.4.0/24 > /dev/null
$ ip neigh show | grep 192.168.4 | grep -v FAILED
192.168.4.92 dev eth0 lladdr 54:52:00:90:90:92 REACHABLE
192.168.4.11 dev eth0 lladdr fa:16:3e:fa:ac:07 REACHABLE
192.168.4.91 dev eth0 lladdr 54:52:00:90:90:91 REACHABLE
192.168.4.90 dev eth0 lladdr 54:52:00:90:90:90 REACHABLE

busybox network configuration script error

Hello I have the script in the start up but I don't get why it is showing error on execution
#!/bin/sh
# Starting the network interface
PATH="/sbin:/bin:/usr/bin:/usr/sbin"
FILENAME="/etc/ipconf"
count=0
while read LINE
do
ipValues[count]=$(echo $LINE | awk -F'=' '{print $2}')
count=`expr $count + 1`
done < $FILENAME
echo "Setting up IP Address"
ifconfig eth0 up
ifconfig eth0 ${ipValues[0]} netmask ${ipValues[1]}
echo "IP :: ${ipValues[0]} SUBNET MASK :: ${ipValues[1]}"
route add default gw ${ipValues[2]}
echo "Default Gateway :: ${ipValues[2]}"
echo "Network configured properly"
exit 0
Here is my ipconf file
IPADDRESS=192.168.1.13
SUBNETMASK=255.255.255.0
GATEWAY=192.168.1.220
And here is my scripts error
ipValues[count]=192.168.1.13 Not found
ipValues[count]=255.255.255.0 Not found
ipValues[count]=192.168.1.220 Not found
Setting up IP Address
Line 20 syntax error: Bad substitution
My script is braking in line ifconfig eth0 ${ipValues[0]} netmask ${ipValues[1]}. Is this array assignment is correct or busybox scripts needs different approach?
You're right, busybox doesn't support the array syntax in your script.
In order to set the values, you might use
eval ipValues$count=$(echo $LINE | awk -F'=' '{print $2}')
and to read the variables
ifconfig eth0 ${ipValues0} netmask ${ipValues1}
echo "IP :: ${ipValues0} SUBNET MASK :: ${ipValues1}"
route add default gw ${ipValues2}
echo "Default Gateway :: ${ipValues2}"

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