summaryrefslogtreecommitdiffstats
path: root/web/attachments/37563-nagiosplug-check_ssh-20021213.diff
blob: 091b5bf01270c4419549504be6bc62e2a0f442c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
Index: plugins/check_ssh.c
===================================================================
RCS file: /cvsroot/nagiosplug/nagiosplug/plugins/check_ssh.c,v
retrieving revision 1.3
diff -u -r1.3 check_ssh.c
--- plugins/check_ssh.c	30 Oct 2002 18:44:43 -0000	1.3
+++ plugins/check_ssh.c	14 Dec 2002 04:31:32 -0000
@@ -34,7 +34,6 @@
 void print_help (void);
 void print_usage (void);
 
-char *ssh_resolve (char *hostname);
 int ssh_connect (char *haddr, short hport);
 
 int
@@ -49,7 +48,7 @@
 	alarm (socket_timeout);
 
 	/* ssh_connect exits if error is found */
-	ssh_connect (ssh_resolve (server_name), port);
+	ssh_connect (server_name, port);
 
 	alarm (0);
 
@@ -109,6 +108,8 @@
 			socket_timeout = atoi (optarg);
 			break;
 		case 'H':									/* host */
+			if (is_host (optarg) == FALSE)
+				usage ("Invalid host name/addresss\n");
 			server_name = optarg;
 			break;
 		case 'p':									/* port */
@@ -125,7 +126,8 @@
 
 	c = optind;
 	if (server_name == NULL && argv[c]) {
-		server_name = argv[c++];
+		if (is_host (argv[c++]))
+			server_name = argv[c++];
 	}
 	else if (port == -1 && argv[c]) {
 		if (is_intpos (argv[c])) {
@@ -153,26 +155,6 @@
 
 /************************************************************************
 *
-* Resolve hostname into IP address
-*
-*-----------------------------------------------------------------------*/
-
-char *
-ssh_resolve (char *hostname)
-{
-	struct hostent *host;
-
-	host = gethostbyname (hostname);
-	if (!host) {
-		herror (hostname);
-		exit (STATE_CRITICAL);
-	}
-	return (host->h_addr);
-}
-
-
-/************************************************************************
-*
 * Try to connect to SSH server at specified server and port
 *
 *-----------------------------------------------------------------------*/
@@ -180,39 +162,24 @@
 int
 ssh_connect (char *haddr, short hport)
 {
-	int s;
-	struct sockaddr_in addr;
-	int addrlen;
-	int len;
+	int result;
+	int sock;
 	char *output = NULL;
 	char *buffer = NULL;
 	char *ssh_proto = NULL;
 	char *ssh_server = NULL;
 	char revision[20];
 
-	sscanf ("$Revision: 1.3 $", "$Revision: %[0123456789.]", revision);
+	sscanf ("$Revision: 1.3 $", "$Revision: %[0-9.]", revision);
 
-	addrlen = sizeof (addr);
-	memset (&addr, 0, addrlen);
-	addr.sin_port = htons (hport);
-	addr.sin_family = AF_INET;
-	bcopy (haddr, (void *) &addr.sin_addr.s_addr, 4);
-
-	s = socket (AF_INET, SOCK_STREAM, 0);
-	if (!s) {
-		printf ("socket(): %s for %s:%d\n", strerror (errno), server_name, hport);
-		exit (STATE_CRITICAL);
-	}
+	result = my_tcp_connect (haddr, hport, &sock);
 
-	if (connect (s, (struct sockaddr *) &addr, addrlen)) {
-		printf ("connect(): %s for %s:%d\n", strerror (errno), server_name,
-						hport);
-		exit (STATE_CRITICAL);
-	}
+	if (result != STATE_OK)
+		return result;
 
 	output = (char *) malloc (BUFF_SZ + 1);
 	memset (output, 0, BUFF_SZ + 1);
-	recv (s, output, BUFF_SZ, 0);
+	recv (sock, output, BUFF_SZ, 0);
 	if (strncmp (output, "SSH", 3)) {
 		printf ("Server answer: %s", output);
 		exit (STATE_CRITICAL);
@@ -228,7 +195,7 @@
 			("SSH ok - %s (protocol %s)\n",
 			 ssh_server, ssh_proto);
 		asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, revision);
-		send (s, buffer, strlen (buffer), MSG_DONTWAIT);
+		send (sock, buffer, strlen (buffer), MSG_DONTWAIT);
 		if (verbose)
 			printf ("%s\n", buffer);
 		exit (STATE_OK);