Merge two Batch Files into one - windows

i have written a small script that is taking infos from an console application and sending it via email to me. I have used Quiet to let the programm run in the background.
start.bat
Quiet script.bat
script.bat
info.exe > "data.txt"
sendEmail -o tls=yes -f myemail#gmail.com -t myemail#gmail.com -s smtp.gmail.com:587 -xu myemail#gmail.com -xp mypasswd -u "XXX" -m "XXX" -a "data.txt"
del "data.txt"
if i run it like this it works fine but when i try to make one file of it like:
script.bat
Quiet script.bat
info.exe > "data.txt"
sendEmail -o tls=yes -f myemail#gmail.com -t myemail#gmail.com -s smtp.gmail.com:587 -xu myemail#gmail.com -xp mypasswd -u "XXX" -m "XXX" -a data.txt"
del "data.txt"
It sends infinte amount of emails. When i make it like this it works but not like i want it to.
script.bat
Quiet script.bat
pause
info.exe > "data.txt"
sendEmail -o tls=yes -f myemail#gmail.com -t myemail#gmail.com -s smtp.gmail.com:587 -xu myemail#gmail.com -xp mypasswd -u "XXX" -m "XXX" -a data.txt"
pause
del "data.txt"

The reason for the infinite mails is that you call the batch file inside the batch file, which causes an infinite loop of running the batch.
The problem is with the logic of what that you are expecting to happen,
you want to click the batch file and expect that it will run by Quiet before it runs in shell.
If you just want to run it in background, you can easily achieve it by running it as "System"(with a service/schedule task and etc.), otherwise I think that you can't resolve this issue(you expect that another executable will run the current batch you are clicking - the first batch will always open a window).

Because you are clicking to start the program, the shortcut can have the launching command in it, changed in the properties.
"c:\folder\Quiet.exe" "c:\batch\folder\script.bat"
and then there is just the one batch file.

UPDATE
Not sure I understood everything.
One way would be in script.bat
Probably echo are needed in front of each command. if it doesn't work, try in one line
#echo off
(
echo info.exe > "data.txt"
echo sendEmail -o tls=yes -f myemail#gmail.com -t myemail#gmail.com -s smtp.gmail.com:587 -xu myemail#gmail.com -xp mypasswd -u "XXX" -m "XXX" -a "data.txt"
echo del "data.txt"
) > output.txt 2>&1 | Quiet.exe
Or in one liner
#echo off
(
echo info.exe > "data.txt" &&^
echo sendEmail -o tls=yes -f myemail#gmail.com -t myemail#gmail.com -s smtp.gmail.com:587 -xu myemail#gmail.com -xp mypasswd -u "XXX" -m "XXX" -a "data.txt" &&^
echo del "data.txt" &&^
) > output.txt 2>&1 | Quiet.exe
In this way, start.bat is not necessary.
Which is equivalent to this:
Quiet.exe info.exe > "data.txt" && sendEmail -o tls=yes -f myemail#gmail.com -t myemail#gmail.com -s smtp.gmail.com:587 -xu myemail#gmail.com -xp mypasswd -u "XXX" -m "XXX" -a "data.txt" && del "data.txt"
EDIT:
#echo off
Quiet start cmd /c "info.exe > 'data.txt' && sendEmail -o tls=yes -f myemail#gmail.com -t myemail#gmail.com -s smtp.gmail.com:587 -xu myemail#gmail.com -xp mypasswd -u "XXX" -m "XXX" -a 'data.txt' && del 'data.txt'"
Pause
In the last example, you have to deal with double quotes, probably not necessary.

Related

Subscribe and Publish in BASH like a receive Buffer

I want to buffer all messages of a subscription during processing publish messages.
The reason is a firmware update over MQTT. I want to send data with crc and the microcontroller have to verify the content and need to answer with new address pointer.
For now, i got it working without any interaction from the microcontroller. If size doesn't fit, or mc is not online, my bash script sends all the data of the binary file into the nirvana.
If I publish a message and subscribe directly after the publish, it is possible to lose an answer very easily.
But I'm not so experienced with bash and I need some suggestions how to do this...
This is what I have:
mosquitto_pub -h $MQTT_SERVER -t "spiffs/${client_name}/file_info" -m "$binary_file" -p 8883 -i "fw_server" --cafile server-cert.pem -d --insecure -u $MQTT_USER -P $MQTT_PW
mosquitto_sub -h $MQTT_SERVER -p 8883 -t "+/${client_name}/#" -i "fw_server" --cafile server-cert.pem -d --insecure -u $MQTT_USER -P $MQTT_PW | while read -r payload
do
fp_jq_rspCode=".upload.fp" # file pointer
address=$(echo $payload | jq -r $fp_jq_rspCode)
echo "${payload}" | jq -c '.[]'
echo "address pointer:$address"
c=${address}
addr=$(printf '%08x' "$c")
content=$(xxd -p -c $payload_size -l $payload_size -seek $c $binary_file)
length=`expr length "$content"`
len=$(printf '%04x' "$length")
crc32=$(echo -n "$content" | gzip -c | tail -c8 | hexdump -n4 -e '"%u"')
crc32=$(printf '%08x' "$crc32")
echo "$addr $len $content $crc32"
message=$(printf '%08x%04x%s%s' "$c" "$length" "$content" "$crc32")
mosq=$(mosquitto_pub -h $MQTT_SERVER -t "spiffs/${client_name}/upload" -m "$message" -p 8883 -i "fw_server" --cafile server-cert.pem -d --insecure -u $MQTT_USER -P $MQTT_PW)
done

Download music from youtube with batch file

I made a script for downloading music from Youtube using youtube-dl. It worked fine until an update of some kind.
#!/bin/bash
read -p ' Title : ' title
read -p 'Artist : ' artist
read -p ' Album : ' album
read -p ' Genre : ' genre
youtube-dl "$1" --quiet --extract-audio --audio-format mp3 --audio-quality 3 --embed-thumbnail --exec 'mp3gain -q -r -c -s i {} > /dev/null & echo -n {}' | xargs -0 mid3v2 -t "$title" -a "$artist" -A "$album" -g "$genre"
echo --- DONE! ---
My beleive is that it is the mp3gain program that is causing the problem and I don't really care about it. I've tried to change the --exec line to:
--exec mid3v2 -t "$title" -a "$artist" -A "$album" -g "$genre"
with no luck.
I liked it because with the command:
"download.bat youtube-url"
I got an mp3 with the sound and a thumbnail.
How could I make a change for it to work?
You are almost close. Would you please change the youtube-dl line as:
youtube-dl "$1" --quiet --extract-audio --audio-format mp3 --audio-quality 3 --embed-thumbnail --exec 'mp3gain -q -r -c -s i {} > /dev/null 2>&1 && printf "%s\0" {}' | xargs -0 mid3v2 -t "$title" -a "$artist" -A "$album" -g "$genre"
mp3gain outputs some messages via stderr. You will need to suppress
them with 2>&1.
It is a good practice to add -0 option to xargs. Then you need to
terminate the filename with a null character.

Weird output observed on executing ssh commands remotely over ProxyCommand

Team, I have two steps to perform:
SCP a shell script file to remote ubuntu linux machine
Execute this uploaded file on remote ubuntu linux machine over SSH session using PROXYCommand because I have bastion server in front.
Code:
scp -i /home/dtlu/.ssh/key.key -o "ProxyCommand ssh -i /home/dtlu/.ssh/key.key lab#api.dev.test.com -W %h:%p" /home/dtlu/backup/test.sh lab#$k8s_node_ip:/tmp/
ssh -o StrictHostKeyChecking=no -i /home/dtlu/.ssh/key.key -o 'ProxyCommand ssh -i /home/dtlu/.ssh/key.key -W %h:%p lab#api.dev.test.com' lab#$k8s_node_ip "uname -a; date;echo "Dummy123!" | sudo -S bash -c 'echo 127.0.1.1 \`hostname\` >> /etc/hosts'; cd /tmp; pwd; systemctl status cachefilesd | grep Active; ls -ltr /tmp/test.sh; echo "Dummy123!" | sudo -Sv && bash -s < test.sh"
Both calls above are working fine. I am able to upload test.sh and also its running but what is bothering me is during the process am observe weird output being thrown out.
output:
/tmp. <<< expected
[sudo] password for lab: Showing one
Sent message type=method_call sender=n/a destination=org.freedesktop.DBus object=/org/freedesktop/DBus interface=org.freedesktop.DBus member=Hello cookie=1 reply_cookie=0 error=n/a
Root directory /run/log/journal added.
Considering /run/log/journal/df22e14b1f83428292fe17f518feaebb.
Directory /run/log/journal/df22e14b1f83428292fe17f518feaebb added.
File /run/log/journal/df22e14b1f83428292fe17f518feaebb/system.journal added.
So, I don't want /run/log/hournal and other lines which don't correspond to my command in sh.
Consider adding -q to the scp and ssh commands to reduce the output they might produce. You can also redirect stderr and stdout to /dev/null as appropriate.
For example:
{
scp -q -i /home/dtlu/.ssh/key.key -o "ProxyCommand ssh -i /home/dtlu/.ssh/key.key lab#api.dev.test.com -W %h:%p" /home/dtlu/backup/test.sh lab#$k8s_node_ip:/tmp/
ssh -q -o StrictHostKeyChecking=no -i /home/dtlu/.ssh/key.key -o 'ProxyCommand ssh -i /home/dtlu/.ssh/key.key -W %h:%p lab#api.dev.test.com' lab#$k8s_node_ip "uname -a; date;echo "Dummy123!" | sudo -S bash -c 'echo 127.0.1.1 \`hostname\` >> /etc/hosts'; cd /tmp; pwd; systemctl status cachefilesd | grep Active; ls -ltr /tmp/test.sh; echo "Dummy123!" | sudo -Sv && bash -s < test.sh"
} >&/dev/null

Using bash and ssh how do I write a log locally from a remote host

I am trying to get data from a file from a remote host and write to a log file locally using SSH. The log file tmp_results.log is not being created. Any ideas where I 'm going wrong please?
( ssh -nq -o StrictHostKeyChecking=no \
-i $PEM_PATH/$PEM_FILE $USER#${host} -p $REMOTE_PORT \
tail -n 6 $REMOTE_HOME/data/result.jtl | >> $SCRIPT_DIR/$project/tmp_results.log)
You seems a little bit confused by using pipes and redirections of filedescriptors.
Here you write in your logfile:
ssh -nq -o StrictHostKeyChecking=no \
-i $PEM_PATH/$PEM_FILE $USER#${host} -p $REMOTE_PORT \
tail -n 6 $REMOTE_HOME/data/result.jtl > $SCRIPT_DIR/$project/tmp_results.log
If you want to append the output on existing file just use:
ssh -nq -o StrictHostKeyChecking=no \
-i $PEM_PATH/$PEM_FILE $USER#${host} -p $REMOTE_PORT \
tail -n 6 $REMOTE_HOME/data/result.jtl >> $SCRIPT_DIR/$project/tmp_results.log

wget: How can I download files but no folders via FTP?

I need to download the files located in the root of in my FTP server, but not the folders, is there a way to do that?
I'm currently connecting with this string:
wget --timeout 20 -m -nH --user "user" --password "pass" ftp://123.123.com
Thanks.
wget doesn't have filtering capabilities.
However you can create a simple script to collect the files first and then get them using wget.
echo open $HOST > ftp.txt
echo user $USER $PASS >> ftp.txt
echo ls >> ftp.txt
echo bye >> ftp.txt
ftp < ftp.txt | awk '^-' | awk '{print $9}' > files.txt
wget --timeout 20 -m -nH --user "user" --password "pass" -i files.txt ftp://123.123.com

Resources