如何使用bash shell找到计算机的IP地址?

时间:2022-07-18 07:09:20

Every now and again, I need to start the Django development server, and have it viewable by other machines on my network, as described here:

我一次又一次地需要启动Django开发服务器,让我的网络上的其他机器可以看到它,如下所述:

http://docs.djangoproject.com/en/dev/ref/django-admin/#runserver

http://docs.djangoproject.com/en/dev/ref/django-admin/#runserver

My machine’s IP address tends to change every now and again, so I’d like to have a little shell alias or something that spits out the manage.py command with my machine’s current IP address, maybe like this:

我的机器的IP地址往往会一次又一次地改变,所以我想要一个小的别名或用我的机器的当前IP地址吐出manage.py命令的东西,可能是这样的:

python manage.py runserver $(COMMAND TO FIND MY MACHINE’S IP ADDRESS GOES HERE):8000

14 个解决方案

#1


47  

ifconfig en0 | grep inet | grep -v inet6

Output of above is expected to be in the following form:

预计上述产出将采用以下形式:

inet 192.168.111.1 netmask 0xffffff00 broadcast 192.168.111.255

inet 192.168.111.1 netmask 0xffffff00 broadcast 192.168.111.255

Add an awk statement to print the second column to avoid using cut (awk is a pretty standard unix tool):

添加一个awk语句来打印第二列以避免使用cut(awk是一个非常标准的unix工具):

ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}'

I use the following to get the current IP when on a LAN where the first few numbers of the IP are always the same (replace 192.168.111 with your own numbers):

我使用以下内容获取当前IP,当在IP的前几个数字始终相同的LAN(用您自己的数字替换192.168.111)时:

ifconfig | grep 192.168.111 | awk '{print $2}'

To get the ip of another machine that you know the name of, try (replace hostname and 192.168.111 with your own values):

要获取另一台知道其名称的计算机的ip,请尝试(将hostname和192.168.111替换为您自己的值):

ping -c 1 hostname | grep 192.168.11 | grep 'bytes from' | awk '{print $4}' | sed 's/://g'

#2


15  

You might already be aware, but running

您可能已经意识到,但正在运行

python manage.py runserver 0.0.0.0:8000

makes your machine visible to everyone on the network.

使您的机器对网络上的每个人都可见。

Is there a reason you'd need to specify your IP?

您是否需要指定IP?

#3


7  

Thi should work as well as other commands I've already seen:

这应该和我已经看过的其他命令一样好用:

ifconfig eth0 | grep inet | awk '{print $2}' | cut -d':' -f2
  • Replace eth0 with the desired interface (eth0, eth1, wlan0...)
  • 将eth0替换为所需的接口(eth0,eth1,wlan0 ......)

I̶ ̶t̶h̶i̶n̶k̶ ̶y̶o̶u̶ ̶c̶a̶n̶ ̶a̶l̶s̶o̶ ̶w̶r̶i̶t̶e̶:̶

I̶̶t̶h̶i̶n̶k̶̶y̶o̶u̶̶c̶a̶n̶̶a̶l̶s̶o̶̶w̶r̶i̶t̶e̶:̶

̶ ̶ ̶ ̶h̶o̶s̶t̶n̶a̶m̶e̶ ̶-̶I̶ ̶|̶ ̶c̶u̶t̶ ̶-̶d̶'̶ ̶'̶ ̶-̶f̶1̶ ̶

̶̶̶̶h̶o̶s̶t̶n̶a̶m̶e̶̶-̶I̶̶|̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶

#4


5  

The best solution would be to:

最好的解决方案是:

ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'

#5


4  

Folks are using character counts to pull the right columns from the ip address line, but using spaces as a delim makes this more scalable to different length ip addresses...

人们正在使用字符计数来从ip地址行中拉出正确的列,但是使用空格作为delim使得这更加可扩展到不同长度的ip地址......

ifconfig en1 | grep inet | grep -v inet6 | cut -d" " -f2

#6


4  

This is a quick and dirty way, that works under OSX

这是一种快速而肮脏的方式,可以在OSX下运行

/sbin/ifconfig | grep 'inet ' | grep -v '127.0.0.1' | head -n1 | awk '{print $2}'

Basically get all interfaces with an IPV4 address, skip localhost and then get the first interface.

基本上使用IPV4地址获取所有接口,跳过localhost然后获取第一个接口。

Also, use path to ifconfig. I have seen to many shell script brake when used from ex. cron because of PATH failure.

另外,使用ifconfig的路径。从ex开始使用时,我看到很多shell脚本制动器。 cron因为PATH失败。

#7


3  

ifconfig is probably what you're after. You'll need to either run it through grep to filter out some of the noise though.

ifconfig可能就是你想要的。您需要通过grep运行它来过滤掉一些噪音。

#8


3  

The following command works perfectly for me on RHEL 6.3:

以下命令在RHEL 6.3上完美适用于我:

 ifconfig | grep -v '127.0.0.1' | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'

#9


3  

On Mac OS X 10.11.6 El Capitan (and probably older releases), you can print your IP with

在Mac OS X 10.11.6 El Capitan(可能是旧版本)上,您可以使用打印您的IP

ipconfig getifaddr <device>

where <device> is en0, en1, etc.

其中是en0,en1等。

http://osxdaily.com/2010/11/21/find-ip-address-mac/

http://osxdaily.com/2010/11/21/find-ip-address-mac/

#10


2  

This may not be as elegant as some of the other solutions, but it works on Linux systems and is more comforting to look at than a regex:

这可能不像其他一些解决方案那样优雅,但它适用于Linux系统,并且比正则表达式看起来更令人欣慰:

ifconfig eth0 | grep 'inet addr:' | awk '{print $2}' | awk -F ':' '{print $2}'

#11


1  

Try this (if you are an Arch user)

试试这个(如果你是Arch用户)

resolveip -s $HOSTNAME

Alternative

替代

For getting IPv4 adress you can use:

要获得IPv4地址,您可以使用:

host $(uname -n) | grep "address" | grep -v "IPv6" | head -n 1 | awk '{print $4}'

For getting IPv6 one:

为了获得IPv6:

host $(uname -n) | grep "IPv6 address" | head -n 1 | awk '{print $5}'

You can replace $(uname -n) with $(hostname) if you'd like.

如果您愿意,可以用$(hostname)替换$(uname -n)。

#12


0  

Here a solution to find the current IP address:

这是一个找到当前IP地址的解决方案:

route -n get default|grep interface|awk ‘{ print $2 }’|xargs ipconfig getifaddr

tested on Mac only.

仅在Mac上测试过。

#13


0  

This checks your default interface, and use that to retrieve your primary ip. Helpful when you have many interfaces, and tons of virtual ip addresses:

这将检查您的默认界面,并使用它来检索您的主IP。有很多接口和大量虚拟IP地址时很有用:

netstat -rn | gawk '/UG/ {print $NF}' | xargs ifconfig | gawk 'match($0,/inet addr:(.*) B/,a) {print a[1]}'

#14


0  

Recap: if you'd like to copy/paste to command line and just get the network IP address, here's what works on Mac (on WiFi):

回顾一下:如果您想复制/粘贴到命令行并获取网络IP地址,这里适用于Mac(在WiFi上):

echo $(ifconfig en0 | grep inet | grep -v inet6 | cut -d" " -f2)

Here is assigning it to bash script variable:

这里将它分配给bash脚本变量:

LOCAL_IP=$(ifconfig en0 | grep inet | grep -v inet6 | cut -d" " -f2)

This is based on answers by Valentin Rocher and Matt Kropmton

这是基于Valentin Rocher和Matt Kropmton的回答

#1


47  

ifconfig en0 | grep inet | grep -v inet6

Output of above is expected to be in the following form:

预计上述产出将采用以下形式:

inet 192.168.111.1 netmask 0xffffff00 broadcast 192.168.111.255

inet 192.168.111.1 netmask 0xffffff00 broadcast 192.168.111.255

Add an awk statement to print the second column to avoid using cut (awk is a pretty standard unix tool):

添加一个awk语句来打印第二列以避免使用cut(awk是一个非常标准的unix工具):

ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}'

I use the following to get the current IP when on a LAN where the first few numbers of the IP are always the same (replace 192.168.111 with your own numbers):

我使用以下内容获取当前IP,当在IP的前几个数字始终相同的LAN(用您自己的数字替换192.168.111)时:

ifconfig | grep 192.168.111 | awk '{print $2}'

To get the ip of another machine that you know the name of, try (replace hostname and 192.168.111 with your own values):

要获取另一台知道其名称的计算机的ip,请尝试(将hostname和192.168.111替换为您自己的值):

ping -c 1 hostname | grep 192.168.11 | grep 'bytes from' | awk '{print $4}' | sed 's/://g'

#2


15  

You might already be aware, but running

您可能已经意识到,但正在运行

python manage.py runserver 0.0.0.0:8000

makes your machine visible to everyone on the network.

使您的机器对网络上的每个人都可见。

Is there a reason you'd need to specify your IP?

您是否需要指定IP?

#3


7  

Thi should work as well as other commands I've already seen:

这应该和我已经看过的其他命令一样好用:

ifconfig eth0 | grep inet | awk '{print $2}' | cut -d':' -f2
  • Replace eth0 with the desired interface (eth0, eth1, wlan0...)
  • 将eth0替换为所需的接口(eth0,eth1,wlan0 ......)

I̶ ̶t̶h̶i̶n̶k̶ ̶y̶o̶u̶ ̶c̶a̶n̶ ̶a̶l̶s̶o̶ ̶w̶r̶i̶t̶e̶:̶

I̶̶t̶h̶i̶n̶k̶̶y̶o̶u̶̶c̶a̶n̶̶a̶l̶s̶o̶̶w̶r̶i̶t̶e̶:̶

̶ ̶ ̶ ̶h̶o̶s̶t̶n̶a̶m̶e̶ ̶-̶I̶ ̶|̶ ̶c̶u̶t̶ ̶-̶d̶'̶ ̶'̶ ̶-̶f̶1̶ ̶

̶̶̶̶h̶o̶s̶t̶n̶a̶m̶e̶̶-̶I̶̶|̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶

#4


5  

The best solution would be to:

最好的解决方案是:

ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'

#5


4  

Folks are using character counts to pull the right columns from the ip address line, but using spaces as a delim makes this more scalable to different length ip addresses...

人们正在使用字符计数来从ip地址行中拉出正确的列,但是使用空格作为delim使得这更加可扩展到不同长度的ip地址......

ifconfig en1 | grep inet | grep -v inet6 | cut -d" " -f2

#6


4  

This is a quick and dirty way, that works under OSX

这是一种快速而肮脏的方式,可以在OSX下运行

/sbin/ifconfig | grep 'inet ' | grep -v '127.0.0.1' | head -n1 | awk '{print $2}'

Basically get all interfaces with an IPV4 address, skip localhost and then get the first interface.

基本上使用IPV4地址获取所有接口,跳过localhost然后获取第一个接口。

Also, use path to ifconfig. I have seen to many shell script brake when used from ex. cron because of PATH failure.

另外,使用ifconfig的路径。从ex开始使用时,我看到很多shell脚本制动器。 cron因为PATH失败。

#7


3  

ifconfig is probably what you're after. You'll need to either run it through grep to filter out some of the noise though.

ifconfig可能就是你想要的。您需要通过grep运行它来过滤掉一些噪音。

#8


3  

The following command works perfectly for me on RHEL 6.3:

以下命令在RHEL 6.3上完美适用于我:

 ifconfig | grep -v '127.0.0.1' | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'

#9


3  

On Mac OS X 10.11.6 El Capitan (and probably older releases), you can print your IP with

在Mac OS X 10.11.6 El Capitan(可能是旧版本)上,您可以使用打印您的IP

ipconfig getifaddr <device>

where <device> is en0, en1, etc.

其中是en0,en1等。

http://osxdaily.com/2010/11/21/find-ip-address-mac/

http://osxdaily.com/2010/11/21/find-ip-address-mac/

#10


2  

This may not be as elegant as some of the other solutions, but it works on Linux systems and is more comforting to look at than a regex:

这可能不像其他一些解决方案那样优雅,但它适用于Linux系统,并且比正则表达式看起来更令人欣慰:

ifconfig eth0 | grep 'inet addr:' | awk '{print $2}' | awk -F ':' '{print $2}'

#11


1  

Try this (if you are an Arch user)

试试这个(如果你是Arch用户)

resolveip -s $HOSTNAME

Alternative

替代

For getting IPv4 adress you can use:

要获得IPv4地址,您可以使用:

host $(uname -n) | grep "address" | grep -v "IPv6" | head -n 1 | awk '{print $4}'

For getting IPv6 one:

为了获得IPv6:

host $(uname -n) | grep "IPv6 address" | head -n 1 | awk '{print $5}'

You can replace $(uname -n) with $(hostname) if you'd like.

如果您愿意,可以用$(hostname)替换$(uname -n)。

#12


0  

Here a solution to find the current IP address:

这是一个找到当前IP地址的解决方案:

route -n get default|grep interface|awk ‘{ print $2 }’|xargs ipconfig getifaddr

tested on Mac only.

仅在Mac上测试过。

#13


0  

This checks your default interface, and use that to retrieve your primary ip. Helpful when you have many interfaces, and tons of virtual ip addresses:

这将检查您的默认界面,并使用它来检索您的主IP。有很多接口和大量虚拟IP地址时很有用:

netstat -rn | gawk '/UG/ {print $NF}' | xargs ifconfig | gawk 'match($0,/inet addr:(.*) B/,a) {print a[1]}'

#14


0  

Recap: if you'd like to copy/paste to command line and just get the network IP address, here's what works on Mac (on WiFi):

回顾一下:如果您想复制/粘贴到命令行并获取网络IP地址,这里适用于Mac(在WiFi上):

echo $(ifconfig en0 | grep inet | grep -v inet6 | cut -d" " -f2)

Here is assigning it to bash script variable:

这里将它分配给bash脚本变量:

LOCAL_IP=$(ifconfig en0 | grep inet | grep -v inet6 | cut -d" " -f2)

This is based on answers by Valentin Rocher and Matt Kropmton

这是基于Valentin Rocher和Matt Kropmton的回答