I have a very small office environment, and my team sends created pdfs to an sFTP server daily.
Occasionally, I will get a call that someone can't log in to upload the files.
My normal course of action is to connect to the sFTP server myself, run a commmand like ls to determine it is responding.
I would like to be able to automate this with notification if there is a failure:
Login to the sFTP server (with credentials).
Run an LS command
Email if connection times out or login fails.
I have limited experience with writing Batch files, but I can't seem to figure a way to get only a 'failed' / no response to send an email.
Could anyone help with ideas? I'd like to run this as a VB or Batch in Scheduled Tasks, as I have a Server 2000 machine this could run on. I know batch has issue sending emails, but i have another batch file that uses Blat.exe to send an email with passed variables, so i could use that if i could get batch to send failed responses...
You should be able to do this with a batch file.
Create a file called logon.ftp. This file contains the FTP logon script. Mine contains:
open Ftp_server
ftpuser
ftppassword
ls -l
quit
The testftp.bat file:
ftp.exe < logon.ftp | grep "Not connected" > nul && call :alert_someone
#echo Logon successful
goto exit
:alert_someone
#echo %date% %time% > alert.txt
#echo ftp_server appears to not be taking logins. >> alert.txt
blat alert.txt -to you -from ftp_watcher -subject "alert %date% %time% ftp_server not taking logins"
:exit
You'll need to get blat, and grep so you can do the string checking. My winxp ftp doesnt support errorlevels, so I'm using the errorlevel returned from grepping the 'Not connected' string to figure out if this worked or not.
You can get wget or curl to do this as well, and they do support errorlevels.
Batch files can be a bit too basic for this kind of thing.
If you were able and willing to experiment with the Python programming language ( http://www.python.org ) and additionally install the Paramiko module ( http://www.lag.net/paramiko/ ) then it would be possible to write a script along the lines of...
import paramiko
try:
t = paramiko.Transport(('TheHostname', 22))
t.connect(username='MyUsername', password='MyPassword')
sftp = paramiko.SFTPClient.from_transport(t)
dirlist = sftp.listdir('.')
except:
print "It's Broken"
#Send e-mails and such here
that you could then schedule to run on a regular basis.
Related
I've been asked to write a script in work that will send an email to our support email if an IP stops allowing us access to telnet on port 23. I've got the gist of what I need, but when the IP doesn't deny access, the script opens telnet and I can't get it to close using a command in the batch file.
This is what I've got so far. Sorry if there is already a question which answers this, I haven't been able to find it.
#ECHO OFF
set IPADDRESS=1.2.3.4
set INTERVAL=5
telnet %IPADDRESS% 23
if errorlevel 1 GOTO LOG
:LOG
ECHO %DATE% %TIME% %IPADDRESS% Could Not Be Reached >> failurelog.txt
GOTO MAIL
:MAIL
blat -install smtpserver
blat failurelog.txt -to email -subject "%IPADDRESS% Is Down"
timeout %INTERVAL%
exit
I wrote a shell script that I use under ash, and I redirect stderr and stdout to a log file. I would like that log file to be emailed to me only if stderr is not empty.
I tried:
exec >mylog.log 2>&1
# Perform various find commands
if [TEST_IF_STDERR_NOT_EMPTY]; then
/usr/bin/mail -s "mylog" email#mydomain.com < mylog.log
fi
My question is twofold:
1- I get a -sh: /usr/bin/mail: not found error. It seems that the mail command doesn't exist under ash (or at least under my linux box, which is a Synology NAS), what would be the alternative? Worst case, perl is available, but I would prefer to use standard sh commands.
2- How to I test that stderr is not empty?
Thanks
How to check if file is empty in bash
As for the first question, in your code you are calling mail but lower in the post you are calling email. Check your code and make sure it is mail.
Use which mail to get the full path. Maybe it is not installed in /usr/bin/.
Use find to locate mail.
If you can go to another shell, run it and then execute which mail to get the full path of mail in case the path is set up in the alternative shells.
I want to know if we can trigger a perl script in unix through a sending a mail.
basically the script should check the incoming mail then trigger the perl script.
Also can some1 out on setting the mail access like reading and saving mail on my unix home.
-Thanks
There are two basic approaches you can take.
Configure your SMTP server to run incoming email through your script. Procmail is the usual tool for choice for this.
Poll (by using cron, or writing your script as a daemon) your IMAP/POP server/Maildir/Mbox/etc.
The former is usually the better option.
You can hack it like this:
inotifywait -m /var/mail/$USER | grep --line-buffered MODIFY | while read _unused_; do
#your perl script here
done
Explanation: It monitors /var/mail/$USER for changes & prints events on stdout. On every MODIFY event, it will trigger the script.
Note: This will work only for unix mails on localhost. Not on external server.
Known bugs:
A mail read activity will also trigger the script.
You can keep polling (unix) mail for any new mails.
while true; do
echo "\nq" | mail >/tmp/new_mail 2>&1 && /path/to/your/perl_script.pl arg1 arg2 ...
sleep 60
done
If you want, you can use contents of /tmp/new_mail in your perl program. If you don't need it, you can redirect mail output to /dev/null instead.
TASK TO BE ACCOMPLISHED:
To schedule a perl script which is executed on a specific time / day in a week
THINGS I HAVE DONE:
In a schedule Tasks, I have created a new Task by which the Task will call a batch file with below contents
cd "DRIVE\FOLDER\Hummingbird\Connectivity\14.00\Exceed\"
ABCD.xs
cd mDrive/bin
perl baseline.pl -publish -location XXX -email
THINGS NOT WORKING FOR ME / CAUSING THE ISSUE:
Wen I run the scheduler, the prompt opens up the ABCD.xs exceed file window seperately file but the below commands are executed in the command pronpt itself
EXPECTED OUTPUT:
I want the commands
cd mDrive/bin
perl baseline.pl -publish -location XXX -email
to be executed in the exceed window
Any kind of solution wud be great
Thanks in advance.
Haresh
Sounds like you need to start getting into either SendKey stuff (Win32 packages) or else look into writing Exceed/Hummingbird scripts and just executing those.
Some other things to look into... does the remote server have a telnet or ssh server running? Or are there other methods of executing code on the remote server?
For example, my work's mainframe is accessed via a Hummingbird terminal emulator, but I can also telnet to the mainframe and execute commands as well as FTP batch job directly into the JES spool. So when I execute things on the mainframe by way of my PC (Perl scripts, etc.), I don't even fool with Hummingbird.
Good luck...
I am using CakePHP 1.3 and I was able to successfully able setup the cron job to run shells using the example that was given in the CakePHP Book.
*/5 * * * * /full/path/to/cakeshell myshell myparam -cli /usr/bin -console /cakes/1.2.x.x/cake/console -app /full/path/to/app >> /path/to/log/file.log
This outputs the results into a log file but I want to receive email when there is an error so I can try to resolve the problem.
I tried the following with no luck.
If I remove the >> /path/to/log/file.log then even the successful run is emailed.
> /dev/null, my assumption was it would send a successful to /dev/null and error to email.
1> /dev/null, tried another variation of 2
Any help is appreciated.
Thanks
Huseyin,
This is not a CakePHP error then, and is maybe a question better suited for serverfault, as you would script your solution.
Bash's built-in facilities are up to the task, try The linux documentation project's neat introductory tutorials on shell scripting and #man bash.
Your solution basically has to use a temporary file or variable in which you store the output of the last cron job run. If there is an error:
cat THE_TMP_FILE | mail -s "Error from Server Huseyin's server" huseyin#fancy_domain.com
else:
cat THE_TMP_FILE >> blah.blah.log
Unfortunatly, you need a MTA available, in order to make the mail command. If you do not have access to the mail command, then you set another cron job following the first in time which then simply runs a if [ -e THE_FILE_CONTAINING_THE_LAST_ERROR]; then { echo $(cat THE_FILE_CONTAINING_THE_LAST_ERROR); rm -v THE_FILE... ;} ; fi
Of course this is not working code, but pretty close, so you'll get the idea.