Bash-common-checks: Difference between revisions
Jump to navigation
Jump to search
m (→Make a banner) |
mNo edit summary |
||
| Line 63: | Line 63: | ||
skull | skull | ||
</pre> | </pre> | ||
====Create a CRC16 against string==== | |||
* use caution in creating unique values < 65k, as there can be slop and overlap | |||
* Good quick and dirty creation that is reproducible | |||
<pre> | |||
STRING=foo | |||
crc16() { | |||
while read -r -d "" -n 1 ; do astring+=( "$REPLY" ) ; done <<< "$1" | |||
cnt=${#1} | |||
c=65535 | |||
for ((x=0;x<$cnt;x++)); do | |||
char=$(printf '%d' \'"${1:$x:1}") | |||
e=$(((c ^ char) & 0x00FF)) | |||
s=$(((e << 4) & 0x00FF)) | |||
f=$(((e ^ s) & 0x00FF)) | |||
r1=$(((c >> 8) ^ (f << 8) ^ (f << 3) ^ (f >> 4))) | |||
c=$r1 | |||
done | |||
c=$((c ^ 0xffff)) | |||
echo "$c" | |||
} | |||
crc16 ${STRING} | |||
returns value 26676 | |||
</pre> | |||
[[Category: Bash]] | [[Category: Bash]] | ||
Revision as of 12:42, 19 October 2021
Common checks and validations that are a PITA to remember
Is Integer?
- Check if a var is an integer
- support negative as well as positive integer values using extended regex
VAR=123
if [[ ${VAR} =~ ^-?[0-9]+$ ]]; then
echo "Is integer"
else
echo "Is not integer"
fi
TCP port check
- leverage /dev
- use timeout for sanity
- Use for any kind of TCP knock
- do not use FQDN, go by IP
IP=192.168.15.100
timeout 1 bash -c "</dev/tcp/${IP}/22"
if [[ $? -ne 0 ]]; then
echo "ssh port closed at $IP"
else
echo "ssh port open at $IP"
fi
Find what using inodes
- use sudo if you are going against /
find / -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n
Make a banner
skull(){
cat << EIEIO
*************************************************************
* *
* .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-. *
* | ______ | *
* | .-" "-. | *
* | / \ | *
* | _ | | _ | *
* | ( \ |, .-. .-. ,| / ) | *
* | > "=._ | )(__/ \__)( | _.=" < | *
* | (_/"=._"=._ |/ /\ \| _.="_.="\_) | *
* | "=._"(_ ^^ _)"_.=" | *
* | "=\__|IIIIII|__/=" | *
* | _.="| \IIIIII/ |"=._ | *
* | _ _.="_.="\ /"=._"=._ _ | *
* | ( \_.="_.=" '--------' "=._"=._/ ) | *
* | > _.=" "=._ < | *
* | (_/ \_) | *
* | | *
* '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=' *
* *
* Abandon All Hope *
*************************************************************/
EIEIO
}
# abandon all hope ye who enter here
skull
Create a CRC16 against string
- use caution in creating unique values < 65k, as there can be slop and overlap
- Good quick and dirty creation that is reproducible
STRING=foo
crc16() {
while read -r -d "" -n 1 ; do astring+=( "$REPLY" ) ; done <<< "$1"
cnt=${#1}
c=65535
for ((x=0;x<$cnt;x++)); do
char=$(printf '%d' \'"${1:$x:1}")
e=$(((c ^ char) & 0x00FF))
s=$(((e << 4) & 0x00FF))
f=$(((e ^ s) & 0x00FF))
r1=$(((c >> 8) ^ (f << 8) ^ (f << 3) ^ (f >> 4)))
c=$r1
done
c=$((c ^ 0xffff))
echo "$c"
}
crc16 ${STRING}
returns value 26676