[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 671
  • Last Modified:

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.
0
jaysilverheels
Asked:
jaysilverheels
1 Solution
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now