{"id":1437,"date":"2024-05-21T22:21:47","date_gmt":"2024-05-21T19:21:47","guid":{"rendered":"https:\/\/techltx.com\/de\/?p=1437"},"modified":"2024-05-21T22:26:56","modified_gmt":"2024-05-21T19:26:56","slug":"bash-script-netcat-connection-sap-s-4hana-system","status":"publish","type":"post","link":"https:\/\/techltx.com\/en\/sap\/bash-script-netcat-connection-sap-s-4hana-system\/2024\/05\/","title":{"rendered":"bash script for the NETCAT connection test to a SAP S\/4HANA system"},"content":{"rendered":"\n<p>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:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Network monitoring: <\/strong>The script can be used to monitor the availability of a specific IP address and ports in a network.<\/li>\n\n\n\n<li><strong>Periodic testing of connections:<\/strong> 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.<\/li>\n\n\n\n<li><strong>Logging of connection events:<\/strong> 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.<\/li>\n\n\n\n<li><strong>Notification of connection errors:<\/strong> 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.<\/li>\n\n\n\n<li><strong>Deletion of old log entries<\/strong>: The script can also clean up older log entries to keep the log file clean and save unnecessary disk space.<\/li>\n<\/ol>\n\n\n\n<p><strong>Netcat<\/strong>, also known as <strong>nc<\/strong>, 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HowTo test netcat connection to SAP System with IP and Port<\/h2>\n\n\n\n<p>First step is to create a file <filename.sh> and store this file in \/usr\/local\/bin<\/filename.sh><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# Funktion zur \u00dcberpr\u00fcfung der Verbindung und Versenden von Benachrichtigungen\ncheck_connection() {\n    timestamp=$(date +\"%Y-%m-%d %H:%M:%S\")\n    if nc -z \"$1\" \"$2\" >\/dev\/null 2>&1; then\n        echo \"$timestamp Verbindung erfolgreich\" >> \/tmp\/connectiontest.txt\n    else\n        echo \"$timestamp Verbindung fehlgeschlagen\" >> \/tmp\/connectiontest-failed.txt\n        # Benachrichtigung per E-Mail senden\n        echo \"Verbindungsfehler zu $1:$2\" | mail -s \"Verbindungsfehler\" your_email@example.com\n    fi\n}\n\n# Funktion zum L\u00f6schen von alten Timestamps in der Datei\ndelete_old_timestamps() {\n    # Finden und L\u00f6schen von Timestamps, die \u00e4lter als 10 Tage sind\n    find \/tmp\/connectiontest.txt -type f -mtime +10 -exec sed -i '\/^&#91;0-9]\\{4\\}-&#91;0-9]\\{2\\}-&#91;0-9]\\{2\\}\/d' {} \\;\n    find \/tmp\/connectiontest-failed.txt -type f -mtime +10 -exec sed -i '\/^&#91;0-9]\\{4\\}-&#91;0-9]\\{2\\}-&#91;0-9]\\{2\\}\/d' {} \\;\n}\n\n# L\u00f6schen \u00e4lterer Timestamps\ndelete_old_timestamps\n\n# Aufruf der Verbindungsfunktion\ncheck_connection \"8.8.8.8\" 3200\n\n# Alle Netcat-Prozesse beenden\n# pkill -u $USER nc\n\n# Prozess beenden\nexit 0<\/code><\/pre>\n\n\n\n<p>Change the linux file system permissions (more information <a href=\"https:\/\/www.redhat.com\/sysadmin\/linux-file-permissions-explained\">https:\/\/www.redhat.com\/sysadmin\/linux-file-permissions-explained<\/a>)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo chmod 755<\/code><\/pre>\n\n\n\n<p>Run the script via cron job &#8211; use a proper user because of killing all nc connections of this user (by default killing it is disabled).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -e<\/code><\/pre>\n\n\n\n<p>Run the script every minute<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>* * * * *  \/usr\/local\/bin\/.sh<\/code><\/pre>\n\n\n\n<p>Check the results in folder \/tmp<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The script is useful for automated monitoring of SAP system connectivity and logging connection events, making it ideal for system administrators to ensure the availability and integrity of their SAP systems. The Bash script checks the connection to a MessageServer of a SAP system instance. Here is a summary of its functionalities:<\/p>\n<p>Connection Test: The script tests the nc connection to a MessageServer of a SAP system instance. It utilizes the check_connection function to perform the connection check.<\/p>\n<p>Logging: The script logs the test results in two files: connectiontest.txt contains all test results, while connectiontest-failed.txt stores only failed connection attempts.<\/p>\n<p>Notification on Connection Failures: In case of a connection failure, the script sends an email notification to the specified email address.<\/p>\n<p>Cleanup of Old Entries: The script deletes old timestamps from the connectiontest.txt log file after 10 days to maintain file cleanliness and save storage space.<\/p>\n<p>Endless Loop for Periodic Testing: The script includes a commented-out endless loop that performs connection tests at regular intervals. This feature can be enabled to continuously monitor the connection. It is recommended to set up a cronjob for this purpose.<\/p>\n<p>Termination of nc Sessions: Upon completion of the script, it terminates all running nc sessions belonging to the current user.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[5],"tags":[92,111,110,109,93,7],"class_list":["post-1437","post","type-post","status-publish","format-standard","hentry","category-sap","tag-bash","tag-connection-test","tag-message-server","tag-netcat","tag-script","tag-sap"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"BALTX","author_link":"https:\/\/techltx.com\/en\/author\/baltx-com\/"},"uagb_comment_info":0,"uagb_excerpt":"The script is useful for automated monitoring of SAP system connectivity and logging connection events, making it ideal for system administrators to ensure the availability and integrity of their SAP systems. The Bash script checks the connection to a MessageServer of a SAP system instance. Here is a summary of its functionalities: Connection Test: The&hellip;","_links":{"self":[{"href":"https:\/\/techltx.com\/en\/wp-json\/wp\/v2\/posts\/1437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techltx.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techltx.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techltx.com\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/techltx.com\/en\/wp-json\/wp\/v2\/comments?post=1437"}],"version-history":[{"count":6,"href":"https:\/\/techltx.com\/en\/wp-json\/wp\/v2\/posts\/1437\/revisions"}],"predecessor-version":[{"id":1447,"href":"https:\/\/techltx.com\/en\/wp-json\/wp\/v2\/posts\/1437\/revisions\/1447"}],"wp:attachment":[{"href":"https:\/\/techltx.com\/en\/wp-json\/wp\/v2\/media?parent=1437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techltx.com\/en\/wp-json\/wp\/v2\/categories?post=1437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techltx.com\/en\/wp-json\/wp\/v2\/tags?post=1437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}