LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ssh, file read, etc. Help please! (https://www.linuxquestions.org/questions/linux-newbie-8/ssh-file-read-etc-help-please-195052/)

nooodles 06-18-2004 01:18 PM

ssh, file read, etc. Help please!
 
I want to be able to write a script that manages a system remotely. this is what i have and i can't seem to figure out the problem, so any pointers, suggestions, comments, possible fixes would be greatly appreciated. =)


---main---
#!/bin/bash

while read cmd; do

ssh 10.10.10.10 $cmd

done < command.file


--command.file---
echo "hello"
echo "this is a test"
.
.
etc...


i have it set such that there's a trusted host relationship b/w the host and client so there's no password prompt. what seems to happen is that while loop in the main program only executes the first command in the command file. and i have no clue why.

help... pleaaaaaaaaase?

Dark_Helmet 06-18-2004 09:10 PM

First, I would suggest using ssh only once. In other words, put your management script on the remote machine, then use ssh to execute that script. You can get the script there by using ftp, scp, or whatever. Then all you would do is: ssh 10.10.10.10 management_script

If that's not an option and you must execute the commands like the script you have above, then i would change it to something like this:
Code:

#!/bin/bash

old_ifs=${IFS}
IFS=$'\n'

cat command.file | while read cmd ; do
  ssh 10.10.10.10 ${cmd}
done

IFS=${old_ifs}

exit 0

I honestly don't know whether you can redirect input like you had originally. I've always done it this way and it works. So what's the business about IFS? IFS stands for Input Field Separator (or something like that). It's default value splits lines of input into "words" by looking for spaces. This is bad if your commands have any spaces in them. For instance, a simple command such as touch lock_file would be split, and two ssh command would be executed: ssh 10.10.10.10 touch and ssh 10.10.10.10 lock_file (which is clearly not what you want). The script saves the original IFS value, replaces it with a newline (so that a full line of text from the command.file will be treated as a single entry), and then restores the original value after the loop (in case you might add something to the script later).

Unfortunately, I don't have an environment to test this on (I only have one machine). So I can't say with 100% certainty it would work.


All times are GMT -5. The time now is 09:45 AM.