how to terminate a process via key stroke - bash

I have this function on my bash script:
sudo tshark -i eth0 -T fields -e ip.src -e dns.qry.name -Y "dns.qry.name~." -q 1>>log.txt 2>/dev/null &
while true
do
cat log.txt
done
it is capturing ips and domain names in live mode and save them into log file.
how can configure this live mode to be terminated by pressing a key?

Using tee to watch log and send the command to background, then read input to terminate script
tshark -i eth0 -T fields -e ip.src -e dns.qry.name -Y "ip" -q 2>/dev/null | tee log.txt &
read -n1 c && echo "Got key $c"
exit
Note: running the command in a console will terminate it :-p

Related

Multiline heredoc with sudo in Dockerfile

We use our local repo in sources.list, and for 20.04 it required to add in apt.conf.d
Acquire::https::local_repo.local_host.com::Verify-Peer "false";
With Bash it works, as using,
sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null <<'EOF'
Acquire::https::local_repo.local_host.com::Verify-Peer "false";
EOF
But I don't find a solution to do it for Dockerfile.
I've tried it with different escape character/new line and so on, but always unsuccessful.
For example,
sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null <<'EOF' \
Acquire::https::local_repo.local_host.com::Verify-Peer "false"; \
EOF
Results - /bin/sh: 1: EOF: not found
To note that cat or echo is not an option, also adding those 3 line in a script is also not preferable.
If you only have one line to append then I wouldn't use a heredoc. It's simpler to use echo:
RUN echo 'Acquire::https::local_repo.local_host.com::Verify-Peer "false";' | \
sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null
Or cat:
RUN cat <<< 'Acquire::https::local_repo.local_host.com::Verify-Peer "false";' | \
sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null
Or send the string directly to sudo tee:
RUN sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null \
<<< 'Acquire::https::local_repo.local_host.com::Verify-Peer "false";'
Note that the latter two options may require you to also set SHELL /bin/bash since <<< is a bash-ism not available in plain sh.

Turn off the return message from the executed command

I'm developing a bash script, I've used ssh command in my bash script to run some commands on a remote server and I need to get the result from the command which runs on the remote server. so I wrote this code:
db="$(ssh -t user#host 'mysql --user=username -ppassword -e \"SHOW DATABASES;\" | grep -Ev \"(Database|information_schema|performance_schema)\"' | grep -Ev \"(mysql)\")"
But each time which I run my bash script, I will get Connection to host closed. in first of the db result. this is a default message from ssh command.
Also, If I use > /dev/null 2>&1 end of my command the db variable would be empty.
How can I turn off the return message from the executed command?
Like this :
#!/bin/bash
db=$(
ssh -t user#host bash<<EOF
mysql --user=username -ppassword -e "SHOW DATABASES" |
grep -Ev "(Database|information_schema|performance_schema|mysql)" \
2> >(grep -v 'Connection to host closed')
EOF
)
or if Connection to host closed comes from STDOUT :
...
mysql --user=username -ppassword -e "SHOW DATABASES" |
grep -Ev "(Database|information_schema|performance_schema|mysql|Connection to host closed)"
...

plink Unable to read from standard input: The handle is invalid

Batch file on WS 2008R2 Enterprise SP1, calling plink.exe (v.0.70.0.0), executing in ubuntu 16.04.4, intermittent failure Unable to read from standard input: The handle is invalid.
echo -e "youGuessedIt\n" | sudo -S nginx -t -c /home/userNoOne/Documents/nginx.conf &> /home/userNoOne/Documents/nginxResult.txt
echo -e "youGuessedIt\n" | sudo -S tail /var/log/nginx/error.log.1 >> /home/userNoOne/Documents/nginxResult.txt
echo -e "youGuessedIt\n" | sudo -S tail /var/log/nginx/error.log >> /home/userNoOne/Documents/nginxResult.txt
There is a known bug with Plink.exe that documents this failure. I'm looking for the simplest work-around:
https://www.chiark.greenend.org.uk/~sgtatham/putty/wishlist/win-plink-stdin-handle-invalid.html

tee not working with ssh running an external script

I'm trying to log everything happening during an ssh session while showing output on shell.
sshpass -p "password" ssh -tt -o ConnectTimeout=10 -oStrictHostKeyChecking=no username#"$terminal" 'bash -s' < libs/debug-mon.lib "$function" | grep -E '^INFO:|^WARNING:' || echo "WARNING: Terminal not reacheable or wrong IP" | tee -a libs/debug-monitor-logs
I'm not getting anything on the log libs/debug-monitor-logs file
Could you please help me to see where the issue is?
Thanks
looks like this only thing you will ever write into the log file is "WARNING: Terminal not reacheable or wrong IP"
try something like this
(command-that-might-fail || echo error message) | tee -a log-file
instead of
commant-that-might-fail || echo error message | tee -a log-file
(put the whole expression in brackets that you want to pipe into tee)

Bash piping issue

I need to execute the following grep query as an argument for konsole (the kde terminal)
grep -R -i -n -A 2 -B 2 --color=always -R "searchtext" * | less -R
works for the current terminal.
konsole --workdir `pwd` -e grep -R -i -n -A 2 -B 2 --color=always -R "searchtext" * | less -R
works, but the konsole window displays the grep query without less pipe.
Ideally I want konsole to spawn as seperate process with konsole &
and send the grep command with less as an argument for konsole -e
You need to run the pipe in a shell.
konsole --workdir pwd -e bash -c 'grep -R -i -n -A 2 -B 2 --color=always -R "searchtext" * | less -R'

Resources