Exclude some errors from python script in linux - linux

I want to exclude some erros from log.
I'm using a Raspberry 3b+.
I use
main.py >> log.txt 2>&1 | grep -v "Network is unreachable"
My error that I don't want is
[tcp # 0x19ccea0] Connection to tcp://192.168.1.32:554?timeout=0 failed: Network is unreachable
I can put the whole error in grep because the ip adress may change
However this error is still in my log.txt file when I run my main.py

You need to pipe to grep, and then redirect grep's output to the file. You're redirecting all the output to the file, so nothing goes to grep.
main.py 2>&1 | grep -v "Network is unreachable" >> log.txt

Related

"permission denied error" but script works perfectly

I was watching the infamous beginners' network pentesting video of Heath Adams and was attempting to make nmap staging script.
Can someone explain why I am getting this irksome permission denied error where I defined the ports variable even though my script has been running without a hitch up until this point?
Here is the staging script I am attempting:
#!/bin/bash
#creating a temp directory to store the output of initial scan
mkdir tempStager
#scannig with given flags and storing the results
echo beginning nmap scan
nmap $* > tempStager/scan.txt
echo basic nmap scan complete
#retrieving open ports
cat tempStager/scan.txt |grep tcp |cut -d " " -f 1| tr -d "/tcp" > tempStager/ports.txt
sleep 2
ports=cat tempStager/ports.txt| awk '{printf "%s,",$0}' tempStager/ports.txt
ip=echo $* | awk 'NF{ print $NF }'
#scanning with -A
#echo ""
#echo starting nmap scan with -A
#nmap -A -p$ports $ip
#removing temp directory
#rm -r tempStager```
ports=cat tempStager/ports.txt| awk '{printf "%s,",$0}' tempStager/ports.txt
assigns the variable the value "cat" and then tries to execute tempStager/ports.txt as an executable. But the file is not an executable (it doesn't have the x bit set, so it cannot be executed.
ports only exists for the runtime of the (would-be) program, it is not available after the program has terminated (which the program does immediately, because your shell fails to run it).
You are also specifying stdin and a file to awk.
If you want to assign the output of awk to a variable, you must use command substitution:
ports="$(awk '{printf "%s,",$0}' tempStager/ports.txt)"

Why is the shell output txt file empty?

Kindly assist. I am working on a script that will perform a telnet test to a specific ip address on a specific TCP port and below is my script.
#! /bin/sh
nc -z -v -w5 192.168.88.55 3389 | tee results.txt
During execution, a "results.txt" file is created but it is empty. I want it to have the output of the script after execution.
I have managed to resolve it by making the below modifications to the script
#! /bin/sh
nc -z -v -w5 192.168.88.55 3389 2>&1 | tee results.txt
sleep 5
exit
It is now able to write the output to the results.txt file.
Thank you.

Extracting IP address from cron not working

I have a script and I am running it as scheduled via cron. In that script I have line to extract IP address of that machine and that line is below
ip_address=$(ip addr show | grep -E -o '(inet ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}))' | grep -v 'inet 127.0.0.1' |
sed 's/inet//g')
If I am just executing above line on bash command line its working and echo $ip_address giving me IP of that machine but If I am calling it via crobjob , echo $ip_address giving empty output.
even If I call entire script from bash shell, I am getting IP in the output but only via cron I am not getting the IP in the output.
Could someone please help ?
Thank you.

tee command not working when run in background

I want a daemon that continuously watches a named pipe for input, and then creates two files: one that contains all the data that went through the named pipe, and one that contains a filtered version of it.
When I run this command from the command line it works as intended, $ACTIONABLE_LOG_FILE is created as a filtered version of $LOG_FILE_BASENAME:
cat $NAMED_PIPE | tee -a "$LOG_FILE_BASENAME" | grep -P -v "$EXCEPTIONS" >> "$ACTIONABLE_LOG_FILE" &
But when I leave the following code running in the background, nothing gets appended to $ACTIONABLE_LOG_FILE:
while true
do
cat $NAMED_PIPE | tee -a "$LOG_FILE_BASENAME" | grep -P -v "$EXCEPTIONS" >> "$ACTIONABLE_LOG_FILE" &
wait $!
done
The file $ACTIONABLE_LOG_FILE gets created, but nothing gets appended to it. What's going on here?
My suspicion would be that when daemonized, you do not have a full environment available, and hence no $PATH. The full path to the command (likely /usr/bin/tee) might help a lot. You can confirm that locally with which tee.

if command "cat /dev/net/tun" result $string then

I'm creating a script which check if a VPS do have TUN driver enabled.
The check command is :
cat /dev/net/tun
if it return:
cat: /dev/net/tun: File descriptor in bad state
the module is enabled. otherwise return ERROR.
Here is my script:
tunstring="File descriptor in bad state"
if cat /dev/net/tun | grep -q "$tunstring"; then
echo "GOOOOOD"
else
echo "ERROR"
fi
I get ERROR message.
I tried the same script with a text file and it worked...
Since that output is being written on stderr you can use:
tunstring="File descriptor in bad state"
if cat /dev/net/tun |& grep -q "$tunstring"; then
echo "GOOOOOD"
else
echo "ERROR"
fi
|& pipes previous command's stdout and stderr to next in pipe line.
Looks like your VPS path i.e /dev/net/tun isn't valid anymore and cat command is failing to read it.

Resources