Cron ssh

In Terminal the following works:

ssh username@xxx.xxx.xxx.xxx

With CronniX, this same command does nothing. It will not trigger when scheduled nor when I try "Run Now".

The crontab I've created is for a user with admin level. What am I missing here?

QuickSilver Dual 1GHz Mac OS X (10.3.9)

Posted on Mar 26, 2006 10:20 PM

Reply
21 replies

Mar 26, 2006 10:58 PM in response to Kenneth S Goff

Do you have key based auth with no passphrase? When you run it from the command line, does it ask fora password or does it log you in?

If you do have key based auth, here's a couple of things:

1) Use the full path to ssh instead of just ssh, i.e, /usr/bin/ssh user@host
2) turn up the log level on cronnix and see what the logs say
3) try running a tcpdump in another window to see if the problem is cronnix or ssh, i.e, sudo tcpdump port 22 and see if you see traffic when you hit run now.

Good luck

Mar 27, 2006 11:37 AM in response to Redwoodtree

I do not yet have key based auth with no passphrase. When run within Terminal, I am asked for a password.

I tried the tcpdump and things seemed to happen with Run Now but I don't know how to interpret the results.

Regarding key base auth with no passphrase, I contacted my commercial Web host (LINUX) and asked about this. They said I should generate a key on my client. I found a script to generate the key as follows:

ssh-keygen -d

I left it (empty for no passphrase) and got the following two files:

identification saved in /Users/myaccountname/.ssh/id_dsa
public key save in /Users/myaccountname/.ssh/id_dsa.pub

Also a key fingerprint was provided. I printed this Terminal window but am not sure what to do with the results. I'm guessing that one of these keys should be copied to the Web server.

QuickSilver Dual 1GHz

QuickSilver Dual 1GHz Mac OS X (10.3.9)

Mar 27, 2006 12:14 PM in response to Kenneth S Goff

Nice work! You're most of the way there.

On the host making the connection, go into the .ssh/ directory of the user in question and open the file id_rsa.pub or id_dsa.pub (whichever type you created). Copy the contents of that file.

No the host you are connecting to, go to the other users .ssh directory. If it doesn't exist, create it and make sure the permissions are correct (and ownership). Create a file called authorized_keys and authorized_keys2 , paste the contents of the .pub file into this file. Make sure there are no line breaks (all on one line).

Again, check the ownership and perms of these files, make sure they're 600. A few other gotchas:

1) The ssh daemon on the receiving host has to allow key based auth. Most do, some don't. If you have access to the SSHD config (usually in /usr/local/etc/sshd_config or /etc/ssh/sshd_config) you can enable.

2) The target users home director must not be GROUP WRITEABLE. Make sure its 650 or 600 or something.

3) Now, in your cron job, call ssh and use the -i flag to point to the identify file you created. For example, /usr/bin/ssh -i /path/to/file/.ssh/id_rsa user@hostname

Try it from the command line like that and you should be logged right in. If you're not, run ssh in verbose, ssh -vvv -i /path/to/file/.ssh/id_rsa user@hostname and see what errors you get.

If it's still not clear, you will have to go to the target host and look in the /var/log/messages or /var/adm/messages to the event log if it's windows host, and see what you're doing wrong or what the host thinks you're doing wrong.

Good luck.
Red

Mar 28, 2006 7:48 AM in response to Redwoodtree

Thanks, Redwoodtree. I think the no auth log on works. The following 2 different commands appear to produce the same results in a tcp dump, whether done directly within Terminal or Run Now in CronniX:

/usr/bin/ssh serveraccountname@xxx.xxx.xxx.xxx

/usr/bin/ssh -i /Users/myaccountname/.ssh/id_dsa serveraccountname@xxx.xxx.xxx.xxx

One clarification. I created 2 identical files from the id_dsa.pub and named them "authorized_keys" and "authorized_keys2" respectively but only copied the first file to the server .ssh directory. Should I have also transferred the second file to the server?

My next step is to copy a file to the server in preparation for updating mySQL. Do I understand correctly that part of this process was to establish the ssh connection to the server and then cp to that server?

I can use cp to copy a file on my local OS X system but I'm getting tripped up performing the operation from my system to the remote Web server. This Web server is described as a shared server or virtual server running under LINUX.

After ssh log in, the prompt is the following:

[serveraccountname] [~]$

The hosting support recommended "/var/www/" for an upload directory. How do I combine the source directory and target directory in a cp or other copy command?

QuickSilver Dual 1GHz

QuickSilver Dual 1GHz

Mar 28, 2006 8:43 AM in response to Kenneth S Goff

Once again, nice work. Many people get tripped up by this procedure.

You're almost there. A companion product to SSH is SCP. Use the scp command to copy a file or ssh to run a command.

For example, to a copy a file you would do:

scp -r -p -i /path/to/id file/.ssh/idrsa /path/to/local/file.tar.gz user@remotehost:/var/www/newfilename.tar

That will seamlessly copy the file over. Note that I included the -r , that will recurse down a directory, if you choose to copy a directory. The -p will attempt to preserve permissions. You may want to omit either.

If you want to execute a command on the other host, you can do the same with ssh. for example:

ssh -i /path/to/id file/ssh/idrsa user@remotehost ls -l

This will return ls -l back to you local host. If you want to run a string of commands you will have to include it in quote, (e.g, user@remotehost 'ls -l ; grep -i sometext'

That tends to get a bit tricky.

Good luck,
-Red

Mar 28, 2006 11:35 AM in response to Redwoodtree

The file copies after error messages.

In Terminal, I enter the scp command but it seems my "path to id_file" is not valid. The following error messages within Terminal occur:

Warning: Identity file /usr/bin/Users/kengoff/.ssh/id_dsa does not exist

or another variation-

Warning: Identity file /usr/bin/kengoff/.ssh/id_dsa does not exist

Then the next message:

Address xxx.xxx.xxx.xx maps to MyWebDomain.com, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT!

But then the file does indeed copy to the intended directory. The percent of file transfer is shown in Terminal and I can view the file within an FTP application. I can also delete it from that FTP application.

When I perform a Find ".ssh" within the my system's Finder, the "id_dsa" file does indeed exist at kengoff/.ssh/id_dsa. The companion, "id_dsa.pub" was what I used to create the "authorized_keys" file that was placed on the server's ".ssh" directory.

Can you see any problem with the following?

scp -r -p -i /usr/bin/Users/kengoff/.ssh/id_dsa /Applications\ \(Mac\ OS\ 9\)/Products.sql accountname@xxx.xxx.xxx.xx:/var/www/Products.sql

QuickSilver Dual 1GHz Mac OS X (10.3.9)

QuickSilver Dual 1GHz Mac OS X (10.3.9)

Mar 28, 2006 11:48 AM in response to Kenneth S Goff

Answers in-line... You'll be an expert on SSH key-based auth before this is all done.

The file copies after error messages.

In Terminal, I enter the scp command but it seems my
"path to id_file" is not valid. The following error
messages within Terminal occur:

Warning: Identity file
/usr/bin/Users/kengoff/.ssh/id_dsa does not exist


If you're initiating this on a mac (which I assume you are) that path is incorrect. It should be /Users/kengoff/.ssh/id_dsa , the /usr/bin is extraneous, take it out.

or another variation-

Warning: Identity file /usr/bin/kengoff/.ssh/id_dsa
does not exist

Then the next message:

Address xxx.xxx.xxx.xx maps to MyWebDomain.com, but
this does not map back to the address - POSSIBLE
BREAKIN ATTEMPT!


This is telling you that your ISP does not or has not configured their DNS with reverse lookups. This is sending off a warning on your version of ssh. Also, if the host-key of the remote host ever changes, you will also get a warning. You will have to open you .ssh/known_hosts and delete that line.

In terms of that warning, if the copy is working, you can ignore it.

But then the file does indeed copy to the intended
directory. The percent of file transfer is shown in
Terminal and I can view the file within an FTP
application. I can also delete it from that FTP
application.

When I perform a Find ".ssh" within the my system's
Finder, the "id_dsa" file does indeed exist at
kengoff/.ssh/id_dsa. The companion, "id_dsa.pub" was
what I used to create the "authorized_keys" file that
was placed on the server's ".ssh" directory.

Can you see any problem with the following?

scp -r -p -i /usr/bin/Users/kengoff/.ssh/id_dsa
/Applications\ \(Mac\ OS\ 9\)/Products.sql
accountname@xxx.xxx.xxx.xx :/var/www/Products.sql
QuickSilver Dual 1GHz Mac
OS X (10.3.9)

QuickSilver Dual 1GHz Mac OS X
(10.3.9)


Yep, just delete the /usr/bin and make it like so:
scp -r -p -i /Users/kengoff/.ssh/id_dsa ..... and so forth.


I'm particularly careful about including the "-i /path/to/.ssh/" because sometimes in cron jobs your users path is not picked up, or you might decided to have multiple keys for different hosts. In your case, you will notice it's not necessary because SSH is correctly picking your key. However, I recommend you keep it in there.

nice work overal!



MacBook Pro 2.0 Mac OS X (10.4.5) G5 1.6, TiBook 800, Pismo 447

Mar 28, 2006 1:14 PM in response to Redwoodtree

Thanks, Red. That eliminates the error about the path to id_file. And the scp copy process works. You really know your stuff.

Now the last part of the cron project. How do I have mySQL on the remote server load the uploaded file?

I'm used to using phpMyAdmin to "Choose File" to designate a text file located on my own local system. That text file has the SQL instructions to update data within mySQL tables.

Mar 28, 2006 1:34 PM in response to Kenneth S Goff

No problem. Always happy to share the wealth, as what goes around comes around.

In regards to the mysql.... there are a bunch of ways to do this. Here's one way to do it. But, you may want to give this some tought just in terms of timing (order of operations) and what to do if things go wrong, and security.

1) Copy the files up to the remote host, from the local host, via one script that contains the SCP commands. You would call this script via cron or whatever. If you want to get fancy, have error checking in the script to make sure it copied... I'll leave that to you 😉

The reason I'm adding this step is in case you want to be really sure the file copied. Otherwise, just use SCP from cron as you have it.

2) Then on the remote host, have a second script that is there to upload into mysql. That script would contain the "mysqlimport" command. E.g, mysqlimport --local dbname tableinfo.txt . As you say, you can use phpmyadmin to formal the SQL. That script would be executable.

The question is how to handle the mysql password on that end. In the worst case situation, you might have to do something really bad like hardcode it in your script. I'll leave that to you too 😉

3) finally, from the local host again, either as part of the 1st script or as part of another cron job, call the remote script via ssh, ssh -i /path/to/.ssh/id_dsa user@remote:/path/to/script.sh or script.pl or whatever..

The remote script will be executed and the files will load into mysql.

If I had to do this, I'd probably use something like ODBC and upload the files through that connection instead of loading them via cron job. At least I could have some host based (ip) security, and also a password... and maybe encrypt the connection.. blah blah blah...getting way too complicated.... 😉

Hope that helps a little bit.

Apr 3, 2006 9:58 PM in response to Redwoodtree

After getting my PHP script to load the uploaded data into MySQL, I was ready to verify the previous steps and modify the destination for the SCP upload. Now I cannot get the upload to occur.

Using a tcpdump in Terminal, it looks like things are happening but this time there is no percentage report of the upload progress like when it was successful. Many lines are written in the tcpdump but it's Greek to me.

Any ideas why it is not working now?

Apr 5, 2006 10:55 AM in response to Redwoodtree

The only way I could get the PHP script to run from a crontab is using curl. It works well. So I've got 3 crontab tasks. The first lauches an AppleScript to cause the Mac OS 9 database to generate the file export. The second use scp to copy the file to the remote server. The third uses curl to activate the PHP file that imports the data into MySQL.

Now I'm ready to look at error detection so that the import process will not be run unless a successful file transfer occurs. Can I prevent executing the curl cron task if the scp copy process produces an error? According to man scp, it exits with 0 on success or > 0 if an error occurred.

QuickSilver Dual 1GHz

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Cron ssh

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.