Send mails to multiple users with looping using mailx and output from another file - mailx

Project scope: Locking few users and sending them email that has to be automated.
Issue: the mailx part isn't working as I need help on how to input the options for mailx.
DB server: AIX 7.x
I have a file that has the below:
alter user x1 account lock;
alter user x2 account lock;
I have to use the x1,x2.. and send these user individual email using mailx.
Part of my script is:
for i in $(cat Test_lock_users.log)
do
print mailx -c manager#abc.com -s "WARNING - $ORACLE_SID User account Locked" "manager#xyz.com $i#xyz.com"
done
Korn shell script as stated above in #2. This is on AIX 7.x server
I got the output as given below and no email sent to the address.
mailx -c manager#abc.com -s WARNING - $ORACLE_SID User account Locked manager#xyz.com x1#xyz.com
mailx -c manager#abc.com -s WARNING - $ORACLE_SID User account Locked manager#xyz.com x2#xyz.com
No error message either. Its just gave the output and how to execute/run the mailx with x1, x2 inputs?

Related

How do i enter the password while joining the Linux machine to Active Directory using shell script?

I am joining Linux machines to windows active directory and i am able to do it successfully using SSSD.
Now I am trying to automate the same process wherein i came across the step where i need to enter a password while joining the domain.
Can someone help in how to enter the password via shell script?
My code is :
#!/bin/bash
set -x
passwd=`cat /domain/domain_join.txt | grep password | awk -F '[=]' '{print$2}'`
/usr/bin/expect << EOF
spawn realm join domainname -U username#domainname -v
expect "Password for username#domainname: \r"
send "$passwd\r"
EOF
set +x
This isn't actually an LDAP question - it's AD and Kerberos and sshd.
It looks like you've got a user account to join the machine - presumably it has the correct rights. The easiest thing to do is to get a keytab created for that account, and then you can do a kinit and call the script in that context
kinit principal#EXAMPLE.COM -k -t keytab; joinscript
You don't need to define your username in the realm join command if you've already done the kinit.
Sorry, I can't test this, and it's been a while, but the secret sauce is a keytab for the Windows credential.

Bash script with sendmail delivers email when executed manually but not from crontab

I wrote the following bash script to send me an alert if there is a problem with my website:
#!/bin/bash
# 1. download the page
BASE_URL="https://www.example.com/ja"
JS_URL="https://www.example.com/"
# # 2. search the page for the following URL: /sites/default/files/google_tag/google_tag.script.js?[FIVE-CHARACTER STRING WITH LETTERS AND NUMBERS]
curl -k -L ${BASE_URL} 2>/dev/null | grep -Eo "/sites/default/files/google_tag/google_tag.script.js?[^<]+" | while read line
do
# 3. download the js file
if curl -k -L ${JS_URL}/$line | grep gtm_preview >/dev/null 2>&1; then
# 4. check if this js file has the text "gtm_preview" or not; if it does, send an email
# echo "Error: gtm_preview found"
sendmail error-ec2#example.com < email-gtm-live.txt
else
echo "No gtm_preview tag found."
fi
done
I am running this from an Amazon EC2 Ubuntu instance. When I execute the script manually like ./script.sh, I receive an email in my webmail inbox for example.com.
However, when I configure this script to run via crontab, the mail does not get sent via the Internet; instead, it gets sent to /var/mail on the EC2 instance.
I don't understand why this is happening or what I can do to fix it. Why does sendmail behave different if it is being run from bash vs being run from crontab?
Be aware that the PATH environment variable is different for crontab executions than it is for your typical interactive sessions. Also, not all of the same environment variables are set. Consider specifying the full path for the sendmail executable ( which you can learn by issuing the 'which sendmail' command ).

How to get SFTP login alert (email or SMS) on ubuntu?

I want to get Email or/and SMS alert whenever someone logs into my server. I have successfully done this for SSH login by editing .bashrc file.
But this method doesn't work for SFTP login.
I know I can do this by extracting information from log files, but i want a more efficient way (Since users can delete from log file).
I have also tried this, but it doesn't work.(I'm using ubuntu 12.04).
Basically, I want to execute a curl command (SMS API) on successful login.
Please help, thank you.
First, make sure you have the sendmail package (sudo apt-get install sendmail).
You can create or edit the file /etc/ssh/sshrc and give it the following code to achieve this:
ip=`echo $SSH_CONNECTION | cut -d " " -f 1`
logger -t ssh-wrapper $USER login from $ip echo "User $USER logged in from $ip" | sendemail -q -u "Email Title" -f "Sender <from#server.com>" -t "Your Name <you#email.com>" -s smtp.server.com &
Fill the appropriate variables (sender and recipient names and addresses) into this code.

sending an email as part of a bash shell script

I have herd of examples of people using alpine to send emails based on data collected or generated from bash scripts.
I have been looking for tutorials to create this for myself with no success. I do have alpine talking to my email server, but requires me to still enter a password. I am guessing I will need to correct this as well.
My end result would be to run a long task such as do a time lapse on my Pi3, and send an email... perform some imagemagick work, render a video on my servers gpu, send an email... it would be easier for me to check emails looking for machines reporting in completion of tasks, sending logs as well for review etc... I got all of this set except the darn email.
Any help would be appreciated
I'm using this method to send mail in bash one-liner via smtp public service using "nail" mail-client:
echo 'Test mail body text' | env MAILRC=/dev/null from='a#a.ru' smtp=mail.a.ru smtp-auth-user='a' smtp-auth-password='xxx' smtp-auth=login mailx -v -n -s 'Test mail again v01' b#b.ru
You can try using mutt
To install in Ubuntu
sudo apt-get install mutt
To send an email with attachment
echo "Please find the file attached." | mutt -a somefile.ext -s "Important File"

Changing IP log with GPS information and mail. I need robustness

I've created a script in order to receive a mail with wan ip information and GPS location of my macbookpro. The content of the script is this:
#!/bin/bash
# -*- ENCODING: UTF-8 -*-
if [ ! -e /tmp/ip ]; then
curl -s icanhazip.com > /tmp/ip
fi
curl -s icanhazip.com > /tmp/ip2
newip=$(diff /tmp/ip /tmp/ip2 | wc -l)
if [ $newip -ne 0 ]; then
mv -f /tmp/ip2 /tmp/ip
date > IPlog.txt
curl -s icanhazip.com >> IPlog.txt
sudo ./Downloads/whereami >> IPlog.txt
mailx mymailadress#mail.com < IPlog.txt
rm IPlog.txt
else
rm /tmp/ip2
fi
Every minute the sistem executes this script that verifies if the wan ip has changed. If it has changed, the script send me a mail with the new information. The problems are:
1.- The mail is not always correctly sent. Sometimes I don't reveive it.
2.- The mail isn't contain all the info. Sometimes it includes only the new wan ip adress.
3.- Sometimes the mail is qualified as spam and I don't know why because the sender is always the same adress.
I have some suggestions to debug your problems.
First you should use a different location to store the ip than tmp. If your system wipes your tmp folder on boot and your system gets a new WAN ip after boot you would loose the previous recorded ip.
Check the exit code of mailx when sending using $?. 0 is ok. You could do a while loop and keep trying to send it until you get exit code 0.
You could add the info for the mail to a local variable instead of a file.
IPLog=`date`
IPLog+=`curl -s icanhazip.com`
The spam problem might be due to the IP address in the mail. Or whatever ./Downloads/whereami is adding to the file. Adding the sending email address as a trusted sender might do it.
Check the email header for information about spam score.

Resources