⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cimop.cpp

📁 Pegasus is an open-source implementationof the DMTF CIM and WBEM standards. It is designed to be por
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================//// Author: Mike Glantz, Hewlett-Packard Company (michael_glantz@hp.com)//// Modified By: David Dillard, Symantec Corp. (david_dillard@symantec.com)////%/////////////////////////////////////////////////////////////////////////////#include <Pegasus/Common/Constants.h>#include "Cimop.h"#include <iostream>#if !defined(PEGASUS_OS_TYPE_WINDOWS)#include <unistd.h>#endif#include <stdlib.h>static const int TIMEOUT = 60000;static const String DEFAULT_NAMESPACE("root/cimv2");static CIMClient _c;static String _hostName;static Uint32 _portNumber = 0;static String _nameSpace;static String _userName;static String _passWord;// ===============================================================// main// ===============================================================  // one day:  // Options:  //   -h hostname (default: localhost)  //   -p port (default: 5988)  //   -u username  //   -w password (required if -u present, else prompts)int main(const int argc, const char **argv){  // error if no args  if (argv[1] == 0)  {    _usage();    return 1;  }  SSLContext *sslContext = 0; // initialized for unencrypted connection  _c.setTimeout(TIMEOUT);  // Get hostname from environment, if defined  char *p = getenv("CIM_HOST");  if (p) _hostName = p;  // Get port number, if defined  char *pn = getenv("CIM_PORT");  if (pn) _portNumber = atol(pn);  if (p)  {    // hostname was specified, we will not connect local    // so decide whether encrypted or not    char *s = getenv("CIM_NOSSL");    if (s) // don't use ssl (not encrypted)    {      if (!pn) _portNumber = 5988; // use 5988 if no port specified    }    else    {      try      {        sslContext = new SSLContext(PEGASUS_SSLCLIENT_CERTIFICATEFILE,                                    verifyServerCertificate,                                    PEGASUS_SSLCLIENT_RANDOMFILE                                    /* "/var/opt/wbem/ssl.rnd" */);      }      catch (Exception &e)      {        cerr << e.getMessage() << endl;        return 1;      }      if (!pn) _portNumber = 5989; // use 5989 if no port specified    }  }  // Get namespace from environment or use default  p = getenv("CIM_NAMESPACE");  _nameSpace = (p==0)? DEFAULT_NAMESPACE:p;  // Get user from environment or don't specify  p = getenv("CIM_USER");  _userName = (p==0)? String::EMPTY : p;  // Get password from environment or use empty  p = getenv("CIM_PASSWORD");  _passWord = (p==0)? String::EMPTY : p;  try  {    if (String::equal(_hostName,String::EMPTY)) _c.connectLocal();    else    // hostname was specified; do remote connect    {      if (sslContext) _c.connect(_hostName, _portNumber, *sslContext, _userName, _passWord);      else _c.connect(_hostName, _portNumber, _userName, _passWord);    }  }  catch(Exception& e)  {    cerr << e.getMessage() << endl;    return 1;  }  // command is first arg  const char *cmd = argv[1];  if (String::equalNoCase(cmd,"getClass") ||      String::equalNoCase(cmd,"gc"))    return _getClass(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"enumerateClasses") ||           String::equalNoCase(cmd,"ec"))    return _enumerateClasses(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"enumerateClassNames") ||           String::equalNoCase(cmd,"ecn"))    return _enumerateClassNames(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"getInstance") ||           String::equalNoCase(cmd,"gi"))    return _getInstance(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"enumerateInstances") ||           String::equalNoCase(cmd,"ei"))    return _enumerateInstances(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"enumerateInstanceNames") ||           String::equalNoCase(cmd,"ein"))    return _enumerateInstanceNames(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"getProperty") ||           String::equalNoCase(cmd,"gp"))    return _getProperty(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"setProperty") ||           String::equalNoCase(cmd,"sp"))    return _setProperty(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"invokeMethod") ||           String::equalNoCase(cmd,"im"))    return _invokeMethod(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"createClass") ||           String::equalNoCase(cmd,"cc"))    return _createClass(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"modifyClass") ||           String::equalNoCase(cmd,"mc"))    return _modifyClass(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"deleteClass") ||           String::equalNoCase(cmd,"dc"))    return _deleteClass(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"createInstance") ||           String::equalNoCase(cmd,"ci"))    return _createInstance(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"modifyInstance") ||           String::equalNoCase(cmd,"mi"))    return _modifyInstance(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"deleteInstance") ||           String::equalNoCase(cmd,"di"))    return _deleteInstance(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"associators") ||           String::equalNoCase(cmd,"a"))    return _associators(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"associatorNames") ||           String::equalNoCase(cmd,"an"))    return _associatorNames(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"references") ||           String::equalNoCase(cmd,"r"))    return _references(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"referenceNames") ||           String::equalNoCase(cmd,"rn"))    return _referenceNames(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"execQuery") ||           String::equalNoCase(cmd,"exq"))    return _execQuery(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"getQualifier") ||           String::equalNoCase(cmd,"gq"))    return _getQualifier(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"setQualifier") ||           String::equalNoCase(cmd,"sq"))    return _setQualifier(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"deleteQualifier") ||           String::equalNoCase(cmd,"dq"))    return _deleteQualifier(argc-2,&argv[2]);  else if (String::equalNoCase(cmd,"enumerateQualifiers") ||           String::equalNoCase(cmd,"eq"))    return _enumerateQualifiers(argc-2,&argv[2]);  else  {    cerr << cmd << ": Invalid CIM operation."<< endl;   _usage();    return 1;  }}// ===============================================================// getClass// ===============================================================int _getClass(const int argc, const char **argv){  if (argv[0]==0)  {    cerr << "Usage: cimop getClass|gc <class>" << endl;    return 1;  }  CIMClass cldef;  try  {    cldef = _c.getClass( _nameSpace, argv[0] );  }  catch (Exception& e)  {    cerr << /* "getClass: " << */ e.getMessage() << endl;    return 1;  }  // Display the class definition  // without qualifiers, for the moment  // First the class name and superclass  cout << "class " << cldef.getClassName().getString() << " : "    << cldef.getSuperClassName().getString() << endl;  cout << "{" << endl;  // Now the properties  // No qualifiers except [key], but specify type, array  for (Uint32 i=0; i<cldef.getPropertyCount(); i++)  {    CIMProperty p = cldef.getProperty(i);    cout << "  ";    // output key, if required    if (_isKey(p)) cout << "[ Key ] ";    // prepare to output type, but    // first, if type is "reference", find target class    if (p.getType() == CIMTYPE_REFERENCE)      cout << p.getReferenceClassName().getString() << " REF ";    // output type    else cout << cimTypeToString(p.getType()) << " ";    // output name    cout << p.getName().getString();    // output array, if required    if (p.isArray()) cout << "[]";    // final eol    cout << ";" << endl;  }  // need to do methods  for (Uint32 i=0; i<cldef.getMethodCount(); i++)  {    CIMMethod m = cldef.getMethod(i);    // output type    cout << "  " << cimTypeToString(m.getType()) << " ";    // output name    cout << m.getName().getString() << "(";    // output parameters    // new line if there are any parameters    for (Uint32 j=0; j<m.getParameterCount(); j++)    {      CIMParameter p = m.getParameter(j);      // output IN/OUT qualifiers on a fresh line      cout << endl << "    [ ";      // loop through qualifiers looking for IN, OUT      for (Uint32 k=0; k<p.getQualifierCount(); k++)      {        // when one found, output its value        CIMQualifier q = p.getQualifier(k);        if (q.getName().equal("in") ||            q.getName().equal("out"))        {          cout << q.getName().getString() << " ";        }      }      // Now the type      cout << "] " << cimTypeToString(p.getType()) << " ";      // finally the name      cout << p.getName().getString();      // array brackets      if (p.isArray()) cout << "[]";      // closing , on parameter if not last      if (j != m.getParameterCount()-1) cout << ",";    }    // after last param, indent before closing paren    // close paren    cout << ")";    // if (m.isArray()) cout << "[]";    // finish output    cout << ";" << endl;  }  // final brace and done  cout << "};" << endl;  return 0;}// ===============================================================// enumerateClasses// ===============================================================int _enumerateClasses(const int argc, const char **argv){  cerr << "Not yet implemented" << endl;  return 1;}// ===============================================================// enumerateClassNames// ===============================================================int _enumerateClassNames(const int argc, const char **argv){  // This curious function calls a version of itself recursively  // so that it can display the classes in tree form  // We want to indent by an increasing amount at each level,  // starting with no indentation  String tab;  // First time we call the client API in a try/catch to  // catch possible bad class spec supplied by user.  // Subsequent calls in the recursive function don't need to.  Array<CIMName> cn;  try  {    if (argc < 1)      cn = _c.enumerateClassNames( _nameSpace );    else      cn = _c.enumerateClassNames( _nameSpace, argv[0] );  }  catch (Exception& e)  {    cerr << /* "enumerateClassNames: " << */ e.getMessage() << endl;    return 1;  }  // Show namespace if not default  if (0!=getenv("CIM_NAMESPACE"))    cerr << "Classes in namespace " << _nameSpace << ":" << endl;  for (Uint32 i=0; i<cn.size(); i++)  {    // print class name after current tab amount    cout << tab << cn[i].getString() << endl;    // recurse to print subclasses of this class with a larger tab    if (_recursiveClassEnum( cn[i], tab+"  " )!=0) return 1;  }  return 0;}// ===============================================================// getInstance// ===============================================================int _getInstance(const int argc, const char **argv){  if (argv[0] == 0)  {    _giUsage();    return 1;  }  // need to get class definition to find keys  // first arg is name of class  CIMClass cldef;  try  {    cldef = _c.getClass( _nameSpace, argv[0] );  }  catch(Exception& e)  {    cerr << /* "getInstance: " << */ e.getMessage() << endl;    return 1;  }  CIMObjectPath ref;  CIMInstance inst;  // If there are no more args, prompt user for keys  if (argv[1] == 0) ref = CIMObjectPath(String::EMPTY, // hostname left blank                                       _nameSpace,                                       argv[0],                                       _inputInstanceKeys(cldef));  // else if there's another arg and it's "list", enumInstNames and print  // a list from which user will select (return if none)  else if (String::equalNoCase("list",argv[1]))  {    ref = _selectInstance(argv[0]);    // An empty ObjectPath means nothing was selected    if (ref.identical(CIMObjectPath())) return 0;  }  // else there's another arg but it's invalid  else  {    _giUsage();    return 1;  }  // get the specified instance  try  {    inst = _c.getInstance(_nameSpace,ref);  }  catch(Exception& e)  {    cerr << /* "getInstance: " << */ e.getMessage() << endl;    return 1;  }  _displayInstance(inst);  return 0;}// ===============================================================// enumerateInstances// ===============================================================int _enumerateInstances(const int argc, const char **argv){  if (argv[0] == 0)  {    cerr << "Usage: cimop enumerateInstances|ei <class>" << endl;    return 1;  }  Array<CIMInstance> ia;  try  {    ia = _c.enumerateInstances( _nameSpace, argv[0] );  }  catch(Exception& e)  {    cerr << /* "enumerateInstances: " << */ e.getMessage() << endl;    return 1;  }  cerr << ia.size() << " instances" << endl;  for (Uint32 i=0; i<ia.size(); i++)  {    cout << endl;    // display property names and values    _displayInstance(ia[i]);  }  return 0;}// ===============================================================// enumerateInstanceNames// ===============================================================int _enumerateInstanceNames(const int argc, const char **argv){  if (argv[0] == 0)  {    cerr << "Usage: cimop enumerateInstanceNames|ein <class>" << endl;    return 1;  }  Array<CIMObjectPath> iNames;  try  {    iNames = _c.enumerateInstanceNames( _nameSpace, argv[0] );  }  catch(Exception& e)  {    cerr << /* "enumerateInstanceNames: " << */ e.getMessage() << endl;    return 1;  }  cerr << iNames.size() << " instance(s)" << endl;  for (Uint32 i=0; i<iNames.size(); i++)    cout << "  " << iNames[i].toString() << endl;  return 0;}// ===============================================================// getProperty// ===============================================================int _getProperty(const int argc, const char **argv){  if (argc < 2)  {    _gpUsage();    return 1;  }  // need to get class definition to find keys  // first arg is name of class  CIMClass cldef;  try  {    cldef = _c.getClass( _nameSpace, argv[0] );  }  catch(Exception& e)  {    cerr << /* "getProperty: " << */ e.getMessage() << endl;    return 1;  }  CIMObjectPath ref;  CIMInstance inst;  // If next arg is "ask", prompt user for keys  if (String::equalNoCase("ask",argv[1])) ref = CIMObjectPath(String::EMPTY,                                                   _nameSpace,                                                   argv[0],                                                   _inputInstanceKeys(cldef) );  // else if the next arg and is "list", enumInstNames and print  // a list from which user will select  else if (String::equalNoCase("list",argv[1]))  {    ref = _selectInstance( argv[0] );    if (ref.identical(CIMObjectPath())) return 0;  }  // else there's another arg but it's invalid  else  {    _gpUsage();    return 1;  }  CIMProperty pDef;  // if no more args, display property names and ask which  if (argc < 3)  {    Uint32 n;    for (n=0; n<cldef.getPropertyCount(); n++)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -