We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

ksh script to ssh multiple servers

jaysilverheels
on
Medium Priority
697 Views
Last Modified: 2012-05-06
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.
Comment
Watch Question

Commented:
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
CERTIFIED EXPERT
Top Expert 2007

Commented:
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
CERTIFIED EXPERT
Top Expert 2007

Commented:
What Unix flavour is this on?

Can you describe the replace/add requirement a little further?  It looks like your question was truncated.

Author

Commented:
this is on redhat

Commented:
@jaysilverheels,

did you tried my script? is this what you need?
CERTIFIED EXPERT
Top Expert 2007

Commented:
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.


Commented:
@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.
CERTIFIED EXPERT
Top Expert 2007

Commented:
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
Commented:
@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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.