【发布时间】:2012-07-12 22:06:48
【问题描述】:
我有一个 debian 服务器,我将它的 mac 地址保存在 /usr 文件夹中的一个 txt 文件中。
每次我启动机器时,我尝试执行的脚本都应该检查当前的 mac 地址,并将其与保存在 txt 文件中的地址进行比较。如果它们不匹配,则机器应关闭。
代码可以在 Windows 上运行,但我必须让它在 debian 上运行。所以我安装了单声道(apt-get install mono-complete),创建了包含代码的 .cs 文件并将其传输到 debian 机器上。
当我运行 mcs shutdown.cs(shutdown 是我创建的文件的名称)命令时,我收到此错误:
CS0234:名称空间“System.Net”中不存在类型或名称空间名称“NetworkInformation”。您是否缺少程序集参考?**
如何解决?
using System.Net.NetworkInformation;
static void Main(string[] args)
{
string newmc = Convert.ToString(GetMacAddress()); //current mac address
string mc = File.ReadAllText(@"/usr/mc.txt"); //mac address saved on a txt file previously
if (newmc != mc) //shutdown if the mac addresses dont match
{
System.Diagnostics.Process.Start("shutdown.exe", "-s -t 0");
}
}
static string GetMacAddress()
{
string macAddresses = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.NetworkInterfaceType != NetworkInterfaceType.Ethernet) continue;
if (nic.OperationalStatus == OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
break;
}
}
return macAddresses;
}
【问题讨论】:
-
这个脚本看起来好像是为 Windows 编写的。你需要对谷歌做一些研究,我建议你从研究如何使用命令行关闭机器开始。
-
立即关闭。但是我不能在cs文件中包含它吗?
-
是的,我同意它是用胜利的心态写的,因为我不知道 BASH 等。这就是为什么我希望在 MONO 中帮助我。
-
K 2 秒,我正在研究如何在 Ruby 脚本中轻松完成。