Three methods to obtain the local ip address in shell script

This article mainly introduces the three methods of obtaining the local ip address in the shell script. The implementation code is directly given below.

method one:

/sbin/ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d "addrs"

/sbin/ifconfig | sed -n '/inet addr/s/^[^:]*:\([0-9.]\{7,15\}\).*/\1/p' | grep -v 127.0.0.1



Method Two:

local_host="`hostname --fqdn`"
local_ip=`host $local_host 2>/dev/null | awk '{print $NF}'`


 

Method three:

nslookup -sil $local_host 2>/dev/null | grep Address: | grep -v "127.0.0.1" | awk '{print $2}' | awk -F '#' '{print $1}'


        PS: nslookup is a command-line tool that monitors whether DNS servers in the network can correctly implement domain name resolution.

 

P.S. A command to kill a process

View the process number of the project:
ps -efww | grep item name | grep -v grep | grep -v less | awk '{print $2}'

kill project:
kill `ps -efww | grep project name | grep -v grep | grep -v less | awk '{print $2}'`

 

Reference article: http://www.jb51.net/article/56585.htm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326313969&siteId=291194637