How do I find my computer's IP address using t

2019-03-08 19: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:

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:

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

15条回答
We Are One
2楼-- · 2019-03-08 20:23

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:

netstat -rn | gawk '/UG/ {print $NF}' | xargs ifconfig | gawk 'match($0,/inet addr:(.*) B/,a) {print a[1]}'
查看更多
3楼-- · 2019-03-08 20:24
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

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}'

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}'

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'
查看更多
等我变得足够好
4楼-- · 2019-03-08 20:27

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

查看更多
登录 后发表回答