PDA

View Full Version : determine ip in applescript




raeserbil
Aug 17, 2013, 04:37 AM
I have a appelscript that upon startup mount my fileserver as a disk. The problem is that my router does not support nat-loopback, so for the script to be working both when I'm at home on my lan and when I'm outside I need to check "something" and then connect to either 192.168.0.101 (if I'm at home) or to myserver.myftp.org (if I'm outside). Of course, the best solution would be to get a router that supports nat-loopback so I could use myserver.myftp.org all the time but right now that's not an option.

Anyone that can help me with the problem? I guess I just need an if/else condition but what should I check to determine if I'm on my lan or outside?



kryten2
Aug 17, 2013, 10:22 AM
Info : http://stackoverflow.com/questions/11738353/get-return-value-of-a-command-with-applescript-to-get-ip-address-and-use-as-a-s

heyadrian
Aug 19, 2013, 12:56 AM
If the IP subnets are different or the full hostnames are different, you could use either:

you could get your IPv4 address by:

set myIP to IPv4 address of (get system info)

or hostname by:

set myHostName to host name of (get system info)

ratsg
Aug 19, 2013, 12:32 PM
I'm not sure what you are referring to as a nat-loopback is, but FWIW, I use this script to determine my IP address. Hope this helps:

........................

715 % cat whatsmypublicip.csh
#!/bin/csh

/bin/curl -s http://checkip.dyndns.org | awk '{print $6}' | awk ' BEGIN { FS = "<" } { print $1 } '

716 %

mwb
Aug 19, 2013, 05:35 PM
Although you asked how to find your IP address, your connected WiFi SSID might be pretty useful too.

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'

So now you have two tests: you are probably on your home network if
1. Your public facing IP address is the same as your home's public facing IP address (ratsg's post), or
2. Your currently connected WiFi SSID is the name of your home network.

raeserbil
Aug 21, 2013, 02:15 PM
Thanks for the help. I solved it by using:


set myName to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'"

if myName = "mySSID" then
…connect to 192.168.0.101
else
…connect to myserver.myftp.org
end if