hello,

I added two new options to the ping command to allow the execution of
the command from a script.

    the counter parameter indicates the number of response to show. If
no argument then control the infinite loop

   the quiet parameter to hide messages on the screen setting.

It is thus possible to script the ping command.
example:
       ping -c 1 -q 10.1.1.1 && echo echo || Link UP DOWN link


example :
        ping 1.1.1.1 (request infinite loop)
        ping -c 2 1.1.1.1 (request two ping echo)
        ping -c 2 -q 1.1.1.1 (no echo on display)


Is it possible to add these changes to the original code ?

Attachment patch file for GIT.

Bye
Cedric Levasseur
From 250f9685334b10974fb56c1de34c93b90bb33a97 Mon Sep 17 00:00:00 2001
From: Cyr-ius <cyr-ius@Hygor>
Date: Sun, 19 Oct 2014 22:03:54 +0200
Subject: [PATCH 1/1] Fix PING Scripting

---
 src/core/pinger.c           | 24 ++++++++++++++++++++++--
 src/hci/commands/ping_cmd.c | 14 +++++++++++++-
 src/include/ipxe/pinger.h   |  3 ++-
 src/include/usr/pingmgmt.h  |  2 +-
 src/usr/pingmgmt.c          | 15 ++++++++++++---
 5 files changed, 50 insertions(+), 8 deletions(-)

diff --git a/src/core/pinger.c b/src/core/pinger.c
index c912a64..b14b180 100644
--- a/src/core/pinger.c
+++ b/src/core/pinger.c
@@ -68,6 +68,12 @@ struct pinger {
 	size_t len;
 	/** Current sequence number */
 	uint16_t sequence;
+	/** Counter ticks */
+	size_t counter;
+	/** Flag deliver */
+	int  *flagdeliver;
+	/** Quiet display */
+	int *silent;
 
 	/** Callback function
 	 *
@@ -159,6 +165,13 @@ static void pinger_expired ( struct retry_timer *timer, int over __unused ) {
 	struct io_buffer *iobuf;
 	int rc;
 
+	/* Counter ticks */
+	if (pinger->counter == pinger->sequence) {
+		pinger_close ( pinger,0 );
+		ref_put ( &pinger->refcnt );
+		return;
+	}
+
 	/* Increase sequence number */
 	pinger->sequence++;
 
@@ -218,6 +231,8 @@ static int pinger_deliver ( struct pinger *pinger, struct io_buffer *iobuf,
 		       pinger, sequence, pinger->sequence );
 		rc = -EPROTO_SEQ;
 	} else {
+		/* Flag success deliver */
+		*pinger->flagdeliver = 0;
 		rc = 0;
 	}
 
@@ -225,7 +240,8 @@ static int pinger_deliver ( struct pinger *pinger, struct io_buffer *iobuf,
 	free_iob ( iobuf );
 
 	/* Notify callback function */
-	pinger->callback ( meta->src, sequence, len, rc );
+	if (*pinger->silent == 0)
+		pinger->callback ( meta->src, sequence, len, rc );
 
 	return rc;
 }
@@ -260,7 +276,8 @@ static struct interface_descriptor pinger_job_desc =
  * @ret rc		Return status code
  */
 int create_pinger ( struct interface *job, const char *hostname,
-		    unsigned long timeout, size_t len,
+		    unsigned long timeout, size_t len, size_t counter,
+		    int *flag , int *quiet,
 		    void ( * callback ) ( struct sockaddr *src,
 					  unsigned int sequence, size_t len,
 					  int rc ) ) {
@@ -281,6 +298,9 @@ int create_pinger ( struct interface *job, const char *hostname,
 	timer_init ( &pinger->timer, pinger_expired, &pinger->refcnt );
 	pinger->timeout = timeout;
 	pinger->len = len;
+	pinger->counter = counter;
+	pinger->flagdeliver = flag;
+	pinger->silent = quiet;
 	pinger->callback = callback;
 
 	/* Open socket */
diff --git a/src/hci/commands/ping_cmd.c b/src/hci/commands/ping_cmd.c
index d514a2a..942eedd 100644
--- a/src/hci/commands/ping_cmd.c
+++ b/src/hci/commands/ping_cmd.c
@@ -42,12 +42,19 @@ FILE_LICENCE ( GPL2_OR_LATER );
 /** Default timeout */
 #define PING_DEFAULT_TIMEOUT TICKS_PER_SEC
 
+/** Default counter length */
+#define PING_DEFAULT_COUNTER -1
+
 /** "ping" options */
 struct ping_options {
 	/** Payload length */
 	unsigned int size;
 	/** Timeout (in ms) */
 	unsigned long timeout;
+	/** Counter */
+	unsigned int counter;
+	/** No echo on display (easy for scripting) */
+	int quiet;
 };
 
 /** "ping" option list */
@@ -56,6 +63,10 @@ static struct option_descriptor ping_opts[] = {
 		      struct ping_options, size, parse_integer ),
 	OPTION_DESC ( "timeout", 't', required_argument,
 		      struct ping_options, timeout, parse_timeout ),
+	OPTION_DESC ( "counter", 'c', required_argument,
+		      struct ping_options, counter, parse_integer ),
+	OPTION_DESC ( "quiet", 'q', no_argument,
+		      struct ping_options, quiet, parse_flag ),
 };
 
 /** "ping" command descriptor */
@@ -78,6 +89,7 @@ static int ping_exec ( int argc, char **argv ) {
 	memset ( &opts, 0, sizeof ( opts ) );
 	opts.size = PING_DEFAULT_SIZE;
 	opts.timeout = PING_DEFAULT_TIMEOUT;
+	opts.counter = PING_DEFAULT_COUNTER;
 
 	/* Parse options */
 	if ( ( rc = reparse_options ( argc, argv, &ping_cmd, &opts ) ) != 0 )
@@ -87,7 +99,7 @@ static int ping_exec ( int argc, char **argv ) {
 	hostname = argv[optind];
 
 	/* Ping */
-	if ( ( rc = ping ( hostname, opts.timeout, opts.size ) ) != 0 )
+	if ( ( rc = ping ( hostname, opts.timeout, opts.size, opts.counter, opts.quiet ) ) != 0 )
 		return rc;
 
 	return 0;
diff --git a/src/include/ipxe/pinger.h b/src/include/ipxe/pinger.h
index 2d84033..9882448 100644
--- a/src/include/ipxe/pinger.h
+++ b/src/include/ipxe/pinger.h
@@ -14,7 +14,8 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #include <ipxe/socket.h>
 
 extern int create_pinger ( struct interface *job, const char *hostname,
-			   unsigned long timeout, size_t len,
+			   unsigned long timeout, size_t len, size_t counter,
+			   int *flag, int *quiet,
 			   void ( * callback ) ( struct sockaddr *peer,
 						 unsigned int sequence,
 						 size_t len,
diff --git a/src/include/usr/pingmgmt.h b/src/include/usr/pingmgmt.h
index 45ad5d3..a97dd3d 100644
--- a/src/include/usr/pingmgmt.h
+++ b/src/include/usr/pingmgmt.h
@@ -11,6 +11,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
 
 #include <stdint.h>
 
-extern int ping ( const char *hostname, unsigned long timeout, size_t len );
+extern int ping ( const char *hostname, unsigned long timeout, size_t len, size_t counter, int quiet);
 
 #endif /* _USR_PINGMGMT_H */
diff --git a/src/usr/pingmgmt.c b/src/usr/pingmgmt.c
index 2d4db49..836e842 100644
--- a/src/usr/pingmgmt.c
+++ b/src/usr/pingmgmt.c
@@ -58,23 +58,32 @@ static void ping_callback ( struct sockaddr *peer, unsigned int sequence,
  * @v hostname		Hostname
  * @v timeout		Timeout between pings, in ticks
  * @v len		Payload length
+ * @v counter		Counter ticks (Author : Cedric Levasseur)
+ * @v quiet		Display Echo on screen 	(Author : Cedric Levasseur)
  * @ret rc		Return status code
  */
-int ping ( const char *hostname, unsigned long timeout, size_t len ) {
+int ping ( const char *hostname, unsigned long timeout, size_t len, size_t counter, int quiet ) {
+	int flag = 1;
 	int rc;
 
 	/* Create pinger */
 	if ( ( rc = create_pinger ( &monojob, hostname, timeout,
-				    len, ping_callback ) ) != 0 ) {
+				    len, counter, &flag, &quiet, ping_callback ) ) != 0 ) {
 		printf ( "Could not start ping: %s\n", strerror ( rc ) );
 		return rc;
 	}
 
 	/* Wait for ping to complete */
 	if ( ( rc = monojob_wait ( NULL, 0 ) ) != 0 ) {
-		printf ( "Finished: %s\n", strerror ( rc ) );
+		if (quiet==0)
+			printf ( "Finished: %s\n", strerror ( rc ) );
 		return rc;
 	}
+	if ( flag != 0)  {
+		if (quiet==0)
+			printf ( "Not Response: %s\n", strerror ( flag ) );
+		return flag;
+	}
 
 	return 0;
 }
-- 
2.1.0

_______________________________________________
ipxe-devel mailing list
[email protected]
https://lists.ipxe.org/mailman/listinfo.cgi/ipxe-devel

Reply via email to