博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++之动态配置ip信息
阅读量:4027 次
发布时间:2019-05-24

本文共 7858 字,大约阅读时间需要 26 分钟。

## configManager.h

#ifndef CONFIGACMUCONTROLLER_H#define CONFIGACMUCONTROLLER_H#include 
using namespace std;class ConfigACMUController{public: static ConfigACMUController * getInstance(); inline void setIsSetting(bool flag) { m_isSetting = flag; } inline bool isSetting() { return m_isSetting; } inline string getAcmuIP() { return m_acmu_ip; } inline string getExtIfIP() { return m_extif_ip; } void startConfig(string acmu_ip,string extif_ip); static void * OnConfigAcmuThread(void * p); void onReportStatus(int state,string content);private: ConfigACMUController(); bool m_isSetting; pthread_t m_config_tid; string m_acmu_ip; string m_extif_ip;};#endif // CONFIGACMUCONTROLLER_H
## configManager.cpp

#include "configacmucontroller.h"#include 
#include "ping_dev.h"#include
#include
#include
#include "csettings.h"using namespace std;/*************************************************************************************************** This Api is Used to Format String for C++.***************************************************************************************************/string formatString(const char* fmt, ...){ va_list args; static char buffer[1024]; va_start(args, fmt); vsnprintf((char *) buffer, 1024, (char *) fmt, args); va_end(args); return string(buffer);}static ConfigACMUController * gInstance = NULL;/*************************************************************************************************** This Api is Used to run the command by pip method.***************************************************************************************************/static int runCommand(string cmd,string & result){ FILE *stream = popen(cmd.c_str(),"r"); if (stream == NULL) { return -1; } static char buff[4*1024]; memset(buff,0,4*1024); fread(buff,sizeof(char),sizeof(buff),stream); pclose(stream); if (strlen(buff) > 0) { result = buff; } return 0;}/*************************************************************************************************** Create Only One Instance.*************************************************************************************************/ConfigACMUController * ConfigACMUController::getInstance(){ if ( NULL == gInstance) { gInstance = new ConfigACMUController(); } return gInstance;}/*************************************************************************************************** Constructor*************************************************************************************************/ConfigACMUController::ConfigACMUController(){ m_isSetting = false;}/*************************************************************************************************** Used to report the running result (OK or Wrong)*************************************************************************************************/void ConfigACMUController::onReportStatus(int state,string content){ cout << "===================Python Exec Result as Following====================" << endl; cout << "state::" << state << endl; cout << "content::" << content << endl; cout << "=======================================================================" << endl; m_isSetting = false;}/*************************************************************************************************** 0. Create one thread for processing the config task.*************************************************************************************************/void ConfigACMUController::startConfig(string acmu_ip,string extif_ip){ if (m_isSetting) { cout << __FUNCTION__ << "KU_CMA is aleady configing the acmu, so ignore the 2th same request" << endl; return; } cout << __FUNCTION__ << "Will Begin Config ACMU Network Enviroment." << "KU_IP:[" << acmu_ip << "]" << "eth1_IP:[" << extif_ip << "]" << endl; m_isSetting = true; m_acmu_ip = acmu_ip; m_extif_ip = extif_ip; pthread_create(&m_config_tid, NULL, OnConfigAcmuThread, this);}/*************************************************************************************************** 0. modify ifcfg-eth1 ok* 1. /etc/sysconfig/network-scripts/ifcfg-eth1 not exist.* 2. pint ku_ip failed.* 3. modify ifcfg-eth1 failed.*************************************************************************************************/void * ConfigACMUController::OnConfigAcmuThread(void * p){ cout << "\n**********************Enter ACMU Config Thread *********************\n" << endl; pthread_detach(pthread_self()); ConfigACMUController * controller = (ConfigACMUController *)(p); if ( NULL == controller) { cout << __FUNCTION__ << "param [controller] is NULL" << endl; return NULL; } int ret = -1; string rslt; {//[1]-save the eth1`s IP string eth1_file = "/etc/sysconfig/network-scripts/ifcfg-eth1"; string eth1_bkup_file = "/etc/sysconfig/network-scripts/ifcfg-eth1_backupFile"; if ( access(eth1_file.c_str(),F_OK) != 0) { controller->onReportStatus(1, formatString("File:(%s) not exist",eth1_file.c_str())); return NULL; } system(formatString("cp %s %s",eth1_file.c_str(),eth1_bkup_file.c_str()).c_str()); } {//[2]-config eht1 and ping ku`s ip string cmd1 = formatString("ifconfig eth1 %s && ping %s -w %d", controller->getExtIfIP().c_str(), controller->getAcmuIP().c_str(), atoi(CSettings::getInstance()->getNetworkWaitperiod().c_str())); ret = runCommand(cmd1,rslt); if (ret != 0) { controller->onReportStatus(2,formatString("command pipe open error: [%s]",cmd1.c_str())); return NULL; } string::size_type comma_pos = rslt.find("100% packet loss", 0); if (comma_pos != string::npos) { cout << __FUNCTION__ << formatString("%s is a invalid ip",controller->getAcmuIP().c_str()) << endl; controller->onReportStatus(3,formatString("%s is a invalid ip",controller->getAcmuIP().c_str())); return NULL; } cout << __FUNCTION__ << formatString("%s is a valid ip",controller->getAcmuIP().c_str()) << endl; } {//[3]-update /etc/../ifcfg-eth1 && /usr/donica/conf/ifcfg-eth1 string modify_scriptFile = "/usr/donica/script/modify_acmu_cfg.py"; if ( access(modify_scriptFile.c_str(),F_OK) != 0) { cout << __FUNCTION__ << formatString("File(%s) not exist!",modify_scriptFile.c_str()) << endl; controller->onReportStatus(3,formatString("File(%s) not exist!",modify_scriptFile.c_str())); return NULL; } string cmd2 = formatString("python %s %s",modify_scriptFile.c_str(),controller->getExtIfIP().c_str()); ret = runCommand(cmd2,rslt); if (ret != 0) { controller->onReportStatus(2,formatString("command pipe open error [%s]!",cmd2.c_str())); return NULL; } cout << __FUNCTION__ << formatString("script[%s] exec result::%s", modify_scriptFile.c_str(), rslt.c_str())<< endl; if (rslt.find("OK",0) != string::npos) { cout << "Modify config file OK" << endl; controller->onReportStatus(0,"modify ifcfg-eth1 ok,will restart Server."); } else { cout << "Modify config file Failed" << endl; controller->onReportStatus(4,"modify ifcfg-eth1 failed"); } } return NULL;}

转载地址:http://scvbi.baihongyu.com/

你可能感兴趣的文章
微信小程序开发全线记录
查看>>
Centos import torchvision 出现 No module named ‘_lzma‘
查看>>
网页设计里的浮动 属性
查看>>
Maximum Subsequence Sum
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
Django 的Error: [Errno 10013]错误
查看>>
机器学习实战之决策树(一)
查看>>
[LeetCode By Python] 2 Add Two Number
查看>>
python 中的 if __name__=='__main__' 作用
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[LeetCode By Python]9. Palindrome Number
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]107. Binary Tree Level Order Traversal II
查看>>
[LeetCode By Python]108. Convert Sorted Array to Binary Search Tree
查看>>