Skip to content

Instantly share code, notes, and snippets.

@reimai
Created August 18, 2016 18:49
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 reimai/3e4e96a9db67e1ccdacdf1e8991053fa to your computer and use it in GitHub Desktop.
Save reimai/3e4e96a9db67e1ccdacdf1e8991053fa to your computer and use it in GitHub Desktop.
a warning for people who ssh and switch instead of connection as a dev user right away
#!/bin/bash
#warns a user to log in as dev by a key instead of sudo su-ing every time
function get_ps {
pid=$1
what=$2
echo $(ps -o $what -p $pid h | tr -d ' ')
}
function get_parent {
pid=$1
echo $(ps -o ppid= $pid)
}
pid=$$
dev_name=$(get_ps $$ user)
while [[ $(get_ps $pid cmd) != "sudosu-$dev_name" && $pid != 1 ]] ; do
pid=$(get_parent $pid)
done
if [[ $pid == 1 ]] ; then
exit
fi
user_name=$(get_ps $pid user)
if [[ $user_name != $dev_name ]]; then
gray='\033[0;37m'
green='\033[0;32m'
def='\033[0;39m'
echo
echo -e "Did you know you could add your ssh public key to ${green}~/.ssh/authorized_keys${def} (or ${green}~/.ssh/authorized_keys2${def} if you don't have an access to the previous one)"
echo "and log in without switching user to $dev_name every time?"
echo
echo "You could also set up a master session to log in just one time, just add this totally non-malicious entry to your local ssh config:"
echo -e "
${gray}#reuse existing ssh connection${def}
${green}Host *
ControlMaster auto
ControlPath ~/.ssh/master-%r@%h:%p${def}"
echo
echo -e "If you don't like this notification and prefer to sudo su, just do"
echo "
touch ~/.hush_sshwarn"
echo
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment