LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-13-2014, 10:16 AM   #1
omkar.jadhav
LQ Newbie
 
Registered: Aug 2013
Posts: 11

Rep: Reputation: Disabled
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..
 
Old 01-13-2014, 10:37 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
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.
 
1 members found this post helpful.
Old 01-13-2014, 10:45 AM   #3
omkar.jadhav
LQ Newbie
 
Registered: Aug 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by acid_kewpie View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
msktutil throws error mlysenko01 Programming 0 01-15-2008 10:28 AM
.htaccess with Basic authentication throws MySQL error in Error Log shailesh_mishra Linux - Security 6 01-17-2007 11:15 AM
Basic authentication with .htaccess throws MySQL error in Error Log shailesh_mishra Linux - Networking 2 01-12-2007 02:16 PM
Every Page of LQ throws a JS error. Rotwang LQ Suggestions & Feedback 5 04-18-2004 01:41 PM


All times are GMT -5. The time now is 03:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration