/** * \file udptabq.c * * Obtains data from 'udp' table in MIB II. * * Makes query to MIB using SNMP protocol and types obtained * data on standard output. * * Run: > ./udptabq host * host - name of SNMP agent, for example ds02 * obtain the same data as: > snmptable host public udp.udpTable * * \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[]= "7.5.1.1"; 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 pro vypis 'udp' tabulky z MIB II ****\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 IP address: */ if (gethostbyaddr(argv[1], 4, AF_INET) != NULL) strcpy(agent_addr, argv[1]); else { h_ent = gethostbyname(argv[1]); if (h_ent == NULL) { error_msg(errno, "Incorrect agent 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(agent_addr, h_ip); } printf("SNMP Manager - parameters of connection\n"); printf(" SNMP manager (client): %s %s\n", my_hname, my_addr); printf(" SNMP agent (server): %s %s\n", argv[1], 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("\nSNMP 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 < 3) { sprintf(mibTabID,"7.5.1.%d", i); res = mib_query(argv[1], mibTabID); i++; } if (!snmp_close(p_session)) { error_msg(errno, "Closing of SNMP session failed."); return(-1); } printf("SNMP table: udp.udpTable\n\n"); print_table(val_count); for (i = 0; i < val_count; i++) free((char *) tab_values[i]); return(0); }