LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   while loop throws an error in ssh (https://www.linuxquestions.org/questions/linux-newbie-8/while-loop-throws-an-error-in-ssh-4175491126/)

omkar.jadhav 01-13-2014 09:16 AM

while loop throws an error in ssh
 
Hi All,

Here is the script which i have prepared to ssh to different servers and find out their OS name:
Code:

#!/bin/ksh
while read host
do
#checking the OS of server from server_list
ssh -p 56786 -l testuser $host
if [ $? -eq "0" ]
then
echo "ssh to $host successfull."
else
echo "ssh to $host not successfull,please troubleshoot."
exit 1
fi
OS=`uname -a|awk '{print $1}'|tr '[a-z]' '[A-Z]'`
case ${OS} in
SUN)
    echo "OS of $host is SUN Soalris"
    cat >> Solaris_serverlist <<EOF
    $host
    EOF
;;
AIX)
    echo "OS of $host is AIX"
    cat >> AIX_serverlist <<EOF
    $host
    EOF
;;
LINUX)
    echo "OS of $host is LINUX"
    cat >> Linux_serverlist <<EOF
    $host
    EOF
;;
esac
exit
done < servers_list

i am getting error as below :
Quote:

test1.sh: line 38: syntax error: unexpected end of file
i have tried to use -n option in ssh but whenever i do so i get below error:
Quote:

Pseudo-terminal will not be allocated because stdin is not a terminal.
Can someone please help me out..

acid_kewpie 01-13-2014 09:37 AM

Your script doesn't make sense, when you ssh you are attempting to start an interactive ssh session on a remote system. IF that worked (which it won't) then the script will present the user with a login prompt and then just sit there waiting for a password (or straight to a bash prompt if your'e using a key. The code you are apparently expecting to use to find the OS will NOT run on the remote server. At some point the ssh session will close, and when the ssh client process terminates, then the rest of the code will run on the LOCAL machine. This isn't going to do what you want.

update the ssh command to run uname there:

Code:

REMOTE_UNAME=$(ssh -p 56786 -l testuser $host uname -a)
then later in your code you can use that uname output as you see fit.

omkar.jadhav 01-13-2014 09:45 AM

Quote:

Originally Posted by acid_kewpie (Post 5097207)
Your script doesn't make sense, when you ssh you are attempting to start an interactive ssh session on a remote system. IF that worked (which it won't) then the script will present the user with a login prompt and then just sit there waiting for a password (or straight to a bash prompt if your'e using a key. The code you are apparently expecting to use to find the OS will NOT run on the remote server. At some point the ssh session will close, and when the ssh client process terminates, then the rest of the code will run on the LOCAL machine. This isn't going to do what you want.

update the ssh command to run uname there:

Code:

REMOTE_UNAME=$(ssh -p 56786 -l testuser $host uname -a)
then later in your code you can use that uname output as you see fit.

thanks acid_kewpie , i will update my script accordingly..could you let me know how i can use while loop to do ssh.
i am trying to ssh remote servers from server_list.txt file.


All times are GMT -5. The time now is 12:14 AM.