Reputation: 37
I'm trying to create a simple script for logging into various servers via ssh, all keys have been installed and are working but i simply cannot get this script to work for me. Basically there is an option as to which server the user wishes to login to but it keeps throwing up the following error:
': not a valid identifier `INPUT
login.sh: line 24: syntax error near unexpected token `elif'
'ogin.sh: line 24: `elif [ $INPUT -eq 2 ] ; then
The script layout can be found below with dummy info:
#!/bin/bash
echo "What Server would you like to login to?"
echo ""
echo ""
echo "1. Server 1"
echo "2. Server 2"
echo "3. Server 3"
echo "4. Server 4"
echo "5. Exit"
read INPUT
if [ $INPUT -eq 1 ] ; then
echo"Logging in"
echo"..."
ssh [email protected] -p 5678
elif [ $INPUT -eq 2 ] ; then
echo"Logging in"
echo"..."
ssh [email protected] -p 5678
elif [ $INPUT -eq 3 ] ; then
echo"Logging in"
echo"..."
ssh [email protected] -p 5678
elif [ $INPUT -eq 4 ] ; then
echo"Logging in"
echo"..."
ssh [email protected] -p 5678
elif [ $INPUT -eq 5 ] ; then
exit 0
else
echo "invalid choice"
return
fi
Any help would be greatly appreciated, relatively new to using bash and this is just annoying me now!
Upvotes: 0
Views: 1879
Reputation: 212198
This answer is really just a comment, but comments are not suitable for code. You're script could be greatly simplified. Consider something like:
#!/bin/bash
servers=( host1 host2 host3 )
ips=( 192.168.1.1 192.168.1.2 192.168.1.3 )
ports=( 123 22 33 )
select server in ${servers[@]}; do
echo "Logging into $server..."
ssh root@${ips[$REPLY]} -p ${ports[$REPLY]}
break
done
(Although it's not at all clear why you would want to specify the IP addresses rather than using the hostname!)
Upvotes: 1
Reputation: 1722
I realize this doesn't answer your question, but you said you were new to bash. I'd suggest using the case statement instead of a mess of ifs and I also added a prompt to your read statement (with the -p option). You might also look into the select statement instead of the case.
#!/bin/bash
echo "What Server would you like to login to?"
echo ""
echo ""
echo "1. Server 1"
echo "2. Server 2"
echo "3. Server 3"
echo "4. Server 4"
echo "5. Exit"
read -p "cmd> " INPUT
case $INPUT in
1)
echo "Logging in"
echo "..."
echo ssh [email protected] -p 5678
;;
2)
echo "Logging in"
echo "..."
echo ssh [email protected] -p 5678
;;
3)
echo "Logging in"
echo "..."
echo ssh [email protected] -p 5678
;;
4)
echo "Logging in"
echo "..."
echo ssh [email protected] -p 5678
;;
5)
echo "exiting"
exit 0
;;
*)
echo "invalid choice"
return
;;
esac
Upvotes: 0
Reputation: 77059
I'll throw myself in front of the SO bus by answering the question you didn't ask on this one. Why not use select
?
echo 'Which server would you like to log into?'
select server in 192.168.0.1 192.168.0.2 192.168.0.3 192.168.0.4; do
printf 'Logging in to server %s...\n' "$server"
ssh "$server" -p "$port"
done
If you don't like select
, then why not at least use case
?
read -p 'Which server do you want? ' input
case "$input" in
whatever)
printf 'Logging in to server %s...\n' "$server"
ssh "$server" -p "$port"
;;
*)
echo 'Invalid option'
;;
esac
Upvotes: 0
Reputation: 2151
Tried your script by copy pasting, it worked. I modified a little more on it to get the invalid choice option to work.
Errm... sorry, but it works for me?
#!/bin/bash
while true ; do
echo "What Server would you like to login to?"
echo ""
echo ""
echo "1. Server 1"
echo "2. Server 2"
echo "3. Server 3"
echo "4. Server 4"
echo "5. Exit"
read INPUT
if [ $INPUT -eq 1 ] ; then
echo "Logging in 1"
echo "..."
ssh [email protected] -p 5678
elif [ $INPUT -eq 2 ] ; then
echo "Logging in 2"
echo "..."
ssh [email protected] -p 5678
elif [ $INPUT -eq 3 ] ; then
echo "Logging in 3"
echo "..."
ssh [email protected] -p 5678
elif [ $INPUT -eq 4 ] ; then
echo "Logging in 4"
echo "..."
ssh [email protected] -p 5678
elif [ $INPUT -eq 5 ] ; then
exit 0
else
echo "invalid choice"
fi
done
Upvotes: 0
Reputation: 2350
looks like you created this file on windows. try, using dos2unix like:
dos2unix <your_script>
Upvotes: 3