• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 730
  • Last Modified:

script to add new line to file in multiple servers with ssh

This script below only works if there is one hostname in the hosts field at the start. Not sure if the appending is the best way either. Also have to sudo to root
#!/usr/bin/ksh
ADD="goodbye"
file="/tmp/bucket"
hosts="host1-1,host-2"

for i in ${hosts}  do
ssh ${i} ; sudo su -
"echo $ADD >> $file"
 done
0
jaysilverheels
Asked:
jaysilverheels
  • 2
  • 2
2 Solutions
 
TintinCommented:
I'd do it like this
#!/usr/bin/ksh
ADD="goodbye"
file=/tmp/bucket
 
for host in host1-1 host-2
do
  ssh $host "sudo -s echo $ADD >>$file" 
done

Open in new window

0
 
omarfaridCommented:
try

#!/usr/bin/ksh
ADD="goodbye"
file="/tmp/bucket"
hosts="host1-1 host-2"
#
for host in $hosts
do
    eval ssh $host "sudo -s echo $ADD >> $file"
done
0
 
jaysilverheelsAuthor Commented:
only problem here is that with -s on sudo you need a password but how do you get around the ttyin /out?
0
 
TintinCommented:
Are you able to change the sudo setup on the remote servers?

If not, then your script will need to be more complicated.
0
 
jaysilverheelsAuthor Commented:
")
0

Featured Post

Technology Partners: 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!

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