Efficient way to get your IP address in shell scripts - shell

Context:
On *nix systems, one may get the IP address of the machine in a shell script this way:
ifconfig | grep 'inet' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}'
Or this way too:
ifconfig | grep 'inet' | grep -v '127.0.0.1' | awk '{print $2}' | sed 's/addr://'
Question:
Would there be a more straightforward, still portable, way to get the IP address for use in a shell script?
(my apologies to *BSD and Solaris users as the above command may not work; I could not test)

you can do it with just one awk command. No need to use too many pipes.
$ ifconfig | awk -F':' '/inet addr/&&!/127.0.0.1/{split($2,_," ");print _[1]}'

you give direct interface thereby reducing one grep.
ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'

Based on this you can use the following command
ip route get 8.8.8.8 | awk 'NR==1 {print $NF}'

Look here at the Beej's guide to networking to obtain the list of sockets using a simple C program to print out the IP addresses using getaddrinfo(...) call. This simple C Program can be used in part of the shell script to just print out the IP addresses available to stdout which would be easier to do then rely on the ifconfig if you want to remain portable as the output of ifconfig can vary.
Hope this helps,
Best regards,
Tom.

ifconfig | grep 'broadcast\|Bcast' | awk -F ' ' {'print $2'} | head -n 1 | sed -e 's/addr://g'

May be this could help.
more /etc/hosts | grep `hostname` | awk '{print $1}'

# for bash/linux
ipaddr(){
if="${1:-eth0}"
result=$(/sbin/ip -o -4 addr show dev "${if}" | sed 's/^.*inet // ; s/\/...*$//')
printf %s "${result}"
tty -s && printf "\n"
}

Related

How can I get the IP from arp command in shell

I've build this command:
arp -a | grep <mac here> | grep -P '\((.*?)\)' -o
This should return an IP address for a specific mac. The problem is that the IP get's returned but with () around them. I need them without the (). Any idea how I can fix this?
Currently: (192.168.187.136)
Should be 192.168.187.136
I believe arp is somewhat deprecated and ip neighbor is the proper replacement. Since ip n output doesn't have those parenthesis, you also avoid this issue
ip n | grep <mac here> | cut -d' ' -f1
You can remove the 1st and last character using sed:
arp -a | grep <mac here> | grep -P '\((.*?)\)' -o | sed 's/^|\(.*\)|$/\1/'
Also, on BASH 4.2 and newer:
ip=$(arp -a | grep <mac here> | grep -P '\((.*?)\)' -o)
echo "${ip:1:-1}"
using sed in one command
arp -a | grep <mac here> | grep -P '\((.*?)\)' -o | sed 's/^|\(.*\)|$/\1/'
Since you're already using grep -P :
arp -a | grep <mac here> | grep -oP '\(\K[^)]*'
\K is a PCRE meta-character meaning "drop the string matched so far", so you will check that there's a bracket before your result without outputting it, and the negated class [^)] will match up to the next bracket excluded.
You can also fuse both greps if the mac address appears before the IP :
arp -a | grep -oP '<mac here>.*\(\K[^)]*'
Using ip neighboor and (posix) awk:
ip n | awk -v mac='<mac here>' 'mac{print $1}'
or using GNU awk and arp:
arp -a | awk -F'[()]' -v mac='<mac here>' '$0 ~ mac{print $2}'

How to combine output from awk and pipe into htop?

$ ps -ef | grep python | awk -F' ' '{print $2}'
9825
4470
4619
$ htop -p 9825,4470,4619
For now, I have to make two separate commands in order to watch all python processes within htop. Is there a way that I can pipe all the results from awk and feed them into htop?
If you have pgrep (you probably do):
htop -p $(pgrep python | paste -sd,)
You could avoid grep and use only awk using something like:
ps -ef | awk '/[p]ython/{print $2}'
Then you could use:
htop -p $(ps -ef | awk -v ORS=, '/[p]ython/{print $2}')
Notice the [] around the p, this is a nice trick to avoid printing the second command itself:
ps -ef | awk '/[p]ython/{print $2}'
| |
cmd 1 cmd 2
it works because awk will translate the regex [p] to say something like "match characters from [p] in this case, p only, followed by ython:
[p]ython != python

bash: getting sorted interfaces ip list, without loopback (comma separated)

I need a script which outputs a sorted, comma separated, ip list of interfaces, not including loopback.
I managed to come up with the following script, which outputs a comma separated ip list of all interfaces:
ifconfig | awk '/inet addr/{print substr($2,6)}' | awk 'NR%2{printf $0",";next;}1'
I'm having trouble figuring out how not to include the loopback and to output a sorted list
I'm not experienced with bash, so this is probably not the simplest, but it works.
ip addr show | awk '/inet /{print substr($2,1)}' | awk -F '/' '{print $1}' | tail -n +2 | sort | tr -s '\n' ',' | head -c -1

Is there a way to shorten this command for finding your local ip address?

I am using
ifconfig | grep en0 -A 5 | grep 'inet ' | cut -d ' ' -f 2
to find my system's local ip address. I am certain there is a way to shorten this.
ipconfig getifaddr en0 assuming that en0 is the network interface you're using.
If you aren't sure...
ipconfig getifaddr `route -n get default | grep interface | awk '{print $2}'`
is pretty bulletproof, but loses the conciseness.
Not shorter, but useful too:
echo $(/usr/sbin/arp $(hostname) | awk -F'[()]' '{print $2}')

Get Ports grep using netstat -v

I want to get a list of ports of the established connections using netstat -v grep.
I am trying this:
sudo netstat -v | grep "ESTABLISHED" | cut -d: -f5
Any help?
Try this with $5 and $4:
netstat -v | awk '/ESTABLISHED/ {split($5, array, ":"); print array[2]}'
Please try
netstat -v| grep "ESTABLISHED"| awk '{print $5}' | cut -d ":" -f2

Resources