Register
It is currently Mon Nov 24, 2014 9:05 am

SSH connection Script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Sun Jun 01, 2008 1:58 pm   

Joined: Sun Jun 01, 2008 1:32 pm
Posts: 1
This script was my first dive into bash script I started writing this script several months ago and then finally went back to it today to clean it up and fix some issues. I had a little bit of help from a friend writing the here document.

I have several boxes I SSH into and always get the port numbers mixed up on the box so I wrote this to track all of the boxes I connect to.


Code:
#!/bin/bash
# Delusion's SSH script .5
#
# If you plan on using this script please rememeber to change the path below
#
#Path to Config
conf="/home/bob/.scripts/ssh.conf"



ssh_connect () {
  if [ $1 ] ; then

   validname=$1

   grep -q $validname $conf
   VALID=$?

   while [ "$VALID" != "0"  ]; do
      
      cat $conf | cut -d\  -f1
      echo ""
      read -p "That name is not valid, please try again: " validname
      grep -q $validname $conf
      VALID=$?
   done

   user=`grep $validname $conf | cut -d\  -f2`
   host=`grep $validname $conf | cut -d\  -f3`
   port=`grep $validname $conf | cut -d\  -f4`
   ssh -p $port $user@$host
fi
}

ssh_add () {
   read -p "Enter a Connection name for this connection: " sshname
   read -p "Enter a Username for connection: " sshuser
   read -p "Enter Hostname or Ip for this connection: " sshhost
   read -p "Enter the Port for this connection: " sshport
   while [ "$sshsure" !=  "w" ]; do
   echo "1.) Connection Name: $sshname"
   echo "2.) Username: $sshuser"
   echo "3.) Hostname\IP: $sshhost"
   echo "4.) Port: $sshport"
   read -p "Please Select the option you wish to edit or press [w] to write the data to the file: " sshsure
   case "$sshsure" in
      1)
      read -p "Enter a Connection name for this connection: " sshname
      ;;
      2)
      read -p "Enter a Username for connection: " sshuser
      ;;
      3)
      read -p "Enter Hostname or Ip for this connection: " sshhost
      ;;
      4)
      read -p "Enter the Port for this connection: " sshport
      ;;   

   esac
   done
   sshentry="$sshname $sshuser $sshhost $sshport"
   echo $sshentry >> $conf
}
ssh_delete () {

   sshremove="NULL"

   while [ $sshremove != "c" ]; do
      ssh_list
      read -p "Please select an entry you want to delete or [c] to cancel : " sshremove

      case "$sshremove" in
         c)
         break
         ;;
         [0-9]*)
         if `cat -n $conf | sed s/^\ *//g | grep -q ^"$sshremove"`; then
            read -p "Are you sure [y|n]: " sshure
            if [ $sshure == "y" -o "Y" ]; then
               vim $conf 2>/dev/null <<SSHREMOVEEOF
               :$sshremove
               dd
               ^[
               :wq
SSHREMOVEEOF
            elif [ $sshure == "n" -o $sshure == "N" -o $sshure == "No" -o $sshure == "no" ]; then
               break
            fi
         else
            echo "You have entered an invaild number please try agian"
         fi
         ;;
      esac                  
   done
}
ssh_help () {
   echo ""
   echo " `basename $0` Usage:"
   echo " c - This is command you use to connect. syntax: connect c <connection name>"
   echo " a - This command will let you add a connection entrys"
   echo " d - This command will let you remove a connection entrys"
   echo " l - This command will list all of you connection entrys"
   echo ""
}


ssh_list () {
   count=`cat $conf | wc -l`
        for ((c=1; c <= count ; c++)); do
      echo ""
      echo -e "Connection Entry: `cat -n $conf | sed s/^\ *//g | grep ^"$c" | awk '{ print $1 }'`"
      echo -e "Name: `cat -n $conf | sed s/^\ *//g | grep ^"$c" | awk '{ print $2 }'`"
      echo -e "Username: `cat -n $conf | sed s/^\ *//g | grep ^"$c" | awk '{ print $3 }'`"
      echo -e "Hostname\Ip: `cat -n $conf | sed s/^\ *//g | grep ^"$c" | awk '{ print $4 }'`"
      echo -e "Port: `cat -n $conf | sed s/^\ *//g | grep ^"$c" | awk '{ print $5 }'`"
   done
}


if [ $1 ] ; then
   case "$1" in
      c)
      ssh_connect $2
      ;;
      a)
      ssh_add
      ;;
      h)
      ssh_help   
      ;;
      d)
      ssh_delete
      ;;
      l)
      ssh_list
      ;;
      *)
      ;;
   esac

else
   ssh_help
fi


Top
 Profile  
 PostPosted: Mon Jun 02, 2008 10:53 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
You can also specify options in ~/.ssh/config, and code in the specific parameters there. For example on my work computer I have it set so that I can just type "ssh home" and it will bring me to the right place.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP