Appending crontab using ssh and sudo without root credentials

Hi,

i have two servers say server A and server B. i have a sudo user say user1 with full privilges on server A and B. i am trying to append the crontab entry of root from server A of server B with the following command. But its appending on A. i need to append it on server B.

please find the Command below which i am running on server A which should login to server B and append crontab entry of B. instead its appending on A only.

/usr/bin/sshpass -p 'password' /usr/bin/ssh -o StrictHostKeyChecking=no -l user1 10.10.10.10 -t 'echo password' | sudo -S  bash -c 'echo "30 10 * * * sh test.sh" >> /var/spool/cron/root'

Depending on the OS, wouldn't it be easier to scp a file to /etc/cron.d?

If you need to do it this way, try putting backticks (the one to the left of the number 1) around the remote command

/usr/bin/sshpass -p 'password' /usr/bin/ssh -o StrictHostKeyChecking=no -l user1 10.10.10.10 -t `'echo password' | sudo -S  bash -c 'echo "30 10 * * * sh test.sh" >> /var/spool/cron/root'`

sshpass is extremely insecure, because the password is passed as a parameter. This gives an opportunity for it to be intercepted.

This is the reason for plain ssh's "annoying" limitation of only accepting passwords from a terminal, and why you had to install a third party utility to do this.

sudo has the same limitation - it will not accept a password from 'echo password'. su also has the same limitation, in fact, any sane authentication system will have the same limitation. Password authentication means typed-in-realtime-by-a-human authentication and no substitutions for human are acceptable.

I suggest using ssh keys for noninteractive authentication for ssh, and also suggest configuring sudo for passwordless operation so you don't have to kludge a password into it.

Hi Padow1

when i use backticks i am getting the below error.

-bash: echo password: command not found

Also its appending on server A not B.

Kindly help

Don't do that. Backticks don't make sense there. But that command is not going to work, period -- sudo does not work that way.

I suggest using ssh keys for noninteractive authentication for ssh, and also suggest configuring sudo for passwordless operation so you don't have to kludge a password into it.

Once you do both of these, you will no longer need a third-party hacking utility to accomplish basic things. It's always easier when you use basic features as intended.