determine ip in applescript

Discussion in 'Mac Programming' started by raeserbil, Aug 17, 2013.

  1. macrumors newbie

    Joined:
    Jul 3, 2013
    Location:
    Sweden
    #1
    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?
     
  2. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
  3. macrumors member

    Joined:
    Aug 14, 2011
    #3
    If the IP subnets are different or the full hostnames are different, you could use either:

    you could get your IPv4 address by:

    Code:
    set myIP to IPv4 address of (get system info)
    or hostname by:

    Code:
    set myHostName to host name of (get system info)
     
  4. macrumors 6502

    Joined:
    Dec 6, 2010
    #4
    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 %
     
  5. mwb
    macrumors newbie

    Joined:
    Jul 21, 2011
    #5
    Although you asked how to find your IP address, your connected WiFi SSID might be pretty useful too.

    Code:
    /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.
     
  6. thread starter macrumors newbie

    Joined:
    Jul 3, 2013
    Location:
    Sweden
    #6
    Thanks for the help. I solved it by using:

    Code:
    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
     

Share This Page