myssh – an interactive ssh management tool

Inspired by the putty manager on Windows system, I have spent the whole afternoon to write ‘myssh’ – an interactive ssh management tool providing the ability to save, display, choose and connect your ssh connection automatically. I am not sure if there is any kind of GUI related stuff on Linux just like putty manager. However, if you are a CLI fan with some ssh connections, please try ‘myssh’. Hope it will save you some time and make you fun:)

myssh

[root@daveti blog]# ./myssh
Welcome to myssh – an interactive ssh management tool
=====================================================
0    user0@server0
1    user0@server1
2    user1@server1
q    quit
=====================================================
Please input the number of your choice: q
[root@daveti blog]#

#!/bin/bash

#
# myssh
# An interactive ssh management tool
# dave.jing.tian@gmail.com
# 23 Feb, 2013
#

#============================================
# Configure the ssh information here ~ change
#============================================

# login list – add your login here
loginList=(“user0” “user1”)

# hostname/IPaddr list – add your remote machine here
remoteList=(“server0” “server1”)

# password list – add your password here
passwdList=(“passwd0” “passwd1” “passwd2”)

# ssh list – add the relation rules to construct the ssh entry
# format: “login-remote-passwd”
# example: “0-1-0” stands for a ssh entry as below
# user0@server1 with password passwd0
sshList=(“0-0-2” “0-1-0” “1-1-1”)

#============================================
# Internal used functions ~ no need to change
#============================================

# Display the usage and then exit
usage()
{
echo “Usage: myssh”
exit 1
}

# Get the “login@remote” from ssh entry
getLoginRemoteFromSshEntry()
{
local sshEntry=”$1″
local loginIndex=`echo $sshEntry | cut -d “-” -f1 | tr -d ” “`
local remoteIndex=`echo $sshEntry | cut -d “-” -f2 | tr -d ” “`
echo “${loginList[loginIndex]}@${remoteList[remoteIndex]}”
}

# Get the “passwd” from ssh entry
getPasswdFromSshEntry()
{
local sshEntry=”$1″
local passwdIndex=`echo $sshEntry | cut -d “-” -f3 | tr -d ” “`
echo “${passwdList[passwdIndex]}”
}

# Display all ssh entries within sshList
displaySshList()
{
echo “Welcome to myssh – an interactive ssh management tool”
echo “=====================================================”
# Go thru the sshList to construct the ssh entry menu
for((i=0; i<${#sshList[@]}; i++))
do
local loginRemote=`getLoginRemoteFromSshEntry ${sshList[i]}`
echo ”  $i    $loginRemote”
done
echo ”  q    quit”
echo “=====================================================”
}

#===========================================
# Main function of myssh ~ no need to change
#===========================================

# Check ssh and sshpass at first
SSHCMD=`which ssh`
SSHPASSCMD=`which sshpass`
if [ “$SSHCMD” = “” ] || [ “$SSHPASSCMD” = “” ]
then
echo “Please install both ssh and sshpass before using myssh”
exit 1
fi

# Display the ssh list
displaySshList

# Ask for the input
read -p “Please input the number of your choice: ” index

# Process the input
if [ “$index” = “q” ]
then
exit 1
elif [[ $index =~ ^-?[0-9]+$ ]] && [[ $index -ge 0 ]] && [[ $index -le ${#sshList[@]} ]]
then
# Re-parse this ssh entry within sshList
loginRemote=`getLoginRemoteFromSshEntry ${sshList[$index]}`
passwd=`getPasswdFromSshEntry ${sshList[$index]}`
# Debug
# echo “$loginRemote $passwd”

# Just do it!
$SSHPASSCMD -p “$passwd” $SSHCMD “$loginRemote”

else
echo “Invalid input: $index”
usage
fi

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Dave's Tools, Programming and tagged , , , . Bookmark the permalink.

3 Responses to myssh – an interactive ssh management tool

  1. daveti says:

    expect may be the other way used to pass the password to ssh, just like sshpass we have used on myssh…
    http://bash.cyberciti.biz/security/expect-ssh-login-script/

  2. daveti says:

    # Enable ssh on boot up
    systemctl enable sshd.service

  3. daveti says:

    For kernel 2.6.X, like CentOS 6, where systemctl is not available:
    start/stop/restart/status sshd:
    service sshd status/start/stop/restart
    enable sshd at the boot:
    chkconfig sshd on

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.