1

I have a bash script which I wrote that lists out some servers/user names. I chose a # which then connects me to said server with said user name. So far the script works fine other then the fact that when I launch ssh, the bash script hangs. It doesn't dump me into ssh.

#!/bin/bash
echo `clear`
SERVER1="1.) Server1/username1"
SERVER2="2.) Server1/username2"
echo -e "Please choose a server:"
echo $SERVER1
echo $SERVER2
read server
if [ $server -eq 1 ]; then
        serverconnect="ssh -t [email protected]"
        servername="server1.com"
        serveruser="username1"
else

        if [ $server -eq 2 ]; then
                serverconnect="ssh -t [email protected]"
                servername="server1.com"
                serveruser="username2"
        fi
fi

echo "Connecting you to: $servername as $serveruser"
echo `$serverconnect`
2
  • You might want to make use of a select around a case, especially if you have more than two servers (and you can use arrays to store your server names and user names). You can eliminate "serverconnect" and just do ssh -t "$serveruser@$servername" Commented Aug 13, 2010 at 8:23
  • In reality I have around 15 servers. Not sure about using select and case, coming to bash from a php background. Commented Aug 13, 2010 at 17:21

3 Answers 3

3

just execute ssh normally. Don't put it inside variable

if [ $server -eq 1 ]; then
        serverconnect="[email protected]"
        servername="server1.com"
        serveruser="username1"
else

        if [ $server -eq 2 ]; then
                serverconnect="[email protected]"
                servername="server1.com"
                serveruser="username2"
        fi
fi

ssh -t "$serverconnect" 
Sign up to request clarification or add additional context in comments.

Comments

3

I wrote a much easier script to connect to ssh. You can add as many servers as you want to array.

#!/bin/bash
echo `clear`
SERVERS=('server1' 'server2' 'server3' 'server4')

echo "Server to connect:"
for server in ${!SERVERS[*]}
do
    printf "%4d: %s\n" $server ${SERVERS[$server]}
done

read -p "Select a server to connect: " CHOISE
read -p "Enter username: " USERNAME


ssh $USERNAME@${SERVERS[$CHOISE]}

Comments

0
#!/bin/bash
echo `clear`
SERVERS=('server1' 'server2' 'server3' 'server4')

echo "Server to connect:"
for server in ${!SERVERS[*]}
do
    printf "%4d: %s\n" $server ${SERVERS[$server]}
done

read -p "Select a server to connect: " CHOISE
read -p "Enter username: " USERNAME


ssh $USERNAME@${SERVERS[$CHOISE]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.