Last active
July 30, 2019 12:56
-
-
Save pranavsriram8/98409bdf48b064597156fea0f93014f2 to your computer and use it in GitHub Desktop.
Connect to SSH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
function setupconfig() { | |
echo "Enter the alias name" | |
read aliasName | |
echo "Enter the Full Hostname of the target server" | |
read targetHost | |
nslookup $targetHost | |
if [ $? -eq 0 ];then | |
echo "Host Found" | |
else | |
echo "Check the hostname provided and network path" | |
exit 1; | |
fi | |
echo "Enter the User of the target " | |
read userName | |
echo "Is the is ssh port 22 [y/n]" | |
read ANS | |
if [ $ANS == "Y" ] || [ $ANS == "y" ]; then | |
port=22 | |
else | |
echo "Enter the port " | |
read port | |
fi | |
echo "Is this the key file [~/.ssh/id_rsa][y/N]: " | |
read ANS | |
if [ $ANS == "Y" ] || [ $ANS == "y" ]; then | |
if [ -f ~/.ssh/id_rsa ];then | |
echo "Using default key ~/.ssh/id_rsa" | |
IdentityFile=/home/opc/.ssh/id_rsa | |
fi | |
else | |
echo "Enter the key path " | |
read IdentityFile | |
if [ -f $IdentityFile ];then | |
echo "Setting key $IdentityFile" | |
else | |
echo "Exiting.." | |
exit 1 | |
fi | |
fi | |
echo "Alias: " $aliasName | |
echo "HostName: " $targetHost | |
echo "User: " $userName | |
echo "Port: " $port | |
echo "Key file: $IdentityFile" | |
if [ ! -d ~/.ssh ];then | |
mkdir ~/.ssh | |
fi | |
if [ -f ~/.ssh/config ]; then | |
echo "Host $aliasName" >> ~/.ssh/config | |
echo " HostName $targetHost">> ~/.ssh/config | |
echo " User $userName">> ~/.ssh/config | |
echo " Port $port">> ~/.ssh/config | |
echo " IdentityFile $IdentityFile">> ~/.ssh/config | |
else | |
echo "Host $aliasName" >> ~/.ssh/config | |
echo " HostName $targetHost">> ~/.ssh/config | |
echo " User $userName">> ~/.ssh/config | |
echo " Port $port">> ~/.ssh/config | |
echo " IdentityFile $IdentityFile">> ~/.ssh/config | |
chmod 600 ~/.ssh/config | |
fi | |
ssh $aliasName | |
if [ $? -eq 0 ];then | |
echo "Connected" | |
else | |
echo "Check if correct keys are in place in source and target" | |
fi | |
} | |
function listhosts(){ | |
grep -w Host .ssh/config | |
} | |
function main() { | |
echo "Select option to be performed: | |
1> set up new host information | |
2> list all available hostnames | |
" | |
read ANS | |
case $ANS in | |
1) | |
echo "setting up new host information.." | |
setupconfig $* | |
;; | |
2) | |
echo "listing all hostname in config file" | |
listhosts $* | |
;; | |
*) | |
echo "Wrong Entry!! Bye!!" | |
;; | |
esac | |
} | |
main $*; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment