Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Generate E-Mails with content on different files

Shell Programming and Scripting


Tags
cron, linux, password, script, sh

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-06-2017   -   Original Discussion by Manu_H
Manu_H Manu_H is offline
Registered User
 
Join Date: Jan 2017
Last Activity: 6 January 2017, 4:48 PM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Unix or Linux Question Generate E-Mails with content on different files

Hello,

I am using a script to create a random password for a user.
The file looks like

Code:
 
user1:thepassword1  
user2:thepassword2  
user3:thepassword3

Passwords are openssl passwd hashed. I have an own file where the password is written in cleartext.
Now i want to send an E-Mail to the users with the new password.
I have a file (maillist.txt) with


Code:
 
user1:mail@domain1.tld  
user2:mail@domain2.tld  
user3:mail@domain3.tld

Now i want to generate an E-Mail to user1.
This should generate an E-Mail to mail@domain1.tld, containing the username (user1) and the password (password1). Automatic for each user.
I want to do it with mailx.
Can you please help me?
Regards
Manu



Moderator's Comments:
Generate E-Mails with content on different files Please use CODE tags as required by forum rules!

Last edited by Manu_H; 01-06-2017 at 01:09 PM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 01-06-2017   -   Original Discussion by Manu_H
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 21 November 2017, 3:22 PM EST
Location: Saskatchewan
Posts: 22,518
Thanks: 1,154
Thanked 4,273 Times in 3,946 Posts
Hashing is not reversible, the hash file is useless for anything but verifying a password someone gives to you.


Code:
while IFS=":" read USERID EMAIL
do
        PASS="$(awk -F: '$1 == USERID { print $2 }')
        if [ -z "$PASS" ]
        then
                echo "No pass for user $USERID" >&2
                continue
        fi

        mailx -s "password for $USERID" EMAIL <<EOF
From:  someone@myemailserver
Subject:  Automatic password
Your automatically generated password is $PASS
EOF

        [ "$?" -eq 0 ] || echo "Couldn't email password for $USERID" >&2
done < emailfile

Test this with example data before you try it for real.
Sponsored Links
    #3  
Old Unix and Linux 01-06-2017   -   Original Discussion by Manu_H
Manu_H Manu_H is offline
Registered User
 
Join Date: Jan 2017
Last Activity: 6 January 2017, 4:48 PM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Unix or Linux Question

Thank you.
I' am using this Script right now...

Code:
join -t: cleartext.txt maillist.txt | awk -F: '{ print "echo Username: "$1" Passwd: "$2" | mailx -s Information "$3 }' |sh

$1 is the Username from Cleartext.txt
$2 is the Password from Cleartext.txt
$3 is the email from the user in maillist.txt
This works fine like this, but i want to add more text (Something like "Hello, your password has been..."). But if i do this, i get an error: i get an unexpected error.
Script from above changed to... { print "echo Blablabla \n test Username: ...}
    #4  
Old Unix and Linux 01-06-2017   -   Original Discussion by Manu_H
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 21 November 2017, 3:22 PM EST
Location: Saskatchewan
Posts: 22,518
Thanks: 1,154
Thanked 4,273 Times in 3,946 Posts
An awk script which generates shell commands is probably not the greatest idea, it would be a security and quoting nightmare. It's not the use of awk or sh, as much as the way they're joined, that's the problem. What if someone's password happens to be '" ; sleep 900000 ? That would be fun. Handle the reading yourself, so that there's no opportunity for arbitrary execution.

I'd forgotten about join, though, that simplifies my script a lot:


Code:
join -t: cleartext.txt maillist.txt | while IFS=":" read -r USERID PASS EMAIL
do
        mailx -s "password for $USERID" EMAIL <<EOF
From:  someone@myemailserver
Subject:  Automatic password
Your automatically generated password is $PASS
This email can contain as many lines as you want
etc
etc
EOF
done


Last edited by Corona688; 01-06-2017 at 05:10 PM.. Reason: fix typo: joint/join
Sponsored Links
    #5  
Old Unix and Linux 01-06-2017   -   Original Discussion by Manu_H
Manu_H Manu_H is offline
Registered User
 
Join Date: Jan 2017
Last Activity: 6 January 2017, 4:48 PM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Hey, thats perfect! Thanks.
Is there just a possibilty to set to "TO" E-Mail to the Adress from the maillist.txt?
Currently all Mails are send to correct recipient, but the "TO" Field is "EMAIL@hostname"@domain.tld.
And where do you define the Variables? (Like $PASS, $USERID etc?)

I edited the Script by adding a -r, to show an sender mail.


Code:
join -t: cleartext.txt maillist.txt | while IFS=":" read -r USERID PASS EMAIL
do
        mailx -r "donotreply@domain.tld" -s "password for $USERID" EMAIL <<EOF
Hello $USERID,

Your automatically generated password is $PASS
EOF
done


Last edited by Manu_H; 01-06-2017 at 05:04 PM..
Sponsored Links
    #6  
Old Unix and Linux 01-06-2017   -   Original Discussion by Manu_H
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 21 November 2017, 3:22 PM EST
Location: Saskatchewan
Posts: 22,518
Thanks: 1,154
Thanked 4,273 Times in 3,946 Posts
read defines them, setting them for every line read. It takes the variable NAME, not the variable itself, which is why its read -r VARNAME and not read -r $VARNAME

Try putting it in with the email, like:


Code:
mailx -s "password for $USERID" EMAIL <<EOF
To:  $EMAIL
From:  someone@myemailserver
Subject:  Automatic password

Your automatically generated password is $PASS
This email can contain as many lines as you want
etc
etc
EOF

p.s. Do not indent the email contents to make them look prettier in the script, it won't work right if you indent it, especially the ending EOF. That has to be at the literal beginning of the line to properly end the here-document.
Sponsored Links
    #7  
Old Unix and Linux 01-06-2017   -   Original Discussion by Manu_H
Manu_H Manu_H is offline
Registered User
 
Join Date: Jan 2017
Last Activity: 6 January 2017, 4:48 PM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Actually not.
The Mail is still from "EMAIL@hostname"@domain.tld
The E-Mail just looks like:


Code:
To:  test@manuh.xyz 
From: someone@myemailserver   
Subject:  Automatic password    
Your automatically generated password is test   
This email can contain as many lines as you want  
etc   
etc

Normally i use mailx like that.
mailx -r sender@domain.tld -s "Mail Subject" recepient@domain.tld

I found another way..
How would this be (security`?)


Code:
#!/bin/bash
>tmp.txt
join -t: cleartext.txt maillist.txt | while read line
do
  echo $line
  USER=$(echo $line | awk -F: '{ print $1 }')
  PSWD=$(echo $line | awk -F: '{ print $2 }')
  EMAILID=$(echo $line | awk -F: '{ print $3 }')
  echo "Hello $(echo $USER | tr a-z A-Z), your password has been changed!!!<br>" >tmp.txt
  echo "<b>Username</b>: $USER<br>" >>tmp.txt
  echo "<b>Password</b>: $PSWD<br>" >> tmp.txt
  cat tmp.txt | mailx -s "$(echo -e 'Password Changed!!!\nContent-Type: text/html')" $EMAILID
done


Last edited by Manu_H; 01-06-2017 at 05:21 PM..
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Merge files and generate a resume in two files jiam912 Shell Programming and Scripting 14 12-04-2013 06:03 PM
Script for count files and send mails atrailm Shell Programming and Scripting 2 12-12-2012 12:20 PM
Generate list of deleted files eshwaconsulting UNIX for Dummies Questions & Answers 1 02-01-2012 01:13 PM
Script to consolidate files as mails,groupid manish123456 Shell Programming and Scripting 7 08-31-2010 10:12 PM
how to generate a list of files jasongr Shell Programming and Scripting 3 12-13-2005 08:15 AM



All times are GMT -4. The time now is 04:15 PM.