1

Below is my script.

#!/bin/bash
SERVERS="sl20rht00-t"
#SERVERS="shcbrht01-t"
USR="adm-dt-c"
for host in $SERVERS
do
ssh -tt $USR@$host 'sudo cp /etc/sudoers /Backups/sudoers_11thmarch2015; sudo cp /etc/ssh/sshd_config /Backups/sshd_11thmarch2015; sudo cat /home/adm-dt-c/denyuser >>  sudo /etc/ssh/sshd_config; sudo /etc/init.d/sshd restart;'
done
exit 0

The problem is I'm not able to append the line in /etc/ssh/sshd_config even after the successful execution of the script.

Anthon
  • 79,293
Divya
  • 11
  • 2
    You can't just throw sudo in randomly like a magic spell - it doesn't work like that. In particular sudo cat /home/adm-dt-c/denyuser >> sudo /etc/ssh/sshd_config makes no sense. – steeldriver Mar 12 '15 at 13:15

1 Answers1

0

sudo doesn't work for redirection. You probably now have a ~$USR/sudo file. Instead of redirect, pipe to a sudo tee.

Change this:

ssh -tt $USR@$host 'sudo cp /etc/sudoers /Backups/sudoers_11thmarch2015; sudo cp /etc/ssh/sshd_config /Backups/sshd_11thmarch2015; sudo cat /home/adm-dt-c/denyuser >>  sudo /etc/ssh/sshd_config; sudo /etc/init.d/sshd restart;'

To something like this:

ssh -tt $USR@$host 'sudo cp /etc/sudoers /Backups/sudoers_11thmarch2015; sudo cp /etc/ssh/sshd_config /Backups/sshd_11thmarch2015; sudo cat /home/adm-dt-c/denyuser | sudo tee -a /etc/ssh/sshd_config > /dev/null; sudo /etc/init.d/sshd restart;'