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条回答
Animai°情兽
2楼-- · 2019-03-08 20:12

The best solution would be to:

ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'
查看更多
等我变得足够好
3楼-- · 2019-03-08 20:12

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:

ifconfig eth0 | grep 'inet addr:' | awk '{print $2}' | awk -F ':' '{print $2}'
查看更多
Summer. ? 凉城
4楼-- · 2019-03-08 20:17

Try this (if you are an Arch user)

resolveip -s $HOSTNAME

Alternative

For getting IPv4 adress you can use:

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

For getting IPv6 one:

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

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

查看更多
你好瞎i
5楼-- · 2019-03-08 20:17

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

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

Here is assigning it to bash script variable:

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

This is based on answers by Valentin Rocher and Matt Kropmton

查看更多
够拽才男人
6楼-- · 2019-03-08 20:19

The following command works perfectly for me on RHEL 6.3:

 ifconfig | grep -v '127.0.0.1' | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'
查看更多
Melony?
7楼-- · 2019-03-08 20:21

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

ipconfig getifaddr <device>

where <device> is en0, en1, etc.

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

查看更多
登录 后发表回答