Can't figure out how to send ^D (EOT) signal to mailx in bash script - bash

I'm writing a bash script to send me an email automatically.
Mailx requires an EOT or ^D signal to know the message body is over and it can send.
I don't want to hit ^D on the keyboard when I run script which is what it does now.
Here is my code:
#! /bin/bash
SUBJ="Testing"
TO="test#test.com"
MSG="message.txt"
echo "I am emailing you" >> $MSG
echo "Time: `date` " >> $MSG
mail -s "$SUBJ" -q "$MSG" "$TO"
rm -f message.txt

If you do not need to add more text and just need to send the content of $MSG, you can replace
mail -s "$SUBJ" -q "$MSG" "$TO"
with
mail -s "$SUBJ" "$TO" < "$MSG"
The EOT will be implicit in the < construct. -q is indeed only used to start a message. The rest is supposed to come through stdin.

Pipe the output of a command group to mail.
#! /bin/bash
SUBJ="Testing"
TO="test#test.com"
MSG="message.txt"
{
echo "I am emailing you"
echo "Time: `date` "
} | mail -s "$SUBJ" -q "$MGS" "$TO"
rm -f message.txt

Related

Password protected shell script

I want to make my script password protected. If I use this code it works:
ACTUAL="sam123"
read -s -p "Password: " enteredpass
I also want to protect the script from being read with cat and vi. I tried to use vim -x <script> to encrypt it but then it won't allow me to run it.
I am using a generic user and haven't gotten anywhere.
You can't do this securely without your sysadmin's help, but you can do something sorta-kinda-maybe-not-really-adequate without it.
So, let's say you create your script like so:
cat >myscript <<EOF
echo "Doing something super secret here"
EOF
...but you don't want anyone who doesn't know the password to run it, even if they're using a shared account. You can do this by encrypting it:
gpg -ac <myscript >myscript.asc
...and then embedding that plaintext into a script:
#!/usr/bin/env bash
{ gpg -d | bash -s "$#"; } <<'EOF'
-----BEGIN PGP MESSAGE-----
jA0EBwMCBogTuO9LcuZg0lsB2wqrsPU8Bw2DRzAZr+hiecYTOe//ajXfcjPI4G6c
P3anEYb0N4ng6gsOhKqOYpZU9JzVVkxeL73CD1GSpcQS46YlKWJI8FKcPckR6BE+
7vqkcPWwcS7oy4H2
=gmFu
-----END PGP MESSAGE-----
EOF
That said, other users in the shared account can still collect your password if they connect to and trace your process while it's running -- running strace on the copy of bash -s will show the text being fed into its stdin. In general, you shouldn't rely on shared accounts for anything that needs to remain confidential.
Late answer for posterity, how about using openssl? here's my scriptencrypt.sh
It generates a new .sh file that requires a password
#!/bin/bash
if [ -z "$1" ]; then echo "usage: $(basename $0) script"; exit 1; fi
script=$(cat "$1")
checksum="$(echo "$script" | md5sum | awk '{ print $1 }')"
extension=$([[ "$(basename $1)" =~ .\.. ]] && echo ".${1##*.}" || echo "")
cat << EOF > "${1%.*}.enc${extension}"
#!/bin/bash
read -r -d '' encrypted_script << EOF2
$(openssl aes-256-cbc -a -salt -in /dev/stdin -out /dev/stdout <<< "${script}")
EOF2
read -s -p "Enter script password: " password
echo
unencrypted_script=\$(openssl aes-256-cbc -d -a -salt -in /dev/stdin -out /dev/stdout <<< "\${encrypted_script}" -pass pass:"\${password}" 2>/dev/null | tr -d '\000')
clear
checksum="\$(echo "\$unencrypted_script" | md5sum | awk '{ print \$1 }')"
if [ "\${checksum}" = "${checksum}" ]; then
eval "\${unencrypted_script}"
exit 0
else
echo "Wrong password inserted"
exit 1
fi
EOF

sending multiple files as attachment in e-mail using mailx

I have a requiremnet to send multiple files as e-mail attachmnet in shell script. I have used below command.
(printf "%s\n" "BODY"; uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt ) | mailx -m -s "TEST" emailid#domain.com
However the number of files i want to send as an attachmnet are dynamic. So I want to assign the uuencode ... comand to a variable and then use it. I have tried below way,
$ ATTACH_CMD=$(echo `cat $OUTPUT_FILE`)
$ echo $ATTACH_CMD
uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt
$ (printf "%s\n" "BODY"; $ATTACH_CMD ) | mailx -m -s "TEST" emailid#domain.com
And i am getting below error.
sh: uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt: not found.
Can any one please help me with this? Thanks in advance.
Have you tried using the below code? Not sure why it works, but maybe the below code could be used as a workaround
(printf "%s\n" "BODY"; `echo $ATTACH_CMD` ) | mailx -m -s "TEST" emailid#domain.com`?
For $ATTACH_CMD I have used echo command.
I finally found the way. eval makes the trick
eval $STR
$ ATTACH_CMD=$(echo `cat $OUTPUT_FILE`)
$ echo $ATTACH_CMD
uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt
$ (printf "%s\n" "BODY"; eval $ATTACH_CMD ) | mailx -m -s "TEST" emailid#domain.com

Fail when sending email from bash using & (background)

When I'm sending an email using this command:
mail -s "export CSV" -a calls.tar mail#mail.pl < text.txt > /dev/null 2>&1
everything working fine. But when I'm using & at the end of the line :
mail -s "export CSV" -a calls.tar mail#mail.pl < text.txt > /dev/null 2>&1 &
query is running to infinity.
How can I run this query properly?
Ideally you should try to find what is happening, but if you're in a rush or everything is failing, you can try to just enclose everything in () and run that in the background. This should work.
$ ( mail -s "export CSV" -a calls.tar mail#mail.pl < text.txt > /dev/null 2>&1 ) &
In any case, you can always use debugging techniques and do your command increasing the complexity little by little to see where it fails.
$ mail -s "export CSV" mail#mail.pl &
$ mail -s "export CSV" mail#mail.pl < text.txt &
$ mail -s "export CSV" mail#mail.pl < text.txt > /dev/null &
$ mail -s "export CSV" mail#mail.pl < text.txt > /dev/null 2>&1 &
$ mail -s "export CSV" -a calls.tar mail#mail.pl < text.txt > /dev/null 2>&1 &
You get the idea. :)
1. As pointed out by psibar you should try redirecting output to a log:
mail -s "export CSV" -a calls.tar mail#mail.pl < text.txt >> /tmp/csv.log 2>&1 &
2. Are you sure the command never finishes? The expected output would be:
user#host:~$ mail -s "export CSV" -a calls.tar mail#mail.pl < text.txt > /dev/null 2>&1 &
[1] 19266
user#host:~$
and after you press ENTER:
[1]+ Done mail -s "export CSV" -a calls.tar mail#mail.pl < text.txt > /dev/null 2>&1 &
user#host:~$
Note: 19266 here is the PID of the background process. You can check if it's still running using
ps -ef | grep [1]9266

ftp while do error

Anyone can help what will be the problem?
Host='192.153.222.1'
User='ftpuser'
passwd='apple'
logfile='a.log'
while :; do
ftp -n -p -v $HOST < example.script >> $logfile
grep -qF "Connected" $logfile &&
grep -qF "File successfully transferred" $logfile && break
done
quote USER $USER
quote PASS $PASSWD
example.script contains
put example.txt
The error is
./example.sh: line 20: syntax error: unexpected end of file
Some fixes:
You missed the closing quote in:
Host='192.153.222.1'
Use a single <, otherwise it's an "here document" in:
ftp -n -p -v "$HOST" < example.script >> "$logfile"
Why you use << in this line?
ftp -n -p -v $HOST << example.script >> $logfile
Change it to
ftp -n -p -v $HOST < example.script >> $logfile
It will work :-)

Sending simple message body + file attachment using Linux Mailx [duplicate]

This question already has answers here:
How to attach a file using mail command on Linux? [duplicate]
(13 answers)
Closed 5 years ago.
I am writing a shell script to send an email using Linux Mailx, the email must contain a file attachment and a message body.
Currently sending an email with an attachment:
output.txt | mail -s "Daily Monitoring" james#dell.com
I wish to add a message body. How should i?
Linux Mailx:
mail [-eIinv] [-a header] [-b addr] [-c addr] [-s subj] to-addr
The usual way is to use uuencode for the attachments and echo for the body:
(uuencode output.txt output.txt; echo "Body of text") | mailx -s 'Subject' user#domain.com
For Solaris and AIX, you may need to put the echo statement first:
(echo "Body of text"; uuencode output.txt output.txt) | mailx -s 'Subject' user#domain.com
The best way is to use mpack!
mpack -s "Subject" -d "./body.txt" "././image.png" mailadress
mpack - subject - body - attachment - mailadress
Johnsyweb's answer didn't work for me, but it works for me with Mutt:
echo "Message body" | mutt -s "Message subject" -a myfile.txt recipient#domain.com
Try this it works for me:
(echo "Hello XYX" ; uuencode /export/home/TOTAL_SI_COUNT_10042016.csv TOTAL_SI_COUNT_10042016.csv ) | mailx -s 'Script test' abc#xde.com
On RHEL Linux, I had trouble getting my message in the body of the email instead of as an attachment . Using od -cx, I found that the body of my email contained several /r. I used a perl script to strip the /r, and the message was correctly inserted into the body of the email.
mailx -s "subject text" me#yahoo.com < 'body.txt'
The text file body.txt contained the char \r, so I used perl to strip \r.
cat body.txt | perl success.pl > body2.txt
mailx -s "subject text" me#yahoo.com < 'body2.txt'
This is success.pl
while (<STDIN>) {
my $currLine = $_;
s?\r??g;
print
}
;
You can try this:
(cat ./body.txt)|mailx -s "subject text" -a "attchement file" receiver#domain.com

Resources