Friday, May 3, 2024
9
rated 0 times [  9] [ 0]  / answers: 1 / hits: 1218  / 2 Years ago, thu, september 8, 2022, 3:45:19

What I need


I have an existing script that pulls port information for domains and stores it into a text file called portscan.txt. Example:


portscan.txt file:


somedomain.com:80
somedomain.com:443

I want to delete the information only if certain conditions are met. These conditions include:



  • The file with the domains should have 2 or less lines

  • The ports should only be 80 or 443 (i.e., I don't want to delete the file if 8080, 8443, or any other port exists in the file).


Note: So basically, the example file provided above should be deleted, but I do not want to delete the file if there are 2 lines, but the ports are 8080 or 8443 (or any other port for that matter)
Example like so:


somedomain.com:8443
somedomain.com:443

This should not be deleted.


What I tried


I attempted scripting this out and here's what I have:


#!/bin/bash

lines=$(cat portscan.txt | wc -l)
ports=$(cat portscan.txt | grep -Pv '(^|[^0-9])(80|443)($|[^0-9])')


if [[ $lines < 3 ]] && [[ $ports != 80 ]]; then
if [[ $ports != 443 ]]; then
echo "I need to delete this"
fi
else
echo "I will NOT delete this..."
fi

This is the second rendering of the script, I attempted nested if statements because I was unable to do a condition like this:


IF portscan.txt is less than two lines AND the ports are NOT 80 OR 443


I also attempted this in a much simpler manner like so:


#!/bin/bash

lines=$(cat portscan.txt | wc -l)
ports=$(cat portscan.txt | grep -Pv '(^|[^0-9])(80|443)($|[^0-9])')


if [[ $lines < 3 ]] && (( $ports != 80||443 )); then
echo "I need to delete this"
else
echo "I will NOT delete this..."
fi

I have tried the (( because I read that this is better to be used with arithmetic functions -- which is what I thought I needed, but I'm not as bash savvy with conditional arguments when it should be like: "This and that or that".


Hopefully this makes sense, any help would be greatly appreciated!


More From » command-line

 Answers
7
checkfile() {
awk '
BEGIN {
FS = ":"
status = 1
ports[80] = 1
ports[443] = 1
}
NR == 3 || !($2 in ports) {status = 0; exit}
END {exit status}
' "$1"
}

file=portscan.txt
checkfile "$file" || echo rm -- "$file"

That awk command will exit with status 0 if the file has a 3rd line, or if it sees a "non-standard" port.


If the function returns non-zero (the file has <= 2 lines and only "standard" ports), then the rm command is printed.


Remove echo if the results look right.




Alternately:


checkfile() {
# if more than 2 lines, keep the file
(( $(wc -l < "$1") > 2 )) && return 0

# if a "non-standard" port exists, keep the file
grep -qv -e ':80$' -e ':443$' "$1" && return 0

# delete the file
return 1
}

or, more tersely


checkfile() {
(( $(wc -l < "$1") > 2 )) || grep -qv -e ':80$' -e ':443$' "$1"
}

[#989] Saturday, September 10, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ampolinhad

Total Points: 88
Total Questions: 100
Total Answers: 116

Location: South Georgia
Member since Tue, Feb 1, 2022
2 Years ago
ampolinhad questions
Tue, Aug 10, 21, 20:03, 3 Years ago
Sat, Oct 16, 21, 22:44, 3 Years ago
Sat, Oct 23, 21, 03:11, 3 Years ago
Thu, Nov 17, 22, 15:34, 1 Year ago
Mon, Nov 14, 22, 22:15, 2 Years ago
;