开发者

SSH Bash script for easy connection

开发者 https://www.devze.com 2023-01-11 14:53 出处:网络
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

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开发者_StackOverflow中文版 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 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`


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

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" 


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]}


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号