LinuxQuestions.org
Help answer threads with 0 replies.
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 06-18-2004, 02:18 PM   #1
nooodles
LQ Newbie
 
Registered: May 2004
Location: Texas
Posts: 18

Rep: Reputation: 0
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?
 
Old 06-18-2004, 10:10 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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.
 
  


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
Read parameters from config file (file parser?) alaios Programming 8 07-09-2012 12:29 PM
How to read .chm file in fedora, can't mount ntfs file system ishti_du Linux - Newbie 12 03-06-2007 04:27 AM
cannot read port 22 for ssh pauly99 Linux - Newbie 2 07-04-2004 01:26 AM
how do I make a read-only file NOT read-only? robster Linux - General 1 02-17-2004 10:11 PM
Change from Read only to Read Write File System? justiceisblind Linux - Newbie 3 03-03-2002 08:23 PM


All times are GMT -5. The time now is 09:49 PM.

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