sftp using batch and public key doing 'put' launching from crontab
Hello! I'm kinda new to *nix automation and have a task that I'm not sure about. The OS is CentOS 7 on 'A' and Ubuntu on 'B'. SFTP is the only method allowed on 'B'.
The task: I would like to sftp a file from server 'A' to server 'B' using 'batchfile', starting from a cronjob. It will also need to email back if there is a failure for some reason. This is what I have so far. It is not tested as I am not confident I have it right. Code:
#originating user's crontab Code:
#Begin in proper directory Code:
progress Any help is appreciated. I looked at a few forum posts here and there, and this one in particular, but I am not sure how to translate the examples to my scenario. :( Thanks! |
CentOS already has a log file for cron which is /var/log/cron, it isn't very verbose tho, it'll just give you the end result.
What you maybe after is called 'I/O redirection' Code:
# uptime >> /root/somefile.log |
Thanks for the response, r3sistance. The redirection might be helpful.
So, if I want to email results to an email address, how would I apply this redirection in the script but only if there is a failure? |
mmm, if sftp has a non 0 exit status maybe? If that works then you could do something like.
Code:
sftp -b /home/user/batchfile_commands -oPort=port# -o IdentityFile=~/.ssh/id_rsa_xfer username@serverB.com >> /some/log/file Should have said this earlier, but I am assuming "port#" is changed to an actual number, as the "#" symbol may act as a comment where the shell would ignore anything after it. |
Thanks! I will mess around with this.
Much appreciated! Yes, you are correct that the bit 'port#' is just a stand-in for whatever the actual port number is. ;-) |
I think I am making progress. The job fires off, does its log in thing, uploads a file then exits. As expected, if everything goes well, I get no notice.
The problem I have now - apparently the third-party will kill the ability to connect via key after a certain amount of time? 24hrs after uploading the public key to the typical 'authorized_keys' file in .ssh/ it will fail to allow me to connect. I have to upload the same pub key again. I'm using 24hrs as a guide since ight after I upload the key, everything works. I come in the next day, with no changes I am aware of, and it won't. As a result, I need to now also pass potential login failures to an email from this cronjob. I noticed there is output in /var/spool/mail/useraccount but I don't need all the info from that file. Only what pops in showing the login fail ergo something like tail -n 25 /var/spool/mail/useraccount. I feel like there is something I can add to the conditional to get this? Just not sure what. |
Just found out a minor detail. The third-party is apparently scrubbing the authorized_keys file. Discovered after I decided to get the remote file and vim it:
# Generated by Chef for remotesite.com # Local modifications will be overwritten. sad face. |
Quote:
We utilize authorized_keys2 Might be worth a try? |
Quote:
Now... we wait...:) |
Thank me if/when it works :)
|
Quote:
Next is to work in something that will email me upon connectivity failure ( since I have the conditional for the sftp actions failing - thanks to r3sistance for that bit to work with ). Here is what I have so far, but it only shows output from the transactions in the batch file, not the actual failure of connecting via sftp: Code:
# Begin in export directory Maybe this?: Code:
if [[ $? != 0 ]]; then EDIT2: more progress. Found out that I had to move extra returns in the output file to get mail to send it in a viewable format. Code:
tr -d \\r |
Gunna guess you are after something like...
Code:
echo $(cat /tmp/log.txt; tail -n 25 /var/spool/mail/useraccount) | mail -s "Error Report" -r "account@domain" "recipientaddr@domain.com" |
methinks scp would be funner in this instance.
|
Quote:
|
All times are GMT -5. The time now is 07:46 PM. |