/** * \file udptabq.c * * Extracts data in 'at' table from MIB II. * * Makes query to MIB using SNMP protocol and types obtained * data on standard output. * * Run: > ./udptabq host * where 'host' is name of SNMP agent (for example ds02) * * Extracts and prints the same data as: > snmptable host public at.atTable * * \author Tomas Jirak, Vladimir Sochor * \date 2003 */ #include "snmp.h" #include "snmp_api.h" #include "mib.h" #include "mibquery.h" #include #include #include #include #include #include #include #include #include #include #include int val_count; char *tab_values[2*MAX_ROW_COUNT]; char my_addr[16], agent_addr[16]; struct snmp_session session; struct snmp_session *p_session; int main(int argc, char *argv[]); /** * Main. */ int main(int argc, char *argv[]) { char mibTabID[]= "3.1.1.1"; // at table ID int i, res; char my_hname[20]; struct hostent *h_ent; unsigned long int *h_address; struct in_addr h_ip_addr; char *h_ip; printf("**** Program for extracting data from 'at' table in MIB II ****\n"); printf("**** authors: Tomas Jirak & Vladimir Sochor, 2003 ****\n\n"); if (argc < 2) { printf("Syntaxe: %s host\n", argv[0]); printf(" One argument required - input host name or host IP address.\n"); return(-1); } /* Get my host name */ res = gethostname(my_hname, 20); if (res < 0) { error_msg(errno, "Cannot get my hostname"); return(-1); } /* Get my IP address */ h_ent = gethostbyname(my_hname); if (h_ent == NULL) { error_msg(errno, "Cannot get my IP address"); return(-1); } h_address = (unsigned long int *) (h_ent->h_addr); h_ip_addr.s_addr = *h_address; h_ip = inet_ntoa(h_ip_addr); strcpy(my_addr, h_ip); /* Get SNMP agent by his IP address: * if (gethostbyaddr(argv[1], 4, AF_INET) != NULL) * strcpy(agent_addr, argv[1]); */ /* Get SNMP agent by his name */ h_ent = gethostbyname(argv[1]); if (h_ent == NULL) { error_msg(errno, "Incorrect SNMP agent name."); return(-1); } h_address = (unsigned long int *) (h_ent->h_addr); h_ip_addr.s_addr = *h_address; h_ip = inet_ntoa(h_ip_addr); strcpy(agent_addr, h_ip); printf("SNMP Manager - input parameters for connection\n"); printf(" SNMP manager (client): %s %s\n", my_hname, my_addr); printf(" SNMP agent (server): %s %s\n\n", h_ent->h_name, agent_addr); initmib(""); bzero((char *) &session, sizeof(struct snmp_session)); session.peername = h_ent->h_name; session.community = (u_char *) "public"; session.community_len = strlen((char *) "public"); session.retries = SNMP_DEFAULT_RETRIES; session.timeout = SNMP_DEFAULT_TIMEOUT; session.authenticator = NULL; printf("SNMP Manager: Opening session ... \n"); snmp_synch_setup(&session); p_session = snmp_open(&session); if (p_session == NULL) { error_msg(errno, "Opening of SNMP session failed"); return(-1); } res = 1; i = 1; val_count = 0; /* obtain requested data from MIB */ while (res && i < 4) { sprintf(mibTabID,"3.1.1.%d", i); res = mib_query(argv[1], mibTabID); i++; } printf("SNMP Manager: Closing session ... \n\n"); if (!snmp_close(p_session)) { error_msg(errno, "Closing of SNMP session failed."); return(-1); } printf("SNMP table: at.atTable\n\n"); print_table(val_count); for (i = 0; i < val_count; i++) free((char *) tab_values[i]); printf("Bye ...\n\n"); return(0); }