Skip to content

Commit 2dc7e11

Browse files
committed
Added code to print the target system with each down message.
This allows for multiple cpings to run in the same window in the background and be able to tell which host missed pings.
1 parent c5ba14d commit 2dc7e11

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cping
33

44
A cooler version of ping that has a -F threshold in milliseconds option. With -F specified, only
55
packets that miss the threshold are printed. Also, if the target host goes down, the duration of the downtime
6-
is printed in summary.
6+
is printed in summary. Also mutiple cpings can be run in the background (ie: ```cping -F 10 host &```) and the output will show which host missed a ping threshold.
77

88
```
99
$ cping -F 30 google.com

cping.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ int maxpacket = sizeof(outpack);
9494

9595
static int broadcast_pings = 0;
9696

97-
static char *pr_addr(__u32);
97+
char *pr_addr(__u32);
9898
static void pr_options(unsigned char * cp, int hlen);
9999
static void pr_iph(struct iphdr *ip);
100100
static void usage(void) __attribute__((noreturn));

cping_common.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ int ident; /* process id to identify our packets */
6060

6161
static int screen_width = INT_MAX;
6262

63-
static int checktimeout(int ours)
63+
char *pr_addr(__u32);
64+
65+
extern struct sockaddr_in whereto; /* who to ping */
66+
67+
static int checktimeout(int ours, char *from)
6468
{
6569
// DRH--- NOT THREAD SAFE!
6670
static int init=0;
@@ -87,14 +91,14 @@ static int checktimeout(int ours)
8791
stime+=4;
8892

8993
if (ours==1) {
90-
if (firstline==0) printf("%20.20s System responed to a ping. Down for %f secs \n", stime, ret/1000000.0);
94+
if (firstline==0) printf("%20.20s System (%s) responed to a ping. Down for %f secs \n", stime, from, ret/1000000.0);
9195
tmp=tvold;
9296
tvold=tvnew;
9397
tvnew=tmp;
9498
firstline=1;
9599
} else {
96100
if ((tvnew->tv_sec - tvold->tv_sec)>2) {
97-
printf("%20.20s Missing pings. Down for %f secs \r", stime, ret/1000000.0);
101+
printf("%20.20s (%s) Missing pings. Down for %f secs \r", stime, from, ret/1000000.0);
98102
fflush(stdout);
99103
if (firstline) {
100104
printf("\n");
@@ -651,7 +655,7 @@ void main_loop(int icmp_sock, __u8 *packet, int packlen)
651655

652656
if (cc < 0) {
653657
if (errno == EAGAIN || errno == EINTR) {
654-
checktimeout(0);
658+
checktimeout(0, pr_addr(whereto.sin_addr.s_addr));
655659
break;
656660
}
657661
if (!receive_error_msg()) {
@@ -683,15 +687,15 @@ void main_loop(int icmp_sock, __u8 *packet, int packlen)
683687

684688
not_ours = parse_reply(&msg, cc, addrbuf, recv_timep);
685689
if (not_ours) {
686-
checktimeout(0);
690+
checktimeout(0, pr_addr(whereto.sin_addr.s_addr));
687691
} else {
688-
checktimeout(1);
692+
checktimeout(1, pr_addr(whereto.sin_addr.s_addr));
689693
}
690694
}
691695

692696
/* See? ... someone runs another ping on this host. */
693697
if (not_ours) {
694-
checktimeout(0);
698+
checktimeout(0, pr_addr(whereto.sin_addr.s_addr));
695699
install_filter();
696700
}
697701

0 commit comments

Comments
 (0)