0

我有一个 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;
        }
4

3 回答 3

0

System.Net.NetworkInformation.NetworkInterface class is in System.dll according to this: http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface . Did you try adding a reference to System.dll when compiling? I believe it's the "-r" argument to mcs.

BTW, what version of Mono are you using. Debian is famous for shipping very old versions of Mono in the "stable" flavour. Mono 2.10.x or higher is recommended, nowadays, as stable.

于 2012-07-12T23:04:10.307 回答
0

System.Net.NetworkInformation.NetworkInterface was introduced .NET 2.0. You need to target .NET 2.0 profile or newer if you want to use it.

If you are compiling using command line compiler use gmcs or dmcs to compile your project. If you are using MonoDevelop you need to set in project setting appropriate framework version.

于 2012-07-13T06:59:23.967 回答
-1

This is how you might do this in Ruby.

begin
  theMac = File.read("/usr/path/to/text/file.txt")

rescue Exception => e
  puts e.message
  puts e.backtrace.inspect
end

response = `ifconfig eth0`
mac = response.match(/HWaddr (.*?):(.*?):(.*?):(.*?):(.*?):(.*?) /)

if theMac == mac[0] then `sudo shutdown -h now`; end

f = File.open("/usr/path/to/text/file.txt", "w")
f.write(mac[0])
f.close()
于 2012-07-12T22:24:42.063 回答