ksh script to ssh multiple servers

i want to ssh to multiple servers and do exactly the same thing on every server -replace a certain line in a ascii text file and add a new line.  for the purpose of the examples sake assume the line start with sorbone then remove the line and add a line where th

so something like
remove="sorbonne"
add="text case"
hosts=host1,host2,host3,host4,host5 (list doesn't have to be like this)
for h in $hosts; do grep "^$remove" then delete it and add new line

Appreciate your help so much with this one.
jaysilverheelsAsked:
Who is Participating?
 
dzamfirCommented:
@Tintin, I have to apologize, you're right. Is not working like that.

but for:

for i in {1,2,3,4,5} ; do echo $i ; done

is working..

Cheers.
0
 
dzamfirCommented:
This should work:

#!/bin/sh

REMOVE="sorbonne"
ADD="text case"
HOSTS="host1,host2,host3,host4,host5"

for h in ${HOSTS}
  do
 ssh user@${h} "sed -i 's/^${REMOVE}.*/${ADD}/g' ascii_text_file"
done
0
 
omarfaridCommented:
try

remove="sorbonne"
add="text case"
hosts="host1host2 host3 host4 host5"
for host in $hosts
do
   eval ssh user@$host "sed -i 's/$REMOVE/$ADD/g' filename"
done
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
TintinCommented:
What Unix flavour is this on?

Can you describe the replace/add requirement a little further?  It looks like your question was truncated.
0
 
jaysilverheelsAuthor Commented:
this is on redhat
0
 
dzamfirCommented:
@jaysilverheels,

did you tried my script? is this what you need?
0
 
TintinCommented:
dzamfir

Note that in your script

HOSTS="host1,host2,host3,host4,host5"


should be

HOSTS="host1 host2 host3 host4 host5"

It's still not clear exactly what jaysilverheels wants to replace/delete/add in the file.


0
 
dzamfirCommented:
@Tintin,

Note that doesn't matter if its comma separated. Is still working. Please try something similar and you will see. I've asked the author is this is what he wants.

Thanks for your intervention.
0
 
TintinCommented:
commas do make a big difference unless you have IFS=','

See:


$ cat a
#!/bin/sh
HOSTS="host1,host2,host3,host4,host5"

for h in ${HOSTS}
do
  echo "HOST=$h"
done

$ ./a
HOST=host1,host2,host3,host4,host5
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.