private void btnProcess_Click(object sender, EventArgs e) { try { if (MessageBox.Show("This will update the DHCP server assignments and will restart the service.\n Are you sure? ", "Info", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes) { string _serverDHCP = txtServerName.Text; string _tmpFile = System.IO.Path.GetTempPath() + cboCOD3.Text + "_NET.conf"; string _configFile = ""; //create the temp File var _RSD = new DynamicRS("Select * from vDHCPConfig where COD3='" + cboCOD3.Text + "'", Values.gDatos); _RSD.Open(); string _group = ""; while (!_RSD.EOF) { if (_RSD["Group"].ToString() != _group) { if (_group != "") { _configFile += "\t}\n"; } _group = _RSD["Group"].ToString(); _configFile += "# " + _group + '\n' + "group " + _group + "{" + '\n'; } _configFile += "\t# " + _RSD["Comment"].ToString() + '\n'; _configFile += "\thost " + _RSD["Host"].ToString() + " {" + '\n'; _configFile += "\t\thardware ethernet " + _RSD["MAC"].ToString() + ";" + '\n'; _configFile += "\t\tfixed-address " + _RSD["IP"].ToString() + ";" + '\n'; _configFile += "\t\t}\n"; _RSD.MoveNext(); } _configFile += "\t}\n"; _RSD.Close(); File.WriteAllText(_tmpFile, _configFile); lblMsg.Text = "File created OK."; //move the file to the server using (var client = new SftpClient(_serverDHCP, txtUser.Text, txtPassword.Text)) { lblMsg.Text = "Connecting the server."; client.Connect(); lblMsg.Text = "Server Connected!"; client.ChangeDirectory("/etc/dhcp/"); using (var fileStream = new FileStream(_tmpFile, FileMode.Open)) { client.BufferSize = 4 * 1024; // bypass Payload error large files client.UploadFile(fileStream, Path.GetFileName(_tmpFile)); } lblMsg.Text = "File updated!"; client.Disconnect(); } //restart the dhcp service using (var client = new SshClient(_serverDHCP, txtUser.Text, txtPassword.Text)) { client.Connect(); client.RunCommand("service isc-dhcp-server restart"); lblMsg.Text = "DHCP Service restarted!"; client.Disconnect(); } MessageBox.Show("The process has finished correctly.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
// Print HU labels public void ZPLPrintLabels(DynamicRS rs, string labelType) { string _printerAddress = Values.LabelPrinterAddress.ToString(); int _printerResolution; string _zplTemplate = "", _zplCode = ""; // Get settings for the printer using (var _RS = new StaticRS($"select ValueString,ValueInteger from MiscData where Code='{Values.LabelPrinterAddress}' and cod3='{Values.COD3}'", Values.gDatos)) { _RS.Open(); if (!_RS.EOF) { _printerAddress = _RS["ValueString"].ToString(); // "\\\\valsrv02\\VALLBLPRN003"; _printerResolution = Convert.ToInt32(_RS["ValueInteger"]); if (_printerResolution != 203 && _printerResolution != 300) { MessageBox.Show($"Printer DPI not set in database.", "Print", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { MessageBox.Show($"Printer {_printerAddress} not found.", "Print", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // Get the ZPL template code using (var _RS = new StaticRS($"select ValueString from MiscData where Code='{labelType}_{_printerResolution}' and cod3='{Values.COD3}'", Values.gDatos)) { _RS.Open(); if (!_RS.EOF) { _zplTemplate = _RS["ValueString"].ToString(); } } if (_zplTemplate == "") { MessageBox.Show("ZLP template not found.", "Print HU", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } using (var _printer = new cRawPrinterHelper(_printerAddress)) { while (!rs.EOF) { // Get the Template code _zplCode = _zplTemplate; // Replace values for (int i = 0; i < rs.FieldCount; i++) { _zplCode = _zplCode.Replace("<" + rs.Fields[i] + ">", rs[rs.Fields[i]].ToString()); } // Print the label _printer.SendUTF8StringToPrinter(_zplCode, 1); rs.MoveNext(); } } }
private void btnProcess_Click(object sender, EventArgs e) { try { if (MessageBox.Show("This will update the DNS server assignments and will restart the service.\n Are you sure? ", "Info", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes) { string _serverDNS = txtServerName.Text; var _SP = new SP(Values.gDatos, "pGetContador"); _SP.AddParameterValue("@Contador", 0); _SP.AddParameterValue("@Serv", ""); _SP.AddParameterValue("@Codigo", "BIND_SERIAL"); _SP.Execute(); var _serial = _SP.Parameters["@Contador"].Value.ToString(); _SP = null; string _tmpFile = System.IO.Path.GetTempPath() + "local.hosts"; string _configFile = "$ttl 38400\n"; _configFile += "local. IN SOA valsrv02. informatica.espackeuro.com. (\n"; _configFile += " "+ _serial + "\n"; _configFile += " 10800\n"; _configFile += " 3600\n"; _configFile += " 604800\n"; _configFile += " 38400 )\n"; _configFile += "local. IN NS valsrv02.\n"; //create the temp File var _RSD = new DynamicRS("Select entry from vDNSFile order by entry", Values.gDatos); _RSD.Open(); while (!_RSD.EOF) { _configFile += _RSD["entry"] + "\n"; _RSD.MoveNext(); } //_configFile += "\t}\n"; _RSD.Close(); File.WriteAllText(_tmpFile, _configFile); lblMsg.Text = "File created OK."; //move the file to the server using (var client = new SftpClient(_serverDNS, txtUser.Text, txtPassword.Text)) { lblMsg.Text = "Connecting the server."; client.Connect(); lblMsg.Text = "Server Connected!"; client.ChangeDirectory("/var/lib/bind/"); using (var fileStream = new FileStream(_tmpFile, FileMode.Open)) { client.BufferSize = 4 * 1024; // bypass Payload error large files client.UploadFile(fileStream, Path.GetFileName(_tmpFile)); } lblMsg.Text = "File updated!"; client.Disconnect(); } //restart the dhcp service using (var client = new SshClient(_serverDNS, txtUser.Text, txtPassword.Text)) { client.Connect(); client.RunCommand("service bind9 reload"); lblMsg.Text = "DNS Service reloaded!"; client.Disconnect(); } MessageBox.Show("The process has finished correctly.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } }