c u t u p @ c u t u p . o r g


[bash ssh bashrc] last modified: 03/25/2007 02:01 pm

If you have a server at home or on a wireless network you frequent, you'd connect to it with an internal IP. But off the LAN, you connect with its domain name or external IP. Here's a simple and obvious function for your .bashrc that figures out where you are and connects accordingly. If defaults to your username but allows you to specify another.

Pick the function for your OS, substitute the stuff in brackets, add a port if you need to, and you'll probably rename the function to something better than sssh, like the server name or something.

# smart ssh for linux
function sssh() {
 WLAN=$(/sbin/iwconfig 2>/dev/null | awk '/ESSID/{split($4,a,"\"");print a[2]}')
 [ $1 ] && local AT='@'
 [ "$WLAN" == "<wireless_ssid>" ] && ssh $1$AT$<internal_ip> || ssh $1$AT<domain_name>

# smart ssh for mac
function sssh() {
 WLAN=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print $2}')
 [ $1 ] && local AT='@'
 [ "$WLAN" == "<wireless_ssid>" ] && ssh $1$AT<internal_ip> || ssh $1$AT<domain_name>

Then you connect with sssh [username] wherever you are and it figures itself out.

Yeah, it's obvious but it's not like I can claim I thunk of it on my own.

[All Posts] [top]