Bash-common-checks: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
Line 9: | Line 9: | ||
else | else | ||
echo "Is not integer" | echo "Is not integer" | ||
fi | |||
</pre> | |||
====TCP port check==== | |||
* leverage /dev | |||
* use timeout for sanity | |||
<pre> | |||
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 | fi | ||
</pre> | </pre> | ||
[[Category: Bash]] | [[Category: Bash]] |
Revision as of 12:25, 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
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