How to get IPv4, gateway and netmask address in shell? - bash

How to detect IPv4, gateway and netmask and DNS address in shell?
I need this to modify a script to automate deployment of virtual machines.

A very simple way, but very unreliable, if you know what interface you need could be:
ifconfig_line=$(ifconfig wlan0 | grep -sw "inet" | tr ":" " ")
echo "IP: "$(echo $ifconfig_line | awk {'print $3'})
echo "Mask:"$(echo $ifconfig_line | awk {'print $7'})
echo "Gateway: "$(route -n |head -n3|tail -n1|awk '{print $2}')
echo "DNS: "$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')

Related

Raspberry Pi Bash Script for network adapter, ip addresss and Mac address

I want to have a short cut to display the adapter, IP address and Mac address. I have the following:
#! /bin/bash
for iface in $(ifconfig | grep -v "lo" | cut -d ' ' -f1| tr '\n' ' ')
do
ipadd=$(ip -o -4 addr list $iface | awk '{print $4}' | cut -d/ -f1)
madd=$(ip -o link list $iface | awk '{print $17}')
printf "$iface\t$ipadd\t$madd\n"
done
The ethernet adapter doesn't show IP address and show as no such device. But if I run the command manually in bash it work and show up. The same script work correctly on my Ubuntu but not on Raspberry Pi (only manual command work). wlan0 works with no problem on Pi
The MAC address doesn't work at all, but if I run the command manually ip -o link list <adapter> | awk '{print $17}') it show the Mac address correctly.
Please advise where could have gone wrong.
Update:
+++ ifconfig
+++ grep -v lo
+++ cut -d ' ' -f1
+++ tr '\n' ' '
++ for iface in $(ifconfig | grep -v "lo" | cut -d ' ' -f1| tr '\n' ' ')
+++ ip -o -4 addr list enxb827ebe7229c:
+++ awk '{print $4}'
+++ cut -d/ -f1
Device "enxb827ebe7229c:" does not exist.
++ ipadd=
+++ ip -o -4 link list enxb827ebe7229c:
+++ awk '{print $17}'
Device "enxb827ebe7229c:" does not exist.
++ madd=
++ printf 'enxb827ebe7229c:\t\t\n'
enxb827ebe7229c:
++ for iface in $(ifconfig | grep -v "lo" | cut -d ' ' -f1| tr '\n' ' ')
+++ ip -o -4 addr list wlan0:
+++ awk '{print $4}'
+++ cut -d/ -f1
++ ipadd=192.168.1.4
+++ ip -o -4 link list wlan0:
+++ awk '{print $17}'
RTNETLINK answers: No such device
Cannot send link get request: No such device
++ madd=
++ printf 'wlan0:\t192.168.1.4\t\n'
wlan0: 192.168.1.4
If I run the command manually:
ip -o -4 link list enxb827ebe7229c | awk '{print $17}'
I get the Mac address
If I run this
ip -o addr list enxb827ebe7229c | awk '{print $4}' | cut -d/ -f1
I too will get the ipaddress correctly
A small fix was enough:
for iface in $(ifconfig | grep -v "lo:" | cut -d ' ' -f1 | cut -d: -f1); do
ipadd=$(ip -o -4 addr list $iface | awk '{print $4}' | cut -d/ -f1);
madd=$(ip -o link list $iface | awk '{print $17}');
printf "$iface\t$ipadd\t$madd\n";
done
The part ifconfig | grep -v "lo:" | cut -d ' ' -f1| tr '\n' ' ' leaves the : character in the output, so you are iterating over eth0: eth1: not over eth0 eth1. You need to remove the :, either with a simple cut -d: -f1 or tr -d: or any other mean.
Also note, as you've just discovered, the ifconfig output differs between platforms and implementations. It's better to just stick to the new ip command. Ex. ip a | sed -n '/^[^ ]*: \([^ ]*\):.*/{s//\1/;p;}'
There is no need for tr '\n' ' '. Shell interprets any whitespace character - that is tab, space or newline - as a word separator.

Get details of all tcp listening processes

#! /bin/bash
a=`netstat -plant | grep -i listen`
#to get ip and port
b=`echo $a | awk {'print $4}'`
#to get the process id
c=`echo $a | awk {'print $7}' | awk -F '/' {'print $1'}`
set -- $c
#to get the details of process
g=`ps aux | grep $1`
m=`echo $g | awk {'print $2}'`
n=`echo $g | awk {'print $9}'`
o=`echo $g | awk {'print $11}'`
echo The process with PID $m invoked by command "$o", is listening at IP and Port : $b . The process has been running since $n
I was trying to make a script to display the details like PID, IP, port, running since, and the command of all tcp listening processes in a simple language. The script I made gives the details of only 1 process
You only need to change the assignment to a with a while read construct:
netstat -plant | grep -i listen | while read a; do
#to get ip and port
b=`echo $a | awk {'print $4}'`
#to get the process id
c=`echo $a | awk {'print $7}' | awk -F '/' {'print $1'}`
set -- $c
#to get the details of process
g=`ps aux | grep $1`
m=`echo $g | awk {'print $2}'`
n=`echo $g | awk {'print $9}'`
o=`echo $g | awk {'print $11}'`
echo The process with PID $m invoked by command "$o", is listening at IP and Port : $b . The process has been running since $n
done
This construct will read output from the grep into a variable one line at a time.

Shell script : Displaying network interface and their corresponding ip address in a table format

I want to display network interface and their ip address in a table format using shell script. I have this code
#! /bin/bash
interface=$(ifconfig | cut -d ' ' -f1| tr '\n' ' ' | tee save.tmp)
ip=$(ifconfig | awk -F':' '/inet addr/&&!/127.0.0.1/{split($2,_," ");print _[1]}')
echo $interface
echo $ip
The output of this code is
eth0 vmnet1 vmnet8
10.30.10.226 192.168.142.1 192.168.3.1
I want like this
eth0 10.30.10.226
vmnet1 192.168.142.1
vmnet8 192.168.3.1
Thank you in advance
Rearrange it like this:
for iface in $(ifconfig | cut -d ' ' -f1| tr '\n' ' ')
do
addr=$(ip -o -4 addr list $iface | awk '{print $4}' | cut -d/ -f1)
printf "$iface\t$addr\n"
done
Here is my shell script (hope it helps) :
#!/bin/bash
clear
echo -e "+-----------+-----------------+\n| Interface | IP Address |\n+-----------+-----------------+"
for iface in $(ip -o -4 addr list | awk '{print $2}' | tr '\n' ' ')
do
ipaddr=$(ip -o -4 addr list $iface | awk '{print $4}' | cut -d/ -f1)
printf "|%10s | %-16s|\n" $iface $ipaddr
done
echo "+-----------+-----------------+"
Output:
+-----------+-----------------+
| Interface | IP Address |
+-----------+-----------------+
| lo | 127.0.0.1 |
| enp0s31f6 | 10.10.10.100 |
| wlp61s0 | 10.11.11.111 |
| tun0 | 10.20.20.200 |
+-----------+-----------------+

if-then-else based on command output

I am using netstat command to get information on networks. I want to put a condition here on protocol fetched. If it's TCP, I need to print different columns than if it's UDP.
Below is what I am trying to do, but it doesn't work. Please suggest and advise if there is something I am doing wrong:
if [$(netstat -anputw | awk '{print $1}')=="tcp"] then
netstat -anputw | awk '{print $1,",",$4"}' >> $HOME/MyLog/connections_$HOSTNAME.csv
elif [$(netstat -anputw | awk '{print $1}')=="udp"] then
netstat -anputw | awk '{print $5,",",$7}' >> $HOME/MyLog/connections_$HOSTNAME.csv
fi
I don't know what you're trying to achieve, however I think that netstat returns a list rather than a string, therefore comparing the output against a string is pointless. You have to loop it. Try the following
#!/bin/bash
OUTPUT=$(netstat -anputw | awk '{print $1}');
for LINE in $OUTPUT
do
if [[ $LINE == "tcp" ]] ; then
echo "TCP!"
elif [[ $LINE == "udp" ]] ; then
echo "UDP!"
fi
done
Why don't you separate the two cases by not asking netstat to mix them up?
netstat -anptw # Just tcp
netstat -anpuw # Just udp
Also, in the tcp case, you don't seem to care about the -p information so you might as well not ask for it. And in the udp case, there is no data in the State column, so the PID/Program name will actually be in column 6.
Putting that together, I get:
netstat -antw | awk '{print $1","$4}' >> $HOME/MyLog/connections_$HOSTNAME.csv
netstat -anpuw | awk '{print $5","$6}' >> $HOME/MyLog/connections_$HOSTNAME.csv
I suspect that isn't quite the information you're looking for either, though. Perhaps you want to distinguish between TCP listening and non-listening connections.
Always leave a space after [ and before ] in if statement and you should put command $(netstat -anputw | awk '{print $1}' under double quotes if you are doing string comparison.
Here's the final script:
if [ "$(netstat -anputw | awk '{print $1}')" == "tcp" ]; then
netstat -anputw | awk '{print $1,",",$4"}' >> $HOME/MyLog/connections_$HOSTNAME.csv
elif [ "$(netstat -anputw | awk '{print $1}') " == "udp" ]; then
netstat -anputw | awk '{print $5,",",$7}' >> $HOME/MyLog/connections_$HOSTNAME.csv
fi

Bash grep local IP address and write to file in makefile?

I'm trying to get my local ip with ifconfig and write it to file but for some reason fail to do so.
run:
LOCALIP=$(shell ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | awk '{print $1}') &
echo "var LOCAL_IP = '${LOCALIP}'" > local_ip.js &
It does get the correct IP but for some reason fails to write it to file and file contains only var LOCAL_IP = ''
Could anyone help me and tell what I'm doing wrong?
Two issues: one, each separate command in a Makefile recipe is executed in a separate shell, so you need to "merge" them. Two, you need to double the $ in the echo so that it is sent to the shell, not expanded first by make. This should work:
run:
LOCALIP=$(shell ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | awk '{print $1}'); \
echo "var LOCAL_IP = '$${LOCALIP}'" > local_ip.js
The semicolon and the line-continuation backslash ensure that the entire recipe is executed in the same shell, so that LOCALIP is still set when the echo is run. You could "simplify" this by embedding the command substitution directly in the argument to echo.
run:
echo "var LOCAL_IP = '$(shell ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | awk '{print $1}')'" > local_ip.js
This awk can work:
ifconfig | awk -F: '/inet addr/&& !($2 ~ /127\.0\.0\.1/){gsub(/ .*/, "", $2); print $2}'

Resources