[e-mailing] send login pwd to several users


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [e-mailing] send login pwd to several users
# 1  
Old 02-11-2017
[e-mailing] send login pwd to several users

Hello,
I would like to send logins and passwords to users.
As input I have a file where there are the login, passwords and email addresses.

Can you help me because I'm not a good scripter
cordially

Code:
cat maillist.txt$1          $2            $3
login1    password1 @mail1
login2    password2 @mail2
login3    password3 @mail3

Code:
cat letter.txt
Hello,
Here is your login : $1 and your password : $2
cordially

Code:
vi my_script.sh#!/bin/bash
cat maillist.txt|while read user
do
cat letter.txt|mail -s "Your subject" $3
done
exit


Last edited by Scrutinizer; 02-11-2017 at 07:23 AM.. Reason: Repositioned [/CODE] markers that were in the wrong place
# 2  
Old 02-11-2017
Hi,

One thing to think about here is whether e-mailing these logins in the clear is actually the best way to distribute usernames and passwords to your users. Depending on your setup, there are likely to be various points at which someone could potentially sniff these messages in clear text as they're passing by, and thusly obtain your users' credentials.

However, if you are set on doing things this way, here's a solution.

Firstly, I've made a slight change to the format of letter.txt, so that it now looks like this:

Code:
$ cat letter.txt
Hello,
Here is your login : _USERNAME_ and your password : _PASSWORD_
cordially
$

So I'm using _USERNAME_ and _PASSWORD_ as placeholders here for what will be the real login details, as we'll see in a bit.

As for your maillist.txt, that's basically the same:

Code:
$ cat maillist.txt
unixforum password-U unixforum@localhost
root password-R root@localhost
$

So the first field is the username to be e-mailed, the second is the password to be e-mailed, and the third is the e-mail address to which the details should be sent.

Lastly, here's the actual script itself:

Code:
$ cat my_script.sh
#!/bin/bash

cat maillist.txt | while read -r line
do
        username=`echo $line | awk '{print $1}'`
        password=`echo $line | awk '{print $2}'`
        email=`echo $line | awk '{print $3}'`

        cat letter.txt | sed -e s/_USERNAME_/$username/g -e s/_PASSWORD_/$password/g | mail -s "Your subject" $email
done
$

So the idea is that we read in each line of maillist.txt, break out the username, password and e-mail address, do a search-and-replace in letter.txt to swap out the underscore-bracketed placeholders for the real details, then we e-mail them to the address on file.

And here's a sample session of it being run:

Code:
$ whoami
unixforum
$ ./my_script.sh
$ mail
"/var/mail/unixforum": 1 message 1 new
>N   1 unixforum@localhost Sat Feb 11 10:28  17/719   Your subject
? 1
<e-mail headers appear here, redacted>

Hello,
Here is your login : unixforum and your password : password-U
cordially
? d 1
? q
Held 0 messages in /var/mail/unixforum
$

Hope this helps.
# 3  
Old 02-11-2017
You CAN use $1 and $2 in your files when taking preventive measures, but it normally is not wise to use reserved words or similar. Slightly adapting drysdalk's proposal, try:
Code:
while read username password email
  do sed "s/\$1/$username/; s/\$2/$password/;" file1 | mail -s "Your subject" $email
  done < file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Send a mail to multiple users

I have a file 1.txt which has 3 mail ids as below: Maillist=abc@gmail.com def@gmail.com rcg@gmail.com Now I want to write a script which will read this file and send the mail to all the users present in this file. (6 Replies)
Discussion started by: Vivekit82
6 Replies

2. Shell Programming and Scripting

Send e-mail to users

Dear all, The perl script to send e-mail is working in my current script. #This part of the script will send the email notification my $to='mohamed.rahman@noridian.com'; my $from='xyz@hotmail.com'; my $subject='POS_CODES_38 DATA LOADED SUCCESSFULLY.'; my $message='The total no. of files... (2 Replies)
Discussion started by: msrahman
2 Replies

3. Shell Programming and Scripting

Challenging task : script for mailing process completion timing report to users.

Hi all, This is my first post. I am new to unix scripting. My requirement is as follows : We are using a financial backoffice application. Now at the end of day we have send users a status report stating all timings of EOD processes for all countries. I need timings for following... (0 Replies)
Discussion started by: ammbhhar
0 Replies

4. AIX

sync samba pwd with aix5.3 pwd

currently, my samba login works just fine. i want my clients to use aix5.3 account to login to samba so they don't have to change samba pwd and aix pwd. i googled, and vi /usr/lib/smb.conf per some of knowledge base, but i could not get to work. aix5.3 and samba 3.0.24.0 thanks in advace..... (2 Replies)
Discussion started by: tjmannonline
2 Replies

5. HP-UX

No users can login

Dear Forum, I had this case before, all of sudden all users including root can't login. What done is by connecting to console port and resetting root password, "pwunconv" command, reboot server. My question is, how this can happen??? thanks :confused: (3 Replies)
Discussion started by: irda
3 Replies

6. Shell Programming and Scripting

users login

Hello everyone I need to send to a file the last command from all users who log in and log out by week or month. My questions are I can do it with the command or I need to do a script ? If the answer is I need to do a script, someone can help me because Im complete new to make a... (3 Replies)
Discussion started by: lo-lp-kl
3 Replies

7. UNIX for Dummies Questions & Answers

to send email to multiple users

hi, i'm pretty new to this unix. i've been asked to create a shell script which will pick up the email id from a text file(stored in same machine, same directory) searches for that id in another file in which a product name( a one line text) is mentioned against it. then it should send a mail... (0 Replies)
Discussion started by: vishwas.shenoy
0 Replies

8. Linux

Users with login privileges

Hi. I need the name for one command which I cant remember. This command displays all users which can log in to the system, wether they are stored in /etc/passwd, NIS og LDAP. Does anyone remember what command this is? (2 Replies)
Discussion started by: sprellari
2 Replies

9. UNIX for Dummies Questions & Answers

send email to more users

Hi, If I want to send an email to serveral users, what is the correct way to do it? I know a little bit mail command. If I want to send email to John and Scott after UNIX shell scripts have completed the job. I type: mail -s 'Job has been completed successfully' john@yahoo.com < log.txt ... (4 Replies)
Discussion started by: duke0001
4 Replies

10. UNIX for Dummies Questions & Answers

How to send mail to multiple users ???

Hi All, How to send mails to multiple users using a single mailx command. Urgently require help Thanks in advance Rgds Arunava (3 Replies)
Discussion started by: arunava_maity
3 Replies
Login or Register to Ask a Question