how to send an email when creating a bash script - bash

I'm trying to create a bash script that will send me an email every time it pings an IP address.
When I run the script I get and error saying
bash: sendmail: command not found
Here is my code
while true; do
ping my_ip_address
echo "some message" | sendmail -s "test" "test#test.com"
sleep 20
done
and this is my sendmail.ini file
smtp_server=smtp.mailtrap.io
smtp_port=587
smtp_ssl=auto
error_logfile=error.log
auth_username=f1e1ea69338455
auth_password=97ce281bfdb99f
pop3_server=pop3.mailtrap.io
pop3_username=f1e1ea69338455
pop3_password=97ce281bfdb99f
force_sender=
force_recipient=
hostname=
I have my sendmail.ini saved in C:\sendmail\

Looks like sendmail is not in your $PATH.
Try to either add directory with sendmail to your $PATH or use full path to sendmail.
Edit:
Also remove credentials from your question.

Related

Mutt bash script sends email fine from command line but not from motioneye notification hook

So I'm about ready to pull my hair out on this one. I am running ssmtp and mutt on a freeBSD jail. I have a bash script called notify that contains the following line.
mutt -s "$subject" "$email" -a "$attachment" < "$logfile3"
When I run
bash notify.sh
The email will send just fine, but if I run that exact same command from inside motioneye I receive an extremely non-descript error from mutt
Could not send the message
I have tried using a daemon but that hasn't had any effect. I wish the mutt error message was more descriptive.
My script did not have access to the "sent" mailbox and therefore mutt was failing to send. -e "set copy=no" added to the mutt call corrected the issue.
For a better walkthrough: https://gitlab.com/muttmua/mutt/issues/119
Make sure you have bash installed (it isn't by default on FreeBSD) and try using absolute path to it (/usr/local/bin/bash).

mail (postfix) not running in launchAgent unless followed by another command

I'm running a user LaunchAgent in El Cap that is supposed to call postfix to send me an email when a certain automatically generated file is 0 length, meaning that something went wrong. Here's the line from the bash script where $me is my username:
echo 'Darn it' | mail -s 'Export failed' "$me"
It works fine if I run the script manually, but mail won't send anything when the agent runs.
By dumb luck I found that if I follow this command with the following command, the message will get sent:
/usr/local/bin/terminal-notifier -message "File export failed." -title "Alert"
Other following commands won't do it (like echo, for example).
Any idea what's going on there?

Oozie shell action doesn't send mail notification

I am running a shell script through oozie. A piece of my shell script has below code. I receive an email when I run this shell script through unix Command line but when I run this shell script through oozie Job it will succeed but I dont get any mail. How can I resolve this? Or is there any alternative way I can get email with attachment once string matches in unix filesystem?
if [ "$Var" = "Error" ]
then
echo "Data error" | mail -v -s "Data Error" -a error.csv -S smtp=smtp://mail-gateway -S from=localhost#gmail.com" Kevin#gmail.com
exit

store ftp command output in a variable

I am using bash a script to connect to an FTP server for deleting a file.
I would like to store the output message and code of the delete command executed on the FTP server into a variable of my script.
How could I do this ?
Here is my snippet :
...
function delete_on_ftp{
ftp -i -n $ftp_host $ftp_port <<EOF
quote USER $ftp_login
quote PASS $ftp_pass
delete $1
quit
EOF
}
output_cmd=$(delete_on_ftp $myfile)
...
By the way I do above I only get the message, no way to get the returned code. Is there another way allowing to get the code and the message, in 1 or 2 variables ?
Thanks, Cheers
I just tested the following curl command, which make your task easy.
curl --ftp-ssl -vX "DELE oldfile.pdf" ftp://$user:$pass#$server/public_html/downloads/
Please do not forget the slash at the end of your directory, it is necessary.
curl: (19) RETR response: 550
550 oldfile.pdf: No such file or directory
curl: (19) RETR response: 250
250 DELE command successful
curl is available at http://curl.haxx.se/.
One of the ways to get FTP to act automatically is to use a Netrc file. By default, FTP will use $HOME/.netrc, but you can override that via the -N parameter. The format of a netrc file is fairly straight forward. A line is either a Macrodef or a line that contains login information. Here's an example below:
Netrc File
mysystem login renard password swordfish
another login renard password 123456
default login renard password foofighter
macdef init
binary
cd foo
get bar
delete bar
quit
macdef fubar
...
The three first lines are the logins for various systems. The default is a login for any system which you don't define a particular login for. The lines that start with marcodef are macros you define to do a series of steps for you. The init macro automatically runs right after login. If the last line is quit, it will quit out of FTP for you. There should be a blank line to end the macro, (although most systems will take an End of the File as the end of the macrodef too).
You can create a Netrc file on the fly, enter your FTP command in that, and then, run your FTP command with that Netrc file:
cat > $netrc_file <<<EOF
$ftp_host login $ftp_login password $ftp_password
macdef init
delete $my_file
quit
EOF
ftp -N $netrc_file
You can capture the output via STDOUT, or in a variable and then parse that for what you need:
ftp -N $netrc_file | tee $ftp_output
Other answers on this question should provide you what you want.
However, if you are keen on specifically using ftp command, you can use expect command for the same...
Note, that this is not the best way to achieve what you are trying.
expect -c "log_user 0;
spawn ftp -i -n $ftp_host $ftp_port;
expect \"<add ftp login username: prompt details here>\"
send \"quote USER $ftp_login\r\n\"
expect \"<add ftp login password: prompt details here>\"
send \"quote PASS $ftp_pass\r\n\"
expect \"<add ftp shell prompt details here>\"
log_user 1; send \"delete $1\r\n\"
log_user 0;
expect \"<add ftp shell prompt details here>\"
send \"quit\r\n\";
interact"
You may need to add some more lines in the above for the login & shell prompts returned by the ftp command.

checking if a streaming server is up using bash?

I use Ubuntu and am trying to write a script that makes the following:
-test if an audio stream works
-if not, send an email.
I have tried the following code (running as a cron job every 10 minutes), which 'works' if I supply the wrong pw e.g.(it sends an email then), but does nothing if the actual server is down (tested by killing the server). any ideas on how to fix the script?
Thanks in advance!
#!/bin/bash
#servertest.sh
username=user1
password=xyz
url="http://wwww.streamingaudioserver.com -passwd $password -user $username"
mplayer $url &
sleep 5
test=$(pgrep -c mplayer)
if [ $test = 0 ]; then
#server is down!
mailfile="downmail.txt"
/usr/sbin/ssmtp test#maildomain.com < "/home/test/$mailfile"
fi
killall mplayer
sleep 5
exit
Your problem is in this line:
$mailfile="downmail.txt"
remove the dollar sign and that should do it.
You should be getting error messages in your cron log or emails to the crontab owner complaining about a command not found or no such file.
Edit:
Does your script work if run from the command line (with the stream down) rather than cron?
Try using set -x (or #!/bin/bash -x) in the script to turn on tracing or use echo "PID: $$, value of \$test: $test" > /tmp/script.out after the assignment to see if you're getting the zero you're expecting.
Also, try an ssmtp command outside the if to make sure it's working (but I think you already said it is under some circumstances).
Try your script without ever starting mplayer.

Resources