Skip to content

Instantly share code, notes, and snippets.

@phillymjs
Last active March 30, 2017 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillymjs/cdab4c4aedc98fe3b1113f0484971c47 to your computer and use it in GitHub Desktop.
Save phillymjs/cdab4c4aedc98fe3b1113f0484971c47 to your computer and use it in GitHub Desktop.
Displays a dialog with the active network port and its IP address, or the IP in use by the Cisco AnyConnect VPN client.
try
--Check the Cisco AnyConnect client state
(do shell script "/opt/cisco/anyconnect/bin/vpn state | grep \"state: Connected\"") as string
--If we get here then we're connected, so return the Cisco IP information
set activeNetworkPortName to "Cisco AnyConnect VPN"
set currentIP to do shell script "/opt/cisco/anyconnect/bin/vpn stats | grep \"Client Address (IPv4):\" | awk '{print $4}'"
on error
--If we end up here then there is no VPN connection, therefore we're on an Apple network port, so we need to figure out which one and return the IP
set activeNetworkPortID to do shell script "/sbin/route -n get 0.0.0.0 2>/dev/null | /usr/bin/awk '/interface: / {print $2}'"
set activeNetworkPortName to do shell script "/usr/sbin/networksetup -listnetworkserviceorder | /usr/bin/grep 'Hardware Port' | grep " & activeNetworkPortID & " | /usr/bin/cut -c 17- | /usr/bin/awk 'BEGIN {FS=\",\"}; {print $1}'"
set currentIP to do shell script "/usr/sbin/ipconfig getifaddr " & activeNetworkPortID
end try
set iconpath to (path to startup disk) & "System:Library:PreferencePanes:Network.prefPane:Contents:Resources:Network.icns" as string
display dialog "Connected via " & activeNetworkPortName & "
IP address: " & currentIP buttons {"OK"} default button "OK" with title "Current Network Connection Info" with icon file iconpath
@phillymjs
Copy link
Author

Rewritten to use the AnyConnect "state" command to determine connection state instead of just trying to get the VPN IP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment