Link to home
Start Free TrialLog in
Avatar of buckethead34
buckethead34

asked on

Cron Job to login and run one command and logout

I am wanting to create a cron job on Fedora core 8 that logs into a 7 of our network devices, and runs one command that pushes the database backup to an ftp server, and then logs out. Can anyone help me with this? Thanks in advance
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

please the below link on how to run ftp through crontab:

https://www.experts-exchange.com/questions/23357010/Cron-FTP.html
I need more details! logs u mean login, if so what type of login it is? I mostly recommend configure password less login if possible.
The easiest way to run a command on a remote server is to use ssh.

In order to log in with out a password, you need to have the public key from the source machine in the authorized_hosts file on the target machine.

On the source machine (the machine running the command) see if you have ~/.ssh/id_dsa.pub or ~/.ssh/id_rsa.pub.  Those are public SSH keys.  If not, run "ssh-keygen -t dsa", which will create ~/.ssh/id_dsa.pub.

On the target machine create .ssh/authorized_keys and put the contents of the public key in that file.

Then simply run this from cron:
ssh target "command"

The very first time you run it you will have to accept the target machine's identity, so run it from the command line manually first.  That will make sure it works as well.

Good luck!
Avatar of buckethead34
buckethead34

ASKER

I don't want the job to run ftp, sorry if I was vague. I want the cron job to telnet to a network device and run one command on the box and logout. I'm looking into adding some scripts to do it and expect being in the scripts etc.
My understanding is that you already have a command on the target machine that created a database dump and puts it on an FTP site.  You just want to be able to run that command from another machine's cron.

Do you also need help constructing the command that does the database dump and puts it on an FTP site?

These should be tackled, if possible, as two separate things.
1) create a script on the target machine that does the database dump and puts it on an ftp site
and
2) create a cron on the source machine that simply logs in to the target and executes the script

You could combine the two, if you need to.. but it'd be an ugly command.
Just number 2 is necessary a login and a command ran.
If u have password less login then use can use the following for loop to login to each network devices and then run the following script.


#!/bin/bash
for i in `seq -w 7`;do echo test$i;ssh test$i"/usr/sbin/backup"; done

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial