Skip to content

Commit 9ed7468

Browse files
Add port option (#36)
1 parent 4aed654 commit 9ed7468

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

check_interfaces.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ int main(int argc, char *argv[]) {
143143
.err_tolerance = 50,
144144
.coll_tolerance = -1,
145145
.hostname = 0,
146+
.port = "161",
146147
.user = 0,
147148
.auth_proto = 0,
148149
.auth_pass = 0,
@@ -194,17 +195,29 @@ int main(int argc, char *argv[]) {
194195
gettimeofday(&tv, &tz);
195196
starttime = (long double)tv.tv_sec + (((long double)tv.tv_usec) / 1000000);
196197

198+
// +1 for the `:` between hostname and port
199+
size_t peername_max_len = strlen(config.hostname) + strlen(config.port) + 1;
200+
char *peername = calloc(1, peername_max_len+1);
201+
if (peername == NULL) {
202+
printf("Failed to allocate memory at %d in %s\n", __LINE__, __FUNCTION__);
203+
exit(3);
204+
}
205+
206+
strlcpy(peername, config.hostname, peername_max_len+1);
207+
strlcat(peername, ":", peername_max_len+1);
208+
strlcat(peername, config.port, peername_max_len+1);
209+
197210
#ifdef DEBUG
198211
benchmark_start("Start SNMP session");
199212
#endif
200213
if (config.user)
201214
/* use snmpv3 */
202215
ss = start_session_v3(&session, config.user, config.auth_proto,
203216
config.auth_pass, config.priv_proto,
204-
config.priv_pass, config.hostname,
217+
config.priv_pass, peername,
205218
config.global_timeout, config.session_retries);
206219
else
207-
ss = start_session(&session, config.community, config.hostname,
220+
ss = start_session(&session, config.community, peername,
208221
config.mode, config.global_timeout,
209222
config.session_retries);
210223
#ifdef DEBUG
@@ -1257,6 +1270,10 @@ bool fetch_interface_names(struct configuration_struct* config, char **oid_names
12571270
return true;
12581271
}
12591272

1273+
enum {
1274+
PORT_OPTION = CHAR_MAX + 1
1275+
};
1276+
12601277
void parse_and_check_commandline(int argc, char **argv,
12611278
struct configuration_struct *config) {
12621279
int opt;
@@ -1277,6 +1294,7 @@ void parse_and_check_commandline(int argc, char **argv,
12771294
{"errors", required_argument, NULL, 'e'},
12781295
{"out-errors", required_argument, NULL, 'f'},
12791296
{"hostname", required_argument, NULL, 'h'},
1297+
{"port", required_argument, NULL, PORT_OPTION},
12801298
{"auth-proto", required_argument, NULL, 'j'},
12811299
{"auth-phrase", required_argument, NULL, 'J'},
12821300
{"priv-proto", required_argument, NULL, 'k'},
@@ -1332,6 +1350,9 @@ void parse_and_check_commandline(int argc, char **argv,
13321350
case 'h':
13331351
config->hostname = optarg;
13341352
break;
1353+
case PORT_OPTION:
1354+
config->port = optarg;
1355+
break;
13351356
case 'j':
13361357
config->auth_proto = optarg;
13371358
break;
@@ -1510,6 +1531,7 @@ int usage(char *progname) {
15101531
printf(" --retries\t\thow often to retry before giving up\n");
15111532
printf(" --max-repetitions\t\tsee "
15121533
"<http://www.net-snmp.org/docs/man/snmpbulkwalk.html>\n");
1534+
printf(" --port\t\tPort (default 161)\n");
15131535
printf("\n");
15141536
return 3;
15151537
}

snmp_bulkget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ typedef struct configuration_struct {
9090
int err_tolerance;
9191
int coll_tolerance;
9292
char *hostname;
93+
char* port;
9394
char *user;
9495
char *auth_proto;
9596
char *auth_pass;

0 commit comments

Comments
 (0)