One-click to get Linux server network IP address Ultimate Shell Script

Efficiency is what you pursue, efficiency is what you
pursue—— Former owner leader


It's not so much a blog, it's a shell script that arrived 6 months late.
In August 2021, the leader asked me to write a script to get the IP address of the physical machine accurately
. Today, it has finally been realized, commemorate

First look at the effect

1

Let's take a look at the script content

2


Open source with everyone, share a ha, welcome one-click three-connection (like , follow , favorite ) yo!

#!/bin/bash
# Author: IT-coding
# Date: 2022-02-23 21:22
# blog: https://qchenz.blog.csdn.net/

pkgs(){
    
    
    yum install iproute iproute2 net-tools hostname -y
}

netAdapter(){
    
    
    hw=`ip a|grep -v "1:"|grep -v "inet6"|grep "2:"|awk '{print $2}'`
    hw2=${hw%:*}
}

getIpWay1(){
    
    
    echo " == get local IP Way1 == "
    net1=`ip a|grep "${hw2}"|grep "inet"|awk '{print $2}'`
    echo ${net1%/*}
}

getIpWay2(){
    
    
    echo " == get local IP Way2 == "
    net2=`ifconfig ${
     
     hw2}|grep "inet"|grep -v "inet6"|awk '{print $2}'`
    echo ${net2}
}

getIpWay3(){
    
    
    echo " == get local IP Way3 == "
    net3=`hostname -I|awk '{print $1}'`
    echo ${net3}
}

main(){
    
    
    echo "=== LET'S BEGIN GETTING LOCAL IP ADDRESS... ==="
    pkgs
    netAdapter
    getIpWay1
    getIpWay2
    getIpWay3
    echo "=== GOT LOCAL IP ADDRESS FINISHED! ==="
}

main

Friends, have you lost your studies?
See you next time, bye!

Guess you like

Origin blog.csdn.net/frdevolcqzyxynjds/article/details/123099826