Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Run Linux command in Perl script as different user

Posted on 2007-12-04
4
Medium Priority
?
4,544 Views
Last Modified: 2013-12-17
I think this is more a Linux command question, but it resides in a perl script so I'll post in both...

I am running a perl script that automates a linux server account setup and then emails the users (using the mail command) to let them know that the account is ready.  Currently, to send an email I do the following:

             # Open for mail output
             open(MAIL, "|mail -s 'Accout is Ready' -b $blindCopyEmails $userEmail ");
             # add message for mail
             print MAIL $emailMessage;
             # Close/Send mail
             close(MAIL);

When the mail is sent out, it is sent from the root account because the automated perl script is ran by the root account, so the email shows from root@thisdomain.com, but I want it to come from another user account.  If I can run the "mail" command as another user, then it will send the email from that user.  I don't believe the mail command has an arguement for changing the from: field, only the subject, cc, bcc, and sendto.  It is required that the actual perl script is run by the root user.

Any suggestions??  

Thanks,
LTCJ
0
Comment
Question by:LocoTechCJ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 21

Expert Comment

by:from_exp
ID: 20405457
0
 
LVL 84

Expert Comment

by:ozo
ID: 20405528
         POSIX::setuid()
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 20405549
Assuming you have sendmail on your computer, you could use sendmail instead of mail:

open (MAIL, "|/usr/sbin/sendmail -t") or die "Could not open sendmail: $!\n";
print MAIL "From: user\@thisdomain.com\n";
print MAIL "To: $userEmail\n";
print MAIL "BCC: $blindCopyEmails\n"
print MAIL "Subject: Account is Ready\n\n";
print MAIL $emailMessage;
close(MAIL) || die "Error closing mail: $!";

0
 
LVL 8

Author Comment

by:LocoTechCJ
ID: 20405835
I had considered using sudo, but it would only give me permission errors running the mail command.

Adam314:  That's a great idea... not sure why I hadn't thought of that.   That works great.  Thanks.

PS:  Must add a ; on the BCC line.  I was scratching my head at first, wondering why it wasn't working. :)

--LTCJ
0

Featured Post

Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
The new Gmail Phishing Scam going around is surprising even the savviest of users with its sophisticated techniques.
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question