473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Windows getting local ip address

On Linux, it is a simple matter to get the local ip address with
system.os("ifconfig >> /tmp/ip"); ip=open("/tmp/ip").readlines(), etc.
How can I do this with Windows?

Mar 22 '06 #1
5 9541
You can do essentially the same thing substituting "ipconfig" for
ifconfig.

Though I am sure there are better ways....

Mar 22 '06 #2
"SolaFide" wrote:
On Linux, it is a simple matter to get the local ip address with
system.os("ifconfig >> /tmp/ip"); ip=open("/tmp/ip").readlines(), etc.
ip = os.popen("ifconfig").readlines()

is a bit more convenient.
How can I do this with Windows?


the command is called "ipconfig" in windows.

there's also
import socket
socket.gethostbyname(socket.gethostname()) '1.2.3.4' socket.gethostbyname_ex(socket.gethostname()) ('bender.shiny.com', ['bender'], ['1.2.3.4']) socket.getaddrinfo(socket.gethostname(), 0) [(2, 1, 0, '', ('1.2.3.4', 0)), (2, 2, 0, '', ('1.2.3.4', 0))]

etc. if you're behind a firewall/NAT etc and you want your "public IP",
you can do something like:
import re, urllib
ip = urllib.urlopen('http://checkip.dyndns.org').read()
re.search("(\d+\.\d+\.\d+.\d+)", ip).group()

'4.3.2.1'

hope this helps!

</F>

Mar 22 '06 #3
The second solution can give really weird results though, e.g. on my
Linux system I get:
gethostbyaddr(gethostname()) ('linux.site', ['linux'], ['127.0.0.2'])

A more flexible but potentially unportable way would be:
import socket
import fcntl
import struct

def get_ip_address(ifname): .... s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
.... return socket.inet_ntoa(fcntl.ioctl(
.... s.fileno(),
.... 0x8915, # SIOCGIFADDR
.... struct.pack('256s', ifname[:15])
.... )[20:24])
.... get_ip_address('eth0')

'192.168.0.174'

Mar 22 '06 #4
The traditional right way (tm) to do this is to call getsockname() on
the (a?) socket that's connected to the guy you want to tell your
address to. This picks the right address in case you have several. If
you don't have a socket handy, you can make a connectionless UDP
socket and connect() it to a suitable place - this won't result in any
packets on the wire. NAT breaks it of course, but then you couldn't
easily be contacted from outside the NAT anyway.

-- erno
Mar 23 '06 #5
That man is a genius:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("gmail.com",80))
print s.getsockname() ('192.168.0.174', 2768) s.close()


Should work on Windows as well.

Mar 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Hemant Shah | last post by:
Folks, I installed DB2 UDB 8.2 FP8 ESE on Win 2K syatem. I cataloged node and database on AIX system. When I try to connect to the database on windows system from an AIX system I get following...
16
by: Marina | last post by:
Hi, I am trying to find the minimum security settings to allow a windows control embedded in IE have full trust. If I give the entire Intranet zone full trust, this works. However, this is...
17
by: Jon B | last post by:
Hi All! I have a ASP.NET 2.0 site that works on the Windows 2000 Server. However, when I tried to view this site on my local Windows XP machine, I get "Server Unavailable". If I switch the...
8
by: MoshiachNow | last post by:
HI, I try to open a link using %windir% in path: <LI><A HREF="%windir%/temp/MainSetupLog.log">F </A></LI> The browser does not seem to find the file. However,If I paste in the Address window:...
4
by: shashank kadge | last post by:
hi all, i am trying to get local admin users and groups on a windows server. here is the C# code that i am using...
1
by: eblackmo | last post by:
I have a test network consisting of four servers running windows 2003 server R2 SP2. I have set up a domain which functioned correctly for about a day and a half until the other servers decided they...
1
by: rufusking | last post by:
My requirement is to change the IP Address of local system programatically using .net. IP Address should change to 10.1.2.100, subnet mask = 255.255.255.0, default gateway=10.1.2.1 I have...
1
by: sanjeetksingh | last post by:
I am using DB2 version 8 and window xp. I created my local DB2 database on my window xp machine. I want to catalog this local database on our AIX 5.3 server. I tried to do it using the IP address...
6
by: josequinonesii | last post by:
I've searched, I've read, I've tested and re-read numerous post but to no avail yet... Quite simply, the settings I've applied to my httpd.conf, httpd-vhost.conf and my hosts files simply does not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.