Sending email through Cygwin - linux

I have a written a shell script wherein I want to send an email through it. I am executing this script on windows through cygwin. I have installed email package on my machine. However, I am having a hard time making it work. Please let me know what is the easiest way to send email through cygwin command prompt.
My ssmtp.conf file is :
mailhub=smtp.gmail.com:587
FromLineOverride=YES
rewriteDomain=gmail.com
root=aci.lfindba#gmail.com
UseTLS=YES
AuthUser=userid
AuthPass=password
and email.conf file has:
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = '25'
MY_NAME = 'ABC'
MY_EMAIL = 'emailaddress'
REPLY_TO = 'emailaddress'
USE_TLS = 'true'
ADDRESS_BOOK = '&/email.address.template'
SMTP_AUTH = 'LOGIN'
SMTP_AUTH_USER = 'userid'
SMTP_AUTH_PASS = 'password'
I am using below command to send email:
echo "mail body"|email -s "subject" recipient#gmail.com
However, I am getting following error:
email: FATAL: Could not connect to server: smtp.gmail.com on port: 25: Operation not permitted
Please help.

Install and configure the ssmtp package.
Create /bin/mail with these contents:
#!/bin/sh
#
# copyright 2016 Gene Pavlovsky [http://www.razorscript.com]
#
# mail: mail-like wrapper script for sendmail
SENDMAIL=/usr/sbin/ssmtp
usage()
{
{
echo "Usage: $(basename $0) [-s "subject"] [-f from-addr] [to-addr]..."
echo
echo "Sends mail."
echo
echo "Options:"
echo -e " -s\tsubject (quote subjects containing spaces)"
echo -e " -f\tfrom address"
} >&2
exit 2
}
while test $# -gt 0; do
case $1 in
-s)
shift
test $# -eq 0 && usage
subj=$1
;;
-f)
shift
test $# -eq 0 && usage
from=$1
;;
-*)
usage
;;
*)
rcpt+=( "$1" )
;;
esac
shift
test "$end_options" = yes && break
done
test ${#rcpt} -eq 0 && usage
{
test "$from" && echo From: $from
test "$subj" && echo Subject: $subj
echo
exec /bin/cat
} | "$SENDMAIL" "${rcpt[#]}"
Don't forget to chmod 755 /bin/mail.

I use the msmtp package, with this configuration:
port 587
auth on
from srpen6#gmail.com
host smtp.gmail.com
tls on
tls_certcheck off
user srpen6#gmail.com
https://cygwin.com/cgi-bin2/package-grep.cgi?grep=msmtp&arch=x86_64

Related

Trying to use this bash script to login to a remote ftp from ssh and delete files older than N days old

I am trying to use the following bash script to login to a remote ftp and delete files older than N days old. Script says it is working and does not give an error - but files are not being deleted. What am I missing? Or is there a better way to do this? Keep in mind this is only a remote FTP and not SSH so I can NOT use the mtime function is why I am trying to do this. Can anyone help?
The usage is all commands - here is what I am using via ssh to run the script
./ftprem.sh -s ftp.server.com -u myusername -p mypassword -f /directory -d 3
#!/bin/bash
PROGNAME=$(basename $0)
OUTFILE="/tmp/ftplist.$RANDOM.txt"
CMDFILE="/tmp/ftpcmd.$RANDOM.txt"
ndays=14
print_usage() {
echo ""
echo "$PROGNAME - Delete files older than N days from an FTP server"
echo ""
echo "Usage: $PROGNAME -s -u -p -f (-d)"
echo ""
echo " -s FTP Server name"
echo " -u User Name"
echo " -p Password"
echo " -f Folder"
echo " -d Number of Days (Default: $ndays)"
echo " -h Show this page"
echo ""
echo "Usage: $PROGNAME -h"
echo ""
exit
}
# Parse parameters
options=':hs:u:p:f:d:'
while getopts $options flag
do
case $flag in
s)
FTPSITE=$OPTARG
;;
u)
FTPUSER=$OPTARG
;;
p)
FTPPASS=$OPTARG
;;
f)
FTPDIR=$OPTARG
;;
d)
ndays=$OPTARG
;;
h)
print_usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [[ -z "$FTPSITE" || -z "$FTPUSER" || -z "$FTPPASS" || -z "$FTPDIR" ]];
then
echo "ERROR: Missing parameters"
print_usage
fi
# work out our cutoff date
TDATE=`date --date="$ndays days ago" +%Y%m%d`
echo FTP Site: $FTPSITE
echo FTP User: $FTPUSER
echo FTP Password: $FTPPASS
echo FTP Folder: $FTPDIR
echo Removing files older than $TDATE
# get directory listing from remote source
ftp -i -n $FTPSITE <<EOMYF > /dev/null
user $FTPUSER $FTPPASS
binary
cd $FTPDIR
ls -l $OUTFILE
quit
EOMYF
if [ -f "$OUTFILE" ]
then
# Load the listing file into an array
lista=($(<$OUTFILE))
# Create the FTP command file to delete the files
echo "user $FTPUSER $FTPPASS" > $CMDFILE
echo "binary" >> $CMDFILE
echo "cd $FTPDIR" >> $CMDFILE
COUNT=0
# loop over our files
for ((FNO=0; FNO<${#lista[#]}; FNO+=9));do
# month (element 5), day (element 6) and filename (element 8)
FMM=${lista[`expr $FNO+5`]}
FDD=${lista[`expr $FNO+6`]}
FYY=${lista[`expr $FNO+7`]}
if [[ $FYY == *\:* ]]
then
FDATE=`date -d "$FMM $FDD" +'%Y%m%d'`
else
FDATE=`date -d "$FMM $FDD $FYY" +'%Y%m%d'`
fi
# echo $FDATE
# check the date stamp
if [[ $FDATE -lt $TDATE ]];
then
echo "Deleting ${lista[`expr $FNO+8`]}"
echo "delete ${lista[`expr $FNO+8`]}" >> $CMDFILE
COUNT=$[$COUNT + 1]
fi
done
echo "quit" >> $CMDFILE
if [[ $COUNT -gt 0 ]];
then
cat $CMDFILE | tr -d "\r" > $CMDFILE
ftp -i -n $FTPSITE < $CMDFILE > /dev/null
else
echo "Nothing to delete"
fi
rm -f $OUTFILE $CMDFILE
fi
If this helps your debugging...
In the# Parse parameter section of the script, the options variable your have just before the case block has value options=':hs:u:p:f:d:' instead of options=':h:s:u:p:f:d:'
I thought i should point that out.

Can anyone help correct my bash script: Custom user creation in bash?

I've got basic knowledge of bash scripting, and need to create a function to create a user with custom settings:
1.User name and password
2.Group and user ID
3.Comment
4.Home directory
`function user_custom {
if [ $(id -u) -eq 0 ] ; then
read -p "Enter name here: " username
read -s -p "Enter password: " password
grep "^$username" /etc/passwd > /dev/null
if [ $? -eq 0]; then
echo -e "${LIGHTGREEN}This user already exists! Try another name
please.${RESET}" ; break
else
pass=$(perl -e 'print crypt(($AVG[0], "password")' $password)
read -p "Enter group for user: "GID
egrep "^$GID" /etc/passwd > /dev/null
if [ $? -eq 0] ; then
$GID=$?
else
echo "No such group"
fi
read -p "Enter custom comment for $username: "comment
read -p "Enter unique user ID" Uid
getent passwd `grep "^Uid" /proc/$3/status |awk '{printf "%4s",$2}'` | if
[$? -eq $Uid ] ; then
echo "Select diferent ID"
fi
useradd -m -p -u -c -g $username $pass $Uid $comment $GID
fi
}`
I have a function ready, but I'm sure there are a lot of obvious mistakes.
Its like a patched skirt, I've gathered a lot of data from different sources.
I would be thankful if someone could help with fixing it.
Ill provide a screenshot of the whole function.
I'm from Ukraine, but i am good at english\russian.
PS: btw the 'if' in the end is included, all ifs are closeTd
Thanks ahead! <3
I correct it for you, I hope this will suit your need. You had lot of typo like if [ anything ]; then there is always needed space after [ and before ].
Brake only working for loops, you need to use exit 1 to terminate function with error code 1.
Instead of encrypt password in the script I prefer let passwd do the job, it will also enforce password rules.
The following way you can pass almost everything to command, like you typed it in from console:
(echo $password ; echo $password ) | passwd $username
I would add a lot of other check to make this script to enforce followings:
Uid should be number, and less/equal than UID_MAX in /etc/login.def
Gid should be a number, and less than the GID_MAY in /etc/login.def
And some feature as well like:
The script cannot handle if you like to make a GID for the user insted of using a existing one.
Asking if it should create home folder or not (currently -m do it in useradd)
If passwd fail because of password rule enforcing on the host, there is no retrying so you need to type it manually.
This is just the ones came into my mind now but there could be more.
#!/bin/bash
function user_custom {
if [ $(id -u) -eq 0 ] ; then
read -p "Enter name here: " username ;
read -s -p "Enter password: " password ; echo
grep "^$username" /etc/passwd > /dev/null
if [ $? -eq 0 ] ; then
echo -e "\nThis user already exists! Try another name please.\n" ; exit 1
else
read -p "Enter group for user: " GID
egrep "^"$GID":" /etc/group > /dev/null
if [ $? -gt 0 ] ; then
echo "No such group ( "$GID" )"; exit 1
fi
read -p "Enter custom comment for $username: " comment
read -p "Enter unique user ID: " Uid ; echo
if [ $(getent passwd | awk -F ":" '{print $3}' | egrep -c "^"$Uid"$" ) -gt 0 ];then
echo " ID already exist! " ; exit 1
fi
fi
useradd -m -u $Uid -c $comment -g $GID $username &&
(echo $password ; echo $password ) | passwd $username
fi
}
user_custom

Stop shell script sending repeated emails - using file flag

My requirement is to send an email if I find a string in a log file; however, I should be sending it only once. The shell script I have written is pasted below; however, it is sending repeated emails via cron job even when the condition is not matching.
#!/bin/bash
filexists=""
lbdown=""`enter code here`
if [ -f "/var/run/.mailsenttoremedy" ];
then
filexists=true
else
filexists=false
echo filexists is $filexists
fi
if tail -1000 /usr/ibm/tivoli/common/CTGIM/logs/trace.log | grep "Root exception is java.net.NoRouteToHostException: No route to host"
then
echo error found
lbdown=true
echo lbdown status after if in tail is $lbdown
else
lbdown=false
echo lbdown status after else in tail is $lbdown
fi
if filexists=false && lbdown=true
then
{
mailx -S intrelay.sysco.com -r xxx#yyy.com -s "**DEV ALERT**Load Balancer Connection not Available" -v xxx#yyy.com < /dev/null
date > /var/run/.mailsenttoremedy
}
fi
if filexists=true && lbdown=true
then
{
echo MAIL ALREADY SENT
}
fi
if lbdown=false
then
rm -f /var/run/.mailsenttoremedy
fi
echo lbdown is $lbdown and filexists is $filexists
echo outputs are:
filexists is false
Root exception is java.net.NoRouteToHostException: No route to host
error found
lbdown status after if in tail is true
Null message body; hope that's ok
Mail Delivery Status Report will be mailed to <xxx#yyy.com>.
MAIL ALREADY SENT
lbdown is false and filexists is true
You could try a normal declaration for the if requests...
Bash format:
if [ $(tail -1000 /usr/ibm/tivoli/common/CTGIM/logs/trace.log | grep "Root exception is java.net.NoRouteToHostException: No route to host") != "" ];
then
if [ "$filexists" = "false" ] && [ "$lbdown" = "true" ];
then
if [ "$lbdown" = "false" ];
then
Test commands via if should be surrounded by [ and ], at least the ones with multiple conditions. Here is a guide for if and some sample codes if you are interested.
In addition, variables need a $ in front of them at least. Normally they are surrounded by { and } too.
PS. You could use a better format for the post so people won't downvote you.
For others, the working code is:
#!/bin/bash
filexists=""
lbdown=""
if [ -f "/var/run/.mailsenttoremedy" ];
then
filexists=true
else
filexists=false
echo filexists is $filexists
fi
if tail -1000 /usr/ibm/tivoli/common/CTGIM/logs/trace.log | grep "nn"
then
echo error found
lbdown=true
echo lbdown status after if in tail is $lbdown
else
lbdown=false
echo lbdown status after else in tail is $lbdown
fi
if [[ "$filexists" = "false" && "$lbdown" = "true" ]];
then
mailx -S intrelay.sysco.com -r xxx#yyy.com -s "**DEV ALERT**Load Balancer Connection not Available" -v xxx#yyy.com < /dev/null
date > /var/run/.mailsenttoremedy
fi
if [[ "$filexists" = "true" && "$lbdown" = "true" ]];
then
echo MAIL ALREADY SENT
fi
if [ "$lbdown" = "false" ];
then
rm -f /var/run/.mailsenttoremedy
echo removing file
fi
echo lbdown is $lbdown and filexists is $filexists

monitoring gearman in nagios

I am trying to monitor gearman by nagios for that I am using script check_gearman.sh.
Localhost is where gearman server running.
When I run
./check_gearman.sh -H localhost -p 4730 -t 1000
It results in:
CRITICAL: gearman: gearman_client_run_tasks : gearman_wait(GEARMAN_TIMEOUT) timeout reached, 1 servers were poll(), no servers were available, pipe:false -> libgearman/universal.cc:331: pid(613)
Can some one please help me out in this.
below is script
#!/bin/sh
#
# gearman check for nagios
# written by Georg Thoma (georg#thoma.cn)
# Last modified: 07-04-2014
#
# Description:
#
#
#
PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="0.04"
export TIMEFORMAT="%R"
. $PROGPATH/utils.sh
# Defaults
hostname=localhost
port=4730
timeout=50
# search for gearmanstuff
GEARMAN_BIN=`which gearman 2>&1 | grep -v "no gearman in"`
if [ "x$GEARMAN_BIN" == "x" ] ; then # result of check is empty
echo "gearman executable not found in path"
exit $STATE_UNKNOWN
fi
GEARADMIN_BIN=`which gearadmin 2>&1 | grep -v "no gearadmin in"`
if [ "x$GEARADMIN_BIN" == "x" ] ; then # result of check is empty
echo "gearadmin executable not found in path"
exit $STATE_UNKNOWN
fi
print_usage() {
echo "Usage: $PROGNAME [-H hostname -p port -t timeout]"
echo "Usage: $PROGNAME --help"
echo "Usage: $PROGNAME --version"
}
print_help() {
print_revision $PROGNAME $REVISION
echo ""
print_usage
echo ""
echo "gearman check plugin for nagios"
echo ""
support
}
# Make sure the correct number of command line
# arguments have been supplied
if [ $# -lt 1 ]; then
print_usage
exit $STATE_UNKNOWN
fi
# Grab the command line arguments
exitstatus=$STATE_WARNING #default
while test -n "$1"; do
case "$1" in
--help)
print_help
exit $STATE_OK
;;
-h)
print_help
exit $STATE_OK
;;
--version)
print_revision $PROGNAME $REVISION
exit $STATE_OK
;;
-V)
print_revision $PROGNAME $REVISION
exit $STATE_OK
;;
-H)
hostname=$2
shift
;;
--hostname)
hostname=$2
shift
;;
-t)
timeout=$2
shift
;;
--timeout)
timeout=$2
shift
;;
-p)
port=$2
shift
;;
--port)
port=$2
shift
;;
*)
echo "Unknown argument: $1"
print_usage
exit $STATE_UNKNOWN
;;
esac
shift
done
# check if server is running and replys to version query
VERSION_RESULT=`$GEARADMIN_BIN -h $hostname -p $port --server-version 2>&1 `
if [ "x$VERSION_RESULT" == "x" ] ; then # result of check is empty
echo "CRITICAL: Server is not running / responding"
exitstatus=$STATE_CRITICAL
exit $exitstatus
fi
# drop funtion echo to remove functions without workers
DROP_RESULT=`$GEARADMIN_BIN -h $hostname -p $port --drop-function echo_for_nagios 2>&1 `
# check for worker echo_for_nagios and start a new one if needed
CHECKWORKER_RESULT=`$GEARADMIN_BIN -h $hostname -p $port --status | grep echo_for_nagios`
if [ "x$CHECKWORKER_RESULT" == "x" ] ; then # result of check is empty
nohup $GEARMAN_BIN -h $hostname -p $port -w -f echo_for_nagios -- echo echo >/dev/null 2>&1 &
fi
# check the time to get the status from gearmanserver
CHECKWORKER_TIME=$( { time $GEARADMIN_BIN -h $hostname --status ; } 2>&1 |tail -1 )
# check if worker returns "echo"
CHECK_RESULT=`cat /dev/null | $GEARMAN_BIN -h $hostname -p $port -t $timeout -f echo_for_nagios 2>&1`
# validate result and set message and exitstatus
if [ "$CHECK_RESULT" = "echo" ] ; then # we got echo back
echo "OK: got an echo back from gearman server version: $VERSION_RESULT, responded in $CHECKWORKER_TIME sec|time=$CHECKWORKER_TIME;;;"
exitstatus=$STATE_OK
else # timeout reached, no echo
echo "CRITICAL: $CHECK_RESULT"
exitstatus=$STATE_CRITICAL
fi
exit $exitstatus
Thanks in advance.
If you download the mod_gearman package, this contains a much better and more featured check_gearman plugin for Nagios.
With your current plugin, the error message shows that the check script cannot connect to the gearman daemon.
You should verify that port 4370 is listening on localhost, and that there is no local firewall blocking connections. It is likely that you have installed your gearmand on a different port, or have it only listening on the network interface, not on localhost. Or maybe it is not runing at all, or is on a different server from the one running the check...

starting WSO2 carbon with su

I did install the WSO2 Identity server on a Ubuntu 10.4 server and connected it to a MySQL database. Now I did create a user wso2user and gave this user full permission over the WSO2 folders. When I start the server with the following command:
#! /bin/sh
su wso2user -c '/opt/identitywso2/bin/wso2server.sh'
the server starts and I can log in, but the my command prompt stays in the shell with the last log message:
[2014-05-19 14:14:27,938] INFO {org.wso2.carbon.identity.entitlement.internal.EntitlementServiceComponent} - Started thrift entitlement service at port:10500
[2014-05-19 14:14:43,534] INFO {org.wso2.carbon.identity.entitlement.internal.SchemaBuilder} - XACML policy schema loaded successfully.
What could be wrong? I want to start the serve without need to stay in the shell.
Thanks for any hints.
Lucas
Here is my script, based on WSO2 API Manager, but you can use to also for any other WSO2 product. Script is based on Suse EE SP3. Put this file in /etc/init.d and do a checkconfig.
#!/bin/sh
#
# /etc/init.d/wso2
# init script for wso2.
#
# chkconfig: 2345 90 60
# description: wso2 indexer service
#
RETVAL=0
. /etc/rc.status
BAD_USER="This script should be run as root or as wso2 user. Exiting......."
cmd="/bin/sh -c"
if [ "$USER" != 'root' -a "$USER" != 'wso2' -a "$USER" != '' ]; then echo $BAD_USER && exit 1;fi
if [ "$USER" == 'root' -o "$USER" == '' ]; then cmd="su - wso2 -c";fi
wso2pid=`pidof java`
wso2_start() {
echo Starting wso2...
$cmd "/opt/wso2/am/bin/wso2server.sh --start"
}
wso2_stop() {
echo Stopping wso2...
$cmd "/opt/wso2/am/bin/wso2server.sh --stop"
if [ -n "$wso2pid" ]
then
echo -n "Waiting for wso2 ($wso2pid)"
while [[ ( -d /proc/$wso2pid ) ]]
do
echo -n "."
sleep 1
done
echo "Stopped"
fi
}
wso2_restart() {
echo Restarting wso2...
$cmd "/opt/wso2/am/bin/wso2server.sh --restart"
}
wso2_status() {
echo -n "Status of wso2 is "
if [ -n "$wso2pid" ]
then echo "Running. ($wso2pid)"
else echo "Stopped."
fi
}
case "$1" in
status)
wso2_status
;;
start)
wso2_start
;;
stop)
wso2_stop
;;
restart)
wso2_restart
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL

Resources