Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Linux shell script to get the IP address of public network

Shulou Source: shulou.com Published: 2022-06-01 15:51:00 10月03日 Update

This article introduces the knowledge of "how to use Linux shell script to obtain public network IP address". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

A shell script based on Linux system to obtain the IP address of public network. The content of the script is as follows: getIp.sh

#! / bin/bash# checks the legality of IP check_ipaddr () {echo $1 | grep "^ [0-9]\ {1pr 3\}\.\ ([0-9]\ {1pr 3\}\.\)\ {2\} [0-9]\ {1Med 3\} $" > / dev/null;if [$?-ne 0] then#echo "IP address must be all numeric" return 1fiipaddrature address 1a = `echo $ipaddr | awk-F. '{print $1}' `# to "." Separate and take out the value of each column b = `echo $ipaddr | awk-F. '{print $2}' `c = `echo $ipaddr | awk-F. '{print $3}' `d = `echo $ipaddr | awk-F. '{print $4}' `for num in $a $b $c $ddoif [$num-gt 255] | | [$num-lt 0] # each value must be in then#echo $ipaddr between 0-255. the field "$num" error "return 1fidone#echo $ipaddr" address is legal "return 0} host=ns1.dnspod.netport=16666ip= `cat / tcp/$host/$ port`check _ ipaddr" $ip "if [" $? "" x = "0" x]; thenecho "public network IP address: $ip" elseecho "failed to get IP address!" Fi

The output can be executed after authorizing the script with the command chmod + x getIp.sh.

This is the end of the content of "how to use the Linux shell script to get the IP address of the public network". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Address script content legal more knowledge output practical learned next between legality commands dilemmas fields actual situation numeric numeric article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Microsoft Huawei NVidia