I need to setup a cron job on multiple servers (all Unix based). For this, I have created a script (lets refer to it as script “A”) which sets up the cron job correctly when executed manually. Also, I have uploaded this script to all the target servers with the intention of calling and executing it automatically via another script on another server.
Now, this other server contains the script “B” (as mentioned above) which logs into all the target servers and calls script A in order to automatically setup the cron on all servers.
The problem is, script A is never called and cron is never setup. Script B is an Expect script and here’s the code,
spawn rsync -avz -e ssh $localDir/autodir $username@$ipaddress:$remoteDir
sleep 10
expect {
"assword:" {
send "$pwd\r"
sleep 5
exp_continue
}
expect {
"@" {
send "cd /home/$username/autodir/config\r"
sleep 5
exp_continue
expect "@"
send "./setCron.sh" # NEED TO EXECUTE THIS AT REMOTE SERVER
sleep 5
exp_continue
}
}
"yes/no" {
send "yes\r"
set timeout -5
exp_continue
}
-re $prompt {
send "\r"
}
timeout {
exit
}
eof {
exit
}
}
Basically, I am looking for a general purpose solution to call a script on a remote server without using ssh or public key authentication because I cannot use both of these methods due to some limitations. Hence I tried using Expect but to no success. I could not find the proper solution even after a lot of rummaging. Help needed here!
Just discovered the solution while tinkering around. Here’s the code,
This logs into all my servers, syncs the file/directories and sets up the cron job. Works like a charm. I had to use ssh in the end though.