Get local IP address [LAN]

BlitzMax Forums/BlitzMax Programming/Get local IP address [LAN]

ghislain(Posted 2008) [#1]
Hi !

I would like to get the IP number of my computer. Not the IP number of my internet-connection [WAN] but just the number within the LAN.
Eg: 192.168.1.10 or something like that.

There is this system command [OsX] 'ifconfig' which shows me the info i need but when I use
system_("ifconfig")
I'm unable to catch the output..

Any help?

Ghislain
MacIntel - OsX 10.5


Perturbatio(Posted 2008) [#2]
Local ips:Int[] = HostIps("")

For Local i:Int = EachIn ips
	Print DottedIP(i)
Next



ghislain(Posted 2008) [#3]
Hi Perturbatio,

On my mac - it doesn't produce anything...

Ghislain
MacIntel - OsX 10.5


xlsior(Posted 2008) [#4]
Perturbatio's snippet ought to work...

But as far as capturing the output of your system_ coimmand, you should be able to do something like this:

system_("ifconfig > myip.txt"


to write it to a file named myip.txt ... Although obviously using the native commands are much preferred over workarounds like that.


Winni(Posted 2008) [#5]
ghislain, use the name of your computer plus ".local", that works for me on Leopard:

Local ips:Int[] = HostIps("dr-darcenstein.local")

Print Len(ips)
For Local i:Int = EachIn ips
	Print DottedIP(i)
Next




xlsior(Posted 2008) [#6]
ghislain, use the name of your computer plus ".local", that works for me on Leopard:


Except... How do you obtain the name of your computer?

It does sound like a bug if that is required on the Mac but not on Windows...


Dreamora(Posted 2008) [#7]
Not really.
Apples are just no Windows, that simple it is ...

MS has a quite different approach to handle that than Apple has (as Linux has another one again). If you are lucky your approach at least works on OSX 10.4 and 10.5 and not only on the one or the other ...


ghislain(Posted 2008) [#8]
Ok both examples work! Great!

Local ips:int[] = HostIps ("Ghis-iMac.local")

and
system_("hostname > myHostname.txt") ' retrieve HostName
system_("ifconfig > myIp.txt") ' retrieve IpAddress


Can I only redirect the system_ output to a file?
It would be most convenient if I can capture it directly to a string...

Ghislain
MacIntel - OsX 10.5


xlsior(Posted 2008) [#9]
Can I only redirect the system_ output to a file?
It would be most convenient if I can capture it directly to a string...


Don't know... Perhaps you can do something with stdout / stdin, but no idea on how to do that... Especially on a Mac.


FlameDuck(Posted 2008) [#10]
It would be most convenient if I can capture it directly to a string...
Use FreeProcess?


ghislain(Posted 2008) [#11]
Hi FlameDuck,

Any code available?

For your info:
Import pub.freeprocess

Global myProcess:TProcess = TProcess.Create("ifconfig", HIDECONSOLE)

While (myProcess <> Null) And myProcess.Status()
	Print myProcess.pipe.ReadLine$()
Wend

myProcess.Terminate

Doesn't produce any output..
Neither do the various pipe-examples on the forum..

Ghislain
MacIntel - OsX 10.5


FlameDuck(Posted 2008) [#12]
Any code available?
I don't have any here, but the IDE uses it IIRC.


grable(Posted 2008) [#13]
Doesn't produce any output..

Try this:
?Win32
Local proc:TProcess = TProcess.Create( "ipconfig", HIDECONSOLE)
?MacOS
Local proc:TProcess = TProcess.Create( "ifconfig", HIDECONSOLE)
?Linux
Local proc:TProcess = TProcess.Create( "ifconfig", HIDECONSOLE)
?

Local stream:TStream = TTextStream.Create( proc.Pipe, TTextStream.UTF8)
While proc.Status()	
	If proc.pipe.ReadAvail() Then Print stream.ReadLine()
Wend



ghislain(Posted 2008) [#14]
Nothing happens.
It only shows 'Process complete".

Ghislain
MacIntel - OsX 10.5


Dreamora(Posted 2008) [#15]
welcome to the world of "how to break compatibility", well known as Apple OSX :)
There surely is a trick behind it. Either you need to execute it through elevated rights, or it got replaced by something else.