0

I am having a problem doing what I need. I can save a ton of time if I do this right.

I basically need to loop over every host, and I can get that part on my own.

But I can never get the command to come out right.

I basically want to do this for every host

 chmod 750 /root; chown -R root:root /root;exit;

I've tried so many variants and all of them them will just ssh to the server and do nothing on that server. They never actually run that command on the destination server. Instead they end up running the command on the server I am running ssh from.

Also note public keys are already set up so passwords are no issue.

So to give a better understanding I will show this

for h in HOSTNAMES do
  ssh $h; OTHER STUFF
done
miken32
  • 42,008
  • 16
  • 111
  • 154
Chris Jones
  • 662
  • 2
  • 10
  • 23

2 Answers2

1

this works for me.

C02NQ9GLG3QD:~ lcerezo$ ssh -K myhost "last -1;uptime"
lcerezo   pts/2        172.31.0.40      Wed Dec 16 17:31 - 17:39  (00:07)
wtmp begins Tue Sep  8 17:15:21 2015
16:12  up 1 day,  5:06, 6 users, load averages: 2.16 1.98 2.00
C02NQ9GLG3QD:~ lcerezo$`

Have you looked at tools like pdsh? For mass configuration though, I think your time will be better spent learning a configuration management tool such as:

  • ansible
  • puppet
  • cfengine
  • salt stack
lcerezo
  • 31
  • 3
1

The command to the remote server is given as the final argument to ssh. Also the exit command is not required, as the connection is closed after commands are completed.

for h in $HOSTNAMES
do
  ssh $h "chmod 750 /root; chown -R root:root /root"
done
miken32
  • 42,008
  • 16
  • 111
  • 154