Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Error with script for ssh

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 5 Days Ago
sam_bd sam_bd is offline
Registered User
 
Join Date: May 2011
Last Activity: 13 August 2017, 10:38 PM EDT
Posts: 71
Thanks: 19
Thanked 1 Time in 1 Post
Error Error with script for ssh

Hi,
I'm getting error while getting output of home directories for multiple servers. Could anyone please help; (using HP-UX; shell=ksh)


Code:
SERVERLIST=serverlist.txt
OUTPUTLIST=output.txt

for host in $(cat ${SERVERLIST})
do
ssh $(host) "`hostname`;`echo $HOME`" > ${OUTPUTLIST}
done

Error:

Code:
ssh: Server1;/path/dir: host nor service provided, or not known
Usage: host [-aCdlrTwv] [-c class] [-n] [-N ndots] [-t type] [-W time]
            [-R number] hostname [server]


Moderator's Comments:
Error with script for ssh Please use CODE tags as required by forum rules!

Last edited by RudiC; 5 Days Ago at 09:55 AM.. Reason: Changed ICODE to CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 5 Days Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 17 August 2017, 2:39 PM EDT
Location: Aachen, Germany
Posts: 11,166
Thanks: 287
Thanked 3,441 Times in 3,170 Posts
There are quite some errors and opportunitites in your code:
- the $(host) is a "command substitution", not a variable expansion. Use braces instead (as you do for the list files).
- the ´hostname´ is a "command substitution" again, making the remote shell try to execute the result of the hostname command, not the command itself. Remove backticks.
- same for the echo command.
- UUOC (useless use of cat) in the for loop. Better: redirected while read.
- for more than one server in the list, all results except for the very last one will be overwritten in the output file. Use append redirection (>>) or shift the redirection.

Consider

Code:
while read host
  do  ssh $host "hostname; echo $HOME"
  done < ${SERVERLIST}  > ${OUTPUTLIST}

The Following User Says Thank You to RudiC For This Useful Post:
sam_bd (4 Days Ago)
Sponsored Links
    #3  
Old Unix and Linux 5 Days Ago
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 17 August 2017, 9:29 PM EDT
Location: Simplicity
Posts: 3,676
Thanks: 298
Thanked 1,229 Times in 1,110 Posts
That goes into the next trap: ssh reads from stdin i.e. the while loop's input.
A fix is -n.
Further, it makes more sense to print the remote $HOME; within 'ticks' the local shell does not substitute it, and the remote shell gets the $HOME without the ticks.

Code:
while read host
do
  ssh -n $host 'hostname; echo $HOME'
done < ${SERVERLIST}  > ${OUTPUTLIST}

The Following User Says Thank You to MadeInGermany For This Useful Post:
sam_bd (4 Days Ago)
    #4  
Old Unix and Linux 4 Days Ago
sam_bd sam_bd is offline
Registered User
 
Join Date: May 2011
Last Activity: 13 August 2017, 10:38 PM EDT
Posts: 71
Thanks: 19
Thanked 1 Time in 1 Post
Thank you very much RudiC and MadeInGermany for your inputs. I'm getting below error when i used while loop as mentioned above;

Code:
Pseudo-terminal will not be allocated because stdin is not a terminal.
ssh: hostname;echo /home/dir: host nor service provided, or not known

shell script i used:

Code:
SERVERLIST=serverlist.txt
OUTPUTLIST=output.txt
while read host
do
ssh -n $host "hostname;echo $HOME"
done < ${SERVERLIST} >> ${OUTPUTLIST}

Sponsored Links
    #5  
Old Unix and Linux 4 Days Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 17 August 2017, 2:39 PM EDT
Location: Aachen, Germany
Posts: 11,166
Thanks: 287
Thanked 3,441 Times in 3,170 Posts
What's the value of the "RequestTTY" option in ssh_config, if any?
Sponsored Links
    #6  
Old Unix and Linux 4 Days Ago
vbe's Unix or Linux Image
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 17 August 2017, 2:10 PM EDT
Location: Switzerland - GE
Posts: 6,443
Thanks: 279
Thanked 547 Times in 509 Posts

Code:
while read host
do
ssh -n $host "hostname;echo $HOME"

You should NEVER name a variable the same as a reserved word : host ( usually system variable, or like here a true UNIX command...)
The Following User Says Thank You to vbe For This Useful Post:
sam_bd (4 Days Ago)
Sponsored Links
    #7  
Old Unix and Linux 4 Days Ago
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 17 August 2017, 9:29 PM EDT
Location: Simplicity
Posts: 3,676
Thanks: 298
Thanked 1,229 Times in 1,110 Posts
The use of $host is no problem.
Do you have ssh aliased or another ssh in your PATH?

Code:
type ssh

should find out.
The following filters some rubbish from the input file

Code:
PATH=/bin:/usr/bin:/sbin:/usr/sbin
SERVERLIST=serverlist.txt
OUTPUTLIST=output.txt
while read host junk
do
  case $host in
  [!#]*)
    ssh -n "$host" 'hostname; echo $HOME'
  ;;
  esac
done < ${SERVERLIST} >> ${OUTPUTLIST}


Last edited by MadeInGermany; 4 Days Ago at 07:09 AM.. Reason: set PATH
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calling shell script within awk script throws error Sudhakar333 Shell Programming and Scripting 10 05-01-2013 12:04 PM
Re: Script Error [syntax error at line] anudeepkumar123 UNIX for Dummies Questions & Answers 3 08-15-2011 03:54 PM
How to grep sql error in shell script and exit the script? allinshell99 Shell Programming and Scripting 2 05-27-2010 09:06 PM
Error opening script file - location error starcraftbud Windows & DOS: Issues & Discussions 9 02-17-2010 04:56 PM
awk Shell Script error : "Syntax Error : `Split' unexpected Herry UNIX for Dummies Questions & Answers 2 03-17-2008 11:16 AM



All times are GMT -4. The time now is 01:08 AM.