Hatena::ブログ(Diary)

cleverwareの日記 このページをアンテナに追加 RSSフィード

2009-07-02

echoping で submission ポートを監視できるようにする

echoping には SMTP プロトコルでの応答があるか確認する便利な機能があるのだが、ポートを指定することが出来ない。

$ echoping -v -S xxx.xxx.xxx.xxx

This is echoping, version 6.0.2.

Trying to connect to internet address xxx.xxx.xxx.xxx 25 to transmit 6 bytes...
Trying to send 256 bytes to internet address xxx.xxx.xxx.xxx...
Connected...
TCP Latency: 0.000243 seconds
Sent (6 bytes)...
Application Latency: 0.011435 seconds
26 bytes read from server.
Estimated TCP RTT: 0.0024 seconds (std. deviation 0.003)
Elapsed time: 3.438002 seconds
$ echoping -v -S xxx.xxx.xxx.xxx:587

This is echoping, version 6.0.2.

echoping: The syntax hostname:port is only for HTTP or ICP
$

現実のシステム管理だと 25番以外のポートで運用することも多く不便なので、ちょっとした改造を行った。

--- echoping-6.0.2/echoping.c.~1~       2007-04-05 21:40:49.000000000 +0900
+++ echoping-6.0.2/echoping.c   2009-07-02 14:07:10.000000000 +0900
@@ -560,7 +560,7 @@
                err_quit("Cannot convert %s to UTF-8 encoding: wrong locale (%s)?",
                         server, stringprep_locale_charset());
 #endif
-       if (!http && !icp) {
+       if (!http && !icp && !smtp) {
                for (p = server; *p && (*p != ':'); p++) {
                }
                if (*p && (*p == ':')) {
@@ -636,6 +636,44 @@
                }
        }
 #endif
+#ifdef SMTP
+       if (smtp) {
+               char           *text_port = NULL;
+
+               if (*server == '[') {   /* RFC 2732 */
+                       server++;
+                       for (p = server; *p && *p != ']'; p++) {
+                       }
+                       p++;
+                       if (*p == ':') {
+                               p--;
+                               *p = 0;
+                               text_port = p + 2;
+                               strncpy(port_name, text_port, NI_MAXSERV);
+                       } else {
+                               p--;
+                               if (*p == ']') {
+                                       *p = 0;
+                               } else {
+                                       (void) fprintf(stderr,
+                                                      "%s: Invalid syntax for IPv6 address.\n",
+                                                      progname);
+                                       exit(1);
+                               }
+                       }
+               } else {
+                       for (p = server; *p; p++) {
+                               if (*p == ':') {
+                                       *p = 0;
+                                       text_port = p + 1;
+                                       if (strcmp(text_port, ""))
+                                               strncpy(port_name, text_port,
+                                                       NI_MAXSERV);
+                               }
+                       }
+               }
+       }
+#endif

 #ifdef LIBIDN
        /*

こんな感じで使えます

% echoping -v -S xxx.xxx.xxx.xxx:587

This is echoping, version 6.0.2.

Trying to connect to internet address xxx.xxx.xxx.xxx 587 to transmit 6 bytes...
Connected...
TCP Latency: 0.022983 seconds
Sent (6 bytes)...
Application Latency: 0.023221 seconds
15 bytes read from server.
Estimated TCP RTT: 0.0230 seconds (std. deviation 0.017)
Elapsed time: 0.070736 seconds
%