Error with script for ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error with script for ssh
# 1  
Old 08-12-2017
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:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 08-12-2017 at 10:55 AM.. Reason: Changed ICODE to CODE tags.
# 2  
Old 08-12-2017
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}

This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-12-2017
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}

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 08-13-2017
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}

# 5  
Old 08-13-2017
What's the value of the "RequestTTY" option in ssh_config, if any?
# 6  
Old 08-13-2017
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...)
This User Gave Thanks to vbe For This Post:
# 7  
Old 08-13-2017
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; 08-13-2017 at 08:09 AM.. Reason: set PATH
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Ssh script to validate ssh connection to multiple serves with status

Hi, I want to validate ssh connection one after one for multiple servers..... password less keys already setup but now i want to validate if ssh is working fine or not... I have .sh script like below and i have servers.txt contains all the list of servers #/bin/bash for host in $(cat... (3 Replies)
Discussion started by: sreeram4
3 Replies

2. Red Hat

Ssh error

ssh -q <remote_server> <command> is not working for only one remote server. From the source, we are able to connect other servers using the above command but one. All the servers are in same network and no selinux/iptables enabled. The verbose output has the following message - debug1:... (4 Replies)
Discussion started by: atanubanerji
4 Replies

3. Shell Programming and Scripting

SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All, I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use... (5 Replies)
Discussion started by: ss112233
5 Replies

4. Shell Programming and Scripting

ssh run script error

Hello when try to excute the following ssh -l pla 10.287.60.55 vis_fil vis_fil not found. but it works fine when login to the server. can you help me run this command in one online because i will add later to shell script regards (1 Reply)
Discussion started by: mogabr
1 Replies

5. UNIX for Advanced & Expert Users

SSH using shell script terminates the script execution

Hello, I am writing a shell script in which i do ssh to remote server and count the number of files there and then exit. After the exit the shell script terminates which i believe is expected behavior. Can some one suggest me a way where even after the exit the script execution resumes. ... (2 Replies)
Discussion started by: manaankit
2 Replies

6. Shell Programming and Scripting

ssh - run shell script - Error

Hi Team, I am trying to run shell script from one server to another server with below command. ssh abc@pqr.america.com /tmp/test.ksh But, it gives below error. Can someone help me what is the issue? exec(): 0509-036 Cannot load program ssh because of the following errors: ... (3 Replies)
Discussion started by: ace_friends22
3 Replies

7. UNIX for Dummies Questions & Answers

Error Executing a script(SSH problem)

Hi i m writing a script which will fetch data from different machines and display it on single terminal. I have created a file named SERVERNAMES containing ip address of machines i need to monitor. Then for every IP i m issuing ssh command to get date on that machine. However When i m trying... (3 Replies)
Discussion started by: pinga123
3 Replies

8. UNIX for Advanced & Expert Users

ssh error

Hi, I'm executing the set of commands in remote machine via ssh command. But the control is not even going inside the if statement (not echo-ing anything). /usr/bin/ssh ${2}@${1} <<EOF >/var/tmp/housekeeping_${1}_${2}_`date +%Y%m%d%H%M%S`.log if ; then echo -e "File system is 100%... (15 Replies)
Discussion started by: apsprabhu
15 Replies

9. UNIX for Advanced & Expert Users

ssh error: Error reading response length from authentication socket

Hi - I am getting the error `Error reading response length from authentication socket' when I ssh from my cluster to another cluster, and then back to my cluster. It doesn't seem to affect anything, but it's just annoying that it always pops up and tends to confuse new users of the cluster. I... (1 Reply)
Discussion started by: cpp6f
1 Replies

10. Shell Programming and Scripting

ssh command in ksh script encounters permission error

I've created a script and copied it to another server and try to execute it at the same time but I'm getting a permission error. I'm logged on as root and I gave the file 777 permission when I created it, but I can't run it remotely via ssh command because of permission issue. Any ideas? ... (5 Replies)
Discussion started by: pdtak
5 Replies
Login or Register to Ask a Question