Skip to content

Instantly share code, notes, and snippets.

@w2ak
Created October 16, 2017 14:42
Show Gist options
  • Save w2ak/cf307845e554ac713723dbe125f58bdd to your computer and use it in GitHub Desktop.
Save w2ak/cf307845e554ac713723dbe125f58bdd to your computer and use it in GitHub Desktop.
SSH script to connect to ENST. ./ssh.sh -h for help.
#!/bin/sh
VNET_USER=""
TELECOM_USER=""
TELECOM_KEY=""
TELECOM_CONFIG=1
PROG="$(readlink -m $0)"
usage () {
cat << EOF >&2
usage: $PROG [config|enst|vnetXX|any|ROOM]
config: will setup the script
enst: ssh connection to ssh.enst.fr
vnetXX: ssh connection to vnetXX.r2.enst.fr
any: ssh connection to any machine in telecom
ROOM: ssh connection to ROOM.enst.fr
EOF
exit 1
}
do_script_config () {
if [ $TELECOM_CONFIG -gt 0 ]; then
echo -n "Télécom username: "
read TELECOM_USER
echo -n "Generate SSH key ? (y/n) "
read dogenkey
if [ "$dogenkey" != "n" ]; then
TELECOM_KEY="$PROG.key"
ssh-keygen -q -b 521 -t ecdsa -N "" -C "$TELECOM_USER" -f "$TELECOM_KEY"
else
echo -n "SSH key path: "
read TELECOM_KEY
fi
echo -n "VNET user [acn]: "
read VNET_USER
if [ -z "$VNET_USER" ]; then
VNET_USER=acn
fi
sed -i -e '2 s%=.*$%="'$VNET_USER'"%; 3 s%=.*$%="'$TELECOM_USER'"%; 4 s%=.*$%="'$TELECOM_KEY'"%; 5 s%=.*$%=0%' $PROG
fi
}
test_script_config () {
if [ $TELECOM_CONFIG -gt 0 ] || [ ! -f "$TELECOM_KEY" ] || [ ! -f "$TELECOM_KEY.pub" ]; then
cat << EOF >&2
You need to setup the script. Run it again with 'config'.
usage: $PROG config
EOF
sed -i -e '5 s%=.*$%=1%' $PROG
exit 1
fi
}
connect_to_enst () {
test_script_config
ssh -oUser="$TELECOM_USER" -oIdentityFile="$TELECOM_KEY" ssh.enst.fr $@
}
copyid_to_enst () {
test_script_config
ssh-copy-id -oUser="$TELECOM_USER" -i "$TELECOM_KEY.pub" ssh.enst.fr
}
proxy_to_enst () {
test_script_config
ssh -oUser="$TELECOM_USER" -oIdentityFile="$TELECOM_KEY" -W $@ ssh.enst.fr
}
connect_to_any () {
test_script_config
host="$1.enst.fr"
port="22"
ssh -oUser="$TELECOM_USER" -oIdentityFile="$TELECOM_KEY" -oProxyCommand="$PROG proxy-enst $host:$port" $host
}
connect_to_firstroom () {
test_script_config
host="$(findroom).enst.fr"
port="22"
ssh -oUser="$TELECOM_USER" -oIdentityFile="$TELECOM_KEY" -oProxyCommand="$PROG proxy-enst $host:$port" $host
}
proxy_to_any () {
test_script_config
host="$(findroom).enst.fr"
port="22"
ssh -oUser="$TELECOM_USER" -oIdentityFile="$TELECOM_KEY" -oProxyCommand="$PROG proxy-enst $host:$port" -W $@ $host
}
connect_to_vnet () {
test_script_config
host="$1.r2.enst.fr"
port="22"
ssh -oUser="$VNET_USER" -oIdentityFile="$TELECOM_KEY" -oProxyCommand="$PROG proxy-any $host:$port" $host
}
findroom () {
output=$(mktemp)
{
cat << EOF | $0 enst bash 2>/dev/null >$output
room_ip () {
ip=\$(host \$1.enst.fr)
if [ \$? -gt 0 ]; then
return 1;
else
echo \$ip | awk '{ print \$NF }'
return 0;
fi
}
room_reach () {
ping -c1 -W1 \$1.enst.fr 2>/dev/null >/dev/null
}
try_to_reach () {
if ip=\$(room_ip \$1); then
if room_reach \$1; then
echo \$1
else
echo >&2 \$1 off
fi
else
echo >&2 \$1 no
fi
}
list_rooms () {
for room in \`seq 120 130\`; do
room=\$(printf %02d \$room);
for comp in \`seq 0 50\`; do
comp=\$(printf %02d \$comp);
echo c\$room-\$comp;
done;
done;
}
allreach () {
list_rooms | sort -R |
while read room; do
try_to_reach "\$room" &
done
}
allreach
EOF
} &
pid=$!
tailf $output | sed '1 q'
kill -9 $pid
rm -rf $output
}
if [ $# -gt 0 ]; then
arg=$1; shift
case "$arg" in
"-h") usage;;
"config") do_script_config; $PROG copy-id;;
"enst") connect_to_enst $@;;
"copy-id") copyid_to_enst;;
"proxy-enst") proxy_to_enst $@;;
"findany") findroom;;
"any") connect_to_firstroom;;
"proxy-any") proxy_to_any $@;;
"vnet"*) connect_to_vnet $arg $@;;
*) connect_to_any $arg $@;;
esac
else
usage;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment