android x86 支付宝,.NET自动安装zabbix客户端(代码)

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Diagnostics;

using System.Management;

using System.ServiceProcess;

namespace Zabbix3AgentSetup

{

class Program

{

static void Main(string[] args)

{

// string test=File.ReadAllText("../../serverip.txt");

// Console.WriteLine(test);

// Console.Read();

killProcess("zabbix_agentd");

if(ServiceIsExisted("Zabbix Agent")){

if (GetOSBit() != 32)

{

RunCmd(@"C:zabbix_agents_2.4.4.wininwin64zabbix_agentd.exe -d -c C:zabbix_agents_2.4.4.winconfzabbix_agentd.win.conf");

}

else

{

RunCmd(@"C:zabbix_agents_2.4.4.wininwin32zabbix_agentd.exe -d -c C:zabbix_agents_2.4.4.winconfzabbix_agentd.win.conf");

}

}

if(Directory.Exists(@"c:zabbix_agents_2.4.4.win"))

{

Directory.Delete(@"c:zabbix_agents_2.4.4.win", true);

}

Directory.CreateDirectory(@"c:zabbix_agents_2.4.4.win");

CopyFile(@"....zabbix_agents_2.4.4.win", @"c:zabbix_agents_2.4.4.win");

string machineName = Environment.MachineName;

string serverip = File.ReadAllText(@"....serverip.txt");

string str1 = ("ServerActive=" + serverip);

string str2 = ("Hostname=" + machineName);

using (FileStream fs = new FileStream(@"c:zabbix_agents_2.4.4.winconfzabbix_agentd.win.conf", FileMode.OpenOrCreate, FileAccess.Write))

{

using (StreamWriter sw = new StreamWriter(fs))

{

sw.BaseStream.Seek(0, SeekOrigin.End);

sw.WriteLine("{0}

", str1, DateTime.Now);

sw.WriteLine("{0}

", str2, DateTime.Now);

sw.Flush();

}

}if (GetOSBit() != 32) { RunCmd(@"C:zabbix_agents_2.4.4.wininwin64zabbix_agentd.exe -i -c C:zabbix_agents_2.4.4.winconfzabbix_agentd.win.conf"); RunCmd(@"C:zabbix_agents_2.4.4.wininwin64zabbix_agentd.exe -s -c C:zabbix_agents_2.4.4.winconfzabbix_agentd.win.conf"); } else { RunCmd(@"C:zabbix_agents_2.4.4.wininwin32zabbix_agentd.exe -i -c C:zabbix_agents_2.4.4.winconfzabbix_agentd.win.conf"); RunCmd(@"C:zabbix_agents_2.4.4.wininwin32zabbix_agentd.exe -s -c C:zabbix_agents_2.4.4.winconfzabbix_agentd.win.conf"); } StartService("Zabbix Agent"); Console.WriteLine("done"); } static bool ServiceIsExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName == serviceName) { return true; } } return false; } static void killProcess(string name) { Process[] pro = Process.GetProcesses();//获取已开启的所有进程 //遍历所有查找到的进程 for (int i = 0; i < pro.Length; i++) { //判断此进程是否是要查找的进程 if (pro[i].ProcessName.ToString().ToLower() == name) { pro[i].Kill();//结束进程 } } } public static void StartService(string serviceName) { try { using (System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(serviceName)) { TimeSpan timeout = new TimeSpan(0, 0, 15); if (sc.Status != ServiceControllerStatus.Running) { sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running, timeout); } } } catch (Exception e) { } } /// /// 获取操作系统位数(x2021年05月26日) /// /// int public static int GetOSBit() { try { string addressWidth = String.Empty; ConnectionOptions mConnOption = new ConnectionOptions(); ManagementScope mMs = new ManagementScope(@"\localhost", mConnOption); ObjectQuery mQuery = new ObjectQuery("select AddressWidth from Win32_Processor"); ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(mMs, mQuery); ManagementObjectCollection mObjectCollection = mSearcher.Get(); foreach (ManagementObject mObject in mObjectCollection) { addressWidth = mObject["AddressWidth"].ToString(); } return Int32.Parse(addressWidth); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return 32; } } static void RunCmd(string cmdStr) { System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动 p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息 p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息 p.StartInfo.RedirectStandardError = true;//重定向标准错误输出 p.StartInfo.CreateNoWindow = true;//不显示程序窗口 p.Start();//启动程序 //向cmd窗口发送输入信息 p.StandardInput.WriteLine(cmdStr); p.StandardInput.WriteLine("exit"); p.StandardInput.AutoFlush = true; //p.StandardInput.WriteLine("exit"); //向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死 //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令 //获取cmd窗口的输出信息 //string output = p.StandardOutput.ReadToEnd(); //StreamReader reader = p.StandardOutput; //string line=reader.ReadLine(); //while (!reader.EndOfStream) //{ // str += line + " "; // line = reader.ReadLine(); //} p.WaitForExit();//等待程序执行完退出进程 p.Close(); // Console.WriteLine(output); } /// /// 复制文件 /// /// 源路径 /// 新路径 static void CopyFile(string sources, string dest) { DirectoryInfo dinfo = new DirectoryInfo(sources); //注,这里面传的是路径,并不是文件,所以不能保含带后缀的文件 foreach (FileSystemInfo f in dinfo.GetFileSystemInfos()) { //目标路径destName = 目标文件夹路径 + 原文件夹下的子文件(或文件夹)名字 //Path.Combine(string a ,string b) 为合并两个字符串 String destName = Path.Combine(dest, f.Name); if (f is FileInfo) { //如果是文件就复制 File.Copy(f.FullName, destName, true);//true代表可以覆盖同名文件 } else { //如果是文件夹就创建文件夹然后复制然后递归复制 Directory.CreateDirectory(destName); CopyFile(f.FullName, destName); } } }}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值