X-Git-Url: https://git.stg.codes/ssmd.git/blobdiff_plain/0bfa03722f65de7d08bfb8665054bff68e25ea20..8481ea2740db7758f5533abfdbdbacce1ac6b919:/src/switch.cpp diff --git a/src/switch.cpp b/src/switch.cpp index 444324b..71917ed 100644 --- a/src/switch.cpp +++ b/src/switch.cpp @@ -130,21 +130,32 @@ void Switch::sync() } { - std::string fileName(_ip + ".sh"); + std::ostream * stream = NULL; + std::string fileName(_settings.scriptBase() + "/" + _ip + ".sh"); std::string newFileName(fileName + ".new"); - std::ofstream script(newFileName.c_str()); - script << "#!/bin/sh\n"; - if (!dropACLs(target, script)) { + + if (_settings.dumpScripts()) { + stream = new std::ofstream(newFileName.c_str()); + *stream << "#!/bin/sh\n"; + } else { + stream = new std::stringstream; + } + + if (!dropACLs(target, *stream)) { logger << "Switch::sync() - failed to drop ACLs for the switch '" << _ip << "'" << std::endl; + delete stream; return; } - if (!createACLs(target, script)) { + if (!createACLs(target, *stream)) { logger << "Switch::sync() - failed to create ACLs for the switch '" << _ip << "'" << std::endl; + delete stream; return; } - script.close(); - rename(newFileName.c_str(), fileName.c_str()); + + delete stream; + if (_settings.dumpScripts()) + rename(newFileName.c_str(), fileName.c_str()); } if (_settings.isDebug()) { @@ -217,7 +228,8 @@ bool Switch::dropACLsByTable(const CTarget & target, unsigned profileId, const S size_t chunks = aclsList.size() / _settings.maxACLPerPDU() + 1; for (size_t i = 0; i < chunks && it != aclsList.end(); ++i) { Pdu pdu; - stream << "snmpset -v2c -cgts_community_w " << _ip; + if (_settings.dumpScripts()) + stream << "snmpset -v2c -c" << _writeCommunity << " " << _ip; for (size_t j = 0; j < _settings.maxACLPerPDU() && it != aclsList.end(); ++j, ++it) { int id; if (int c = it->get_value(id) != SNMP_CLASS_SUCCESS) { @@ -230,9 +242,11 @@ bool Switch::dropACLsByTable(const CTarget & target, unsigned profileId, const S Vb vb(Oid(dropACLOid.c_str())); vb.set_value(int(6)); pdu += vb; - stream << " " << dropACLOid << " i 6"; + if (_settings.dumpScripts()) + stream << " " << dropACLOid << " i 6"; } - stream << "\n"; + if (_settings.dumpScripts()) + stream << "\n"; if (int c = _snmp.set(pdu, target) != SNMP_CLASS_SUCCESS) { if (c != SNMP_ERROR_TOO_BIG) { logger << "Switch::dropACLsByTable() - failed to invoke Snmp::set for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'" << std::endl; @@ -260,7 +274,8 @@ bool Switch::createACLs(const CTarget & target, std::ostream & stream) pdu.clear(); _aclsCreated = true; ++pos; - stream << "snmpset -v2c -cgts_community_w " << _ip << " " << *it << "\n"; + if (_settings.dumpScripts()) + stream << "snmpset -v2c -c" << _writeCommunity << " " << _ip << " " << *it << "\n"; } return true; }