Skip to content

Instantly share code, notes, and snippets.

@avtomaton
Created January 18, 2016 18:12
Show Gist options
  • Save avtomaton/e40a88db9df5914eb655 to your computer and use it in GitHub Desktop.
Save avtomaton/e40a88db9df5914eb655 to your computer and use it in GitHub Desktop.
SSH access scripts generator
#!/bin/bash
postfix="0"
username=ec2-user
if [ $# -lt 2 ]; then
echo "Usage: $0 <IP> <key file> [<postfix> [<username>]]"
exit 1
else
if [[ ! $1 =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
echo "IP \"$1\" has incorrect format"
exit 1
fi
echo "IP: $1"
if [ ! -f $2 ]; then
echo "WARNING: key \"$2\" does not exist!"
fi
echo "key file: $2"
ip=$1
key_file=$2
if [ $# -gt 2 ]; then
postfix=$3
fi
if [ $# -gt 3 ]; then
username=$4
fi
file_connect="connect-$postfix.sh"
file_upload="upload-$postfix.sh"
file_download="download-$postfix.sh"
if [ -f $file_connect ]; then
echo "\"$file_connect\" exists, exiting"
exit 2
fi
if [ -f $file_upload ]; then
echo "\"$file_upload\" exists, exiting"
exit 2
fi
if [ -f $file_download ]; then
echo "\"$file_download\" exists, exiting"
exit 2
fi
fi
# connect
echo -e "#!/bin/bash\n\nssh -i $key_file $username@$ip\n" > $file_connect
# upload
echo -e '#!/bin/bash\n\nif [ $# -ne 2 ]; then\n\tdst=`basename $1`\nelse\n\tdst=$2\nfi\n\n' > $file_upload
echo -e "scp -i $key_file -r \$1 $username@$ip:\$2" >> $file_upload
# download
echo -e "scp -i $key_file -r $username@$ip:\$1 \$2" > $file_download
chmod a+x $file_connect
chmod a+x $file_upload
chmod a+x $file_download
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment