博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux 得到内网外网ip
阅读量:5227 次
发布时间:2019-06-14

本文共 1953 字,大约阅读时间需要 6 分钟。

1. Get Internal IP Address(es) on Linux Shell / Command Line

1.1 Get Single IP Address by Interface

Returns plain IP address.

/sbin/ifconfig $1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}' ## Example usage ##/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'10.20.10.1

Create simple bash function (example int-ip) with following command.

function int-ip {
/sbin/ifconfig $1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'; } ## Example usage ##int-ip eth010.20.10.1
 

1.2 Get Every Interfaces IP Address

Returns every interface and IP address pairs.

/sbin/ifconfig |grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }' ## Example output ##eth0: 10.20.10.1eth1: 10.20.1.168lo: 127.0.0.1

Create simple bash function (example int-ips) with following command.

function int-ips {
/sbin/ifconfig |grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'; } ## Example usage ##int-ipseth0: 10.20.10.1eth1: 10.20.1.168lo: 127.0.0.1
 

2. Get External IP Address on Linux Shell / Command Line

I use here whatismyip.org service.

2.1 Get External IP Address Using Lynx

Returns plain IP address.

lynx --dump http://ipecho.net/plain ## Example output ##80.10.10.80

已验证

Create simple bash function (example ext-ip) with following command.

function ext-ip () {
lynx --dump http://ipecho.net/plain; } ## Example usage ##ext-ip80.10.10.80
 

2.2 Get External IP Address Using Curl

Returns plain IP address.

curl http://ipecho.net/plain; echo ## Example output ##80.10.10.80

Create simple bash function (example ext-ip) with following command.

function ext-ip () {
curl http://ipecho.net/plain; echo; } ## Example usage ##ext-ip80.10.10.80

转载于:https://www.cnblogs.com/wangkangluo1/archive/2013/03/18/2967061.html

你可能感兴趣的文章
SpringAop与AspectJ
查看>>
Leetcode 226: Invert Binary Tree
查看>>
http站点转https站点教程
查看>>
解决miner.start() 返回null
查看>>
bzoj 2007: [Noi2010]海拔【最小割+dijskstra】
查看>>
BZOJ 1001--[BeiJing2006]狼抓兔子(最短路&对偶图)
查看>>
C# Dynamic通用反序列化Json类型并遍历属性比较
查看>>
128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
查看>>
定制jackson的自定义序列化(null值的处理)
查看>>
auth模块
查看>>
javascript keycode大全
查看>>
前台freemark获取后台的值
查看>>
log4j.properties的作用
查看>>
游戏偶感
查看>>
Leetcode: Unique Binary Search Trees II
查看>>
C++ FFLIB 之FFDB: 使用 Mysql&Sqlite 实现CRUD
查看>>
Spring-hibernate整合
查看>>
c++ map
查看>>
exit和return的区别
查看>>
js += 含义(小知识)
查看>>