views:

1910

answers:

5

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
+1  A: 

This seems to work on the bash shell on OS X 10.5.6:

ifconfig | grep 'inet ' | grep -v '127.0.0.1' | cut -c 7-17

Adapted from here: http://www.cyberciti.biz/tips/read-unixlinux-system-ip-address-in-a-shell-script.html

Paul D. Waite
Doesn't work if you have multiple interfaces.
Paul Tomblin
+3  A: 

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

James Gregory
+6  A: 

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?

Joey Robert
That sounds great, although it doesn’t seem to work on my network here (other machines don’t seem to be able to see 0.0.0.0).
Paul D. Waite
No, you still connect to your regular IP address. So for example if you run that command on your machine, and your IP is 192.168.0.100, the machines on your network still access your machine through 192.168.0.100. Putting in 0.0.0.0 just automates the task rather than looking for the IP. It is what you want.
Joey Robert
+5  A: 

I would say by using ifconfig, but to me the command would look a little bit more like that :

ifconfig [interface name] | grep inet | grep -v inet6 | cut -d ":" -f 2 | cut -d " " -f 1

considering you know which interface you need, and you want the ipv4 version, not the ipv6.

Valentin Rocher
Aha, gotcha, that looks like a nice way to do it.
Paul D. Waite
I amended the `cut` bit a little, as that wasn’t working for me. I’ve got `ifconfig en1 | grep inet | grep -v inet6 | cut -c 7-17`
Paul D. Waite
Yeah, I changed the cut because cutting on character placement didn't seem so reliable to me.
Valentin Rocher
A: 

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.

Mat