SSH Bash脚本,便于连接

时间:2010-08-13 01:38:38

标签: bash ssh

我有一个我编写的bash脚本,列出了一些服务器/用户名。我选择了#然后用我的用户名将我连接到所述服务器。到目前为止,脚本工作正常,然后当我启动ssh时,bash脚本挂起。它不会把我转储成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 username1@server1.com"
        servername="server1.com"
        serveruser="username1"
else

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

echo "Connecting you to: $servername as $serveruser"
echo `$serverconnect`

3 个答案:

答案 0 :(得分:3)

正常执行ssh。不要把它放在变量

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

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

ssh -t "$serverconnect" 

答案 1 :(得分:1)

我写了一个简单得多的脚本来连接ssh。您可以添加要阵列的任意数量的服务器。

#!/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]}

答案 2 :(得分: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]}