using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace snmp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//变量声明
String ipStr = "";
String oidStr = "";
String cmdStr = "";
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.SelectedIndex = 0;
}
//关机命令
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("cmd.exe", "/c shutdown -s");
}
//关机命令解除
private void button2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("cmd.exe", "/c shutdown -a");
}
//实现snmp get命令
private void button_get_Click(object sender, EventArgs e)
{
//IP不能为空
if(this.textBox_ip.Text == "")
{
MessageBox.Show("请先填写管理端IP地址!");
return;
}
//OID不能为空
if (this.textBox_oid.Text == "")
{
MessageBox.Show("请您添加所要操作的OID");
return;
}
//get operation
ipStr = this.textBox_ip.Text;
oidStr = this.textBox_oid.Text;
cmdStr = "snmptool.exe get " + ipStr + " public " + oidStr;
Process p_get = new Process();
p_get.StartInfo.FileName = "cmd.exe";
// 这里是关键点,不用Shell启动/重定向输入/重定向输出/不显示窗口
p_get.StartInfo.UseShellExecute = false;
p_get.StartInfo.RedirectStandardInput = true;
p_get.StartInfo.RedirectStandardOutput = true;
p_get.StartInfo.CreateNoWindow = true;
p_get.Start();
// 向cmd.exe输入command
p_get.StandardInput.WriteLine(cmdStr);
p_get.StandardInput.WriteLine("exit");
p_get.WaitForExit(40000);
string s = p_get.StandardOutput.ReadToEnd();// 得到cmd.exe的输出
MessageBox.Show(s);
p_get.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//注意:默认的索引是从0开始的
int i = this.comboBox1.SelectedIndex+1;
switch (i)
{
case 1:
this.textBox_type.Text = "Integer";
break;
case 2:
this.textBox_type.Text = "OctetString";
break;
case 3:
this.textBox_type.Text = "ObjectIdentfier";
break;
case 4:
this.textBox_type.Text = "IpAddress";
break;
case 5:
this.textBox_type.Text = "Counter";
break;
case 6:
this.textBox_type.Text = " Gauge";
break;
case 7:
this.textBox_type.Text = " TimeTicks";
break;
case 8:
this.textBox_type.Text = " Opaque";
break;
default:
break;
}
}
private void button_set_Click(object sender, EventArgs e)
{
ipStr = this.textBox_ip.Text;
oidStr = this.textBox_oid.Text;
String typeStr = this.comboBox1.Text;
String value = this.textBox_value.Text;
cmdStr = "snmptool.exe set " + ipStr + " public " + oidStr + " " + typeStr + " " + value;
//
Process p_set = new Process();
p_set.StartInfo.FileName = "cmd.exe";
// 这里是关键点,不用Shell启动/重定向输入/重定向输出/不显示窗口
p_set.StartInfo.UseShellExecute = false;
p_set.StartInfo.RedirectStandardInput = true;
p_set.StartInfo.RedirectStandardOutput = true;
p_set.StartInfo.CreateNoWindow = true;
p_set.Start();
// 向cmd.exe输入command
p_set.StandardInput.WriteLine(cmdStr);
p_set.StandardInput.WriteLine("exit");
p_set.WaitForExit(20000);
String s = p_set.StandardOutput.ReadToEnd();
MessageBox.Show(s);
p_set.Close();
}
//v1trap 按钮事件
private void button_v1trap_Click(object sender, EventArgs e)
{
//就这一句也可以实现相应的启动功能
System.Diagnostics.Process.Start("cmd.exe", "/c snmptool trap");
}
}
}