📄 ipinfo.cpp
字号:
Uint32 c = 0; String httpVersion = String (); String httpMethod = String (); String timeoutStr = String (); String GetOptString = String (); getoopt getOpts; // // Construct GetOptString //#ifndef DISABLE_SUPPORT_FOR_REMOTE_CONNECTIONS GetOptString.append (_OPTION_HOSTNAME); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR); GetOptString.append (_OPTION_PORTNUMBER); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR); GetOptString.append (_OPTION_SSL); GetOptString.append (_OPTION_TIMEOUT); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR); GetOptString.append (_OPTION_USERNAME); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR); GetOptString.append (_OPTION_PASSWORD); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR);#endif GetOptString.append (_OPTION_DEBUG); // // Initialize and parse getOpts // getOpts = getoopt (); getOpts.addFlagspec (GetOptString); getOpts.parse (argc, argv); if (getOpts.hasErrors ()) { CommandFormatException e (getOpts.getErrorStrings () [0]); throw e; } // // Get options and arguments from the command line // for (i = getOpts.first (); i < getOpts.last (); i++) { if (getOpts [i].getType () == Optarg::LONGFLAG) { UnexpectedArgumentException e ( getOpts [i].Value ()); throw e; } else if (getOpts [i].getType () == Optarg::REGULAR) { UnexpectedArgumentException e ( getOpts [i].Value ()); throw e; } else /* getOpts [i].getType () == FLAG */ { c = getOpts [i].getopt () [0]; switch (c) { case _OPTION_HOSTNAME: { if (getOpts.isSet (_OPTION_HOSTNAME) > 1) { // // More than one hostname option was found // DuplicateOptionException e (_OPTION_HOSTNAME); throw e; } _hostName = getOpts [i].Value (); _hostNameSet = true; break; } case _OPTION_PORTNUMBER: { if (getOpts.isSet (_OPTION_PORTNUMBER) > 1) { // // More than one portNumber option was found // DuplicateOptionException e (_OPTION_PORTNUMBER); throw e; } _portNumberStr = getOpts [i].Value (); try { getOpts [i].Value (_portNumber); } catch (const TypeMismatchException&) { InvalidOptionArgumentException e (_portNumberStr, _OPTION_PORTNUMBER); throw e; } _portNumberSet = true; break; } case _OPTION_SSL: { // // Use port 5989 as the default port for SSL // _useSSL = true; if (!_portNumberSet) _portNumber = 5989; break; } case _OPTION_DEBUG: { _enableDebug = true; break; } case _OPTION_TIMEOUT: { if (getOpts.isSet (_OPTION_TIMEOUT) > 1) { // // More than one timeout option was found // DuplicateOptionException e (_OPTION_TIMEOUT); throw e; } timeoutStr = getOpts [i].Value (); try { getOpts [i].Value (_timeout); } catch (const TypeMismatchException&) { InvalidOptionArgumentException e (timeoutStr, _OPTION_TIMEOUT); throw e; } break; } case _OPTION_USERNAME: { if (getOpts.isSet (_OPTION_USERNAME) > 1) { // // More than one username option was found // DuplicateOptionException e (_OPTION_USERNAME); throw e; } _userName = getOpts [i].Value (); _userNameSet = true; break; } case _OPTION_PASSWORD: { if (getOpts.isSet (_OPTION_PASSWORD) > 1) { // // More than one password option was found // DuplicateOptionException e (_OPTION_PASSWORD); throw e; } _password = getOpts [i].Value (); _passwordSet = true; break; } default: // // This path should not be hit // break; } } } if (getOpts.isSet (_OPTION_PORTNUMBER) < 1) { // // No portNumber specified // Default to WBEM_DEFAULT_PORT // Already done in constructor // } else { if (_portNumber > _MAX_PORTNUMBER) { // // Portnumber out of valid range // InvalidOptionArgumentException e (_portNumberStr, _OPTION_PORTNUMBER); throw e; } } if (getOpts.isSet (_OPTION_TIMEOUT) < 1) { // // No timeout specified // Default to DEFAULT_TIMEOUT_MILLISECONDS // Already done in constructor // } else { if (_timeout <= 0) { // // Timeout out of valid range // InvalidOptionArgumentException e (timeoutStr, _OPTION_TIMEOUT); throw e; } }}/** ErrorExit - Print out the error message and exits. @param errPrintWriter The ostream to which error output should be written @param message Text for error message @return None, Terminates the program*/void IPInfoCommand::errorExit( ostream& errPrintWriter, const String& message){ errPrintWriter << "ipinfo error: " << message << endl; exit(1);}void IPInfoCommand::getIPInfo(ostream& outPrintWriter, ostream& errPrintWriter){ CIMClient client; client.setTimeout( _timeout ); try { _connectToServer( client, outPrintWriter); IPRouteInfo ipr (client, _enableDebug, outPrintWriter, errPrintWriter); IPPEpInfo ippep(client, _enableDebug, outPrintWriter, errPrintWriter); BIPTLEpInfo ipbiptle(client, _enableDebug, outPrintWriter, errPrintWriter); } // end try catch(const Exception& e) { errorExit(errPrintWriter, e.getMessage()); }}/** Executes the command and writes the results to the PrintWriters. @param outPrintWriter the ostream to which output should be written @param errPrintWriter the ostream to which error output should be written @return 0 if the command is successful 1 if an error occurs in executing the command */Uint32 IPInfoCommand::execute (ostream& outPrintWriter, ostream& errPrintWriter) { try { IPInfoCommand::getIPInfo( outPrintWriter, errPrintWriter ); } catch (const IPInfoException& e) { errPrintWriter << IPInfoCommand::COMMAND_NAME << ": " << e.getMessage () << endl; return (RC_ERROR); } return (RC_SUCCESS);}/** Parses the command line, and executes the command. @param argc the number of command line arguments @param argv the string vector of command line arguments @return 0 if the command is successful 1 if an error occurs in executing the command */PEGASUS_NAMESPACE_END// exclude main from the Pegasus NamespacePEGASUS_USING_PEGASUS;PEGASUS_USING_STD;int main (int argc, char* argv []) { IPInfoCommand command = IPInfoCommand (); int rc; try { command.setCommand (argc, argv); } catch (const CommandFormatException& cfe) { cerr << IPInfoCommand::COMMAND_NAME << ": " << cfe.getMessage () << endl; cerr << command.getUsage () << endl; exit (Command::RC_ERROR); } rc = command.execute (cout, cerr); exit (rc); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -