The bash script is well suited for various use cases, especially if you need to regularly check the availability of a network connection to an SAP system. This can be helpful if you use different connections via SD-WAN and notice in the logs of your SAP system that there are connection interruptions. Here are some use cases for which the bash script is suitable:
- Network monitoring: The script can be used to monitor the availability of a specific IP address and ports in a network.
- Periodic testing of connections: By using a cron job, the script can be run periodically to ensure that the connection is checked at a specific time or at regular intervals.
- Logging of connection events: The script logs all connection events to a text file, including timestamps and whether the connection succeeded or failed. These logs can be useful for error analysis and diagnostics.
- Notification of connection errors: In the event of a connection error, the script sends an e-mail notification to the administrator so that they are informed of problems and can react accordingly.
- Deletion of old log entries: The script can also clean up older log entries to keep the log file clean and save unnecessary disk space.
Netcat, also known as nc, is a command line tool used for network interaction. It can serve as a simple TCP/IP client or server and allows data to be sent and received over network connections. Netcat can be used for a variety of tasks, including port scans, file transfers, network communication tests and even as a backdoor for remote access. Therefore, nc is a very popular tool among network colleagues, which we are now using to test the connection with an SAP S/4HANA system.
HowTo test netcat connection to SAP System with IP and Port
First step is to create a file
#!/bin/bash
# Funktion zur Überprüfung der Verbindung und Versenden von Benachrichtigungen
check_connection() {
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
if nc -z "$1" "$2" >/dev/null 2>&1; then
echo "$timestamp Verbindung erfolgreich" >> /tmp/connectiontest.txt
else
echo "$timestamp Verbindung fehlgeschlagen" >> /tmp/connectiontest-failed.txt
# Benachrichtigung per E-Mail senden
echo "Verbindungsfehler zu $1:$2" | mail -s "Verbindungsfehler" your_email@example.com
fi
}
# Funktion zum Löschen von alten Timestamps in der Datei
delete_old_timestamps() {
# Finden und Löschen von Timestamps, die älter als 10 Tage sind
find /tmp/connectiontest.txt -type f -mtime +10 -exec sed -i '/^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}/d' {} \;
find /tmp/connectiontest-failed.txt -type f -mtime +10 -exec sed -i '/^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}/d' {} \;
}
# Löschen älterer Timestamps
delete_old_timestamps
# Aufruf der Verbindungsfunktion
check_connection "8.8.8.8" 3200
# Alle Netcat-Prozesse beenden
# pkill -u $USER nc
# Prozess beenden
exit 0
Change the linux file system permissions (more information https://www.redhat.com/sysadmin/linux-file-permissions-explained)
sudo chmod 755
Run the script via cron job – use a proper user because of killing all nc connections of this user (by default killing it is disabled).
crontab -e
Run the script every minute
* * * * * /usr/local/bin/.sh
Check the results in folder /tmp
