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条回答
闹够了就滚
2楼-- · 2019-03-08 20:00

This is a quick and dirty way, that works under 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.

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

查看更多
看我几分像从前
3楼-- · 2019-03-08 20:01

Simple Command to fine IP Address with default interface.

ip route | grep src | awk -F 'src' '{print $NF; exit}' | awk '{print $1}'

Tested on All Unix OS

查看更多
再贱就再见
4楼-- · 2019-03-08 20:02

Here a solution to find the current IP address:

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

tested on Mac only.

查看更多
何必那么认真
5楼-- · 2019-03-08 20:09

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?

查看更多
甜甜的少女心
6楼-- · 2019-03-08 20:10

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...

ifconfig en1 | grep inet | grep -v inet6 | cut -d" " -f2
查看更多
男人必须洒脱
7楼-- · 2019-03-08 20:12

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

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̶ ̶

查看更多
登录 后发表回答