如何使用bash shell找到我的电脑的IP地址? [英] How do I find my computer's IP address using the bash shell?

查看:181
本文介绍了如何使用bash shell找到我的电脑的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每隔一段时间,我需要启动Django开发服务器,并可以在我的网络上由其他机器查看,如下所述:

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:

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

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

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:

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


推荐答案

ifconfig en0 | grep inet | grep -v inet6

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

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

inet 192.168.111.1网络掩码0xffffff00广播192.168.111.255

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

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

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

我在LAN上使用以下内容获取当前的IP IP的前几个数字总是相同的(用你自己的数字替换192.168.111):

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):

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

要获取您知道名称的另一台机器的ip,尝试(用你自己的值替换主机名和192.168.111):

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

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

这篇关于如何使用bash shell找到我的电脑的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆