无法连接到一个 VM 上的网络适配器,相同的代码适用于另一个 VM
Posted
技术标签:
【中文标题】无法连接到一个 VM 上的网络适配器,相同的代码适用于另一个 VM【英文标题】:Cannot attach to a network adapter on one VM, same code works on another VM 【发布时间】:2011-06-10 10:20:55 【问题描述】:我需要从网卡获取性能计数器。为方便起见,用户通过在控制台应用程序中键入索引号来选择所需的适配器。
这是获取用户输入并创建性能计数器实例的代码
var connectionNames = NetworkCardLocator.GetConnectedCardNames().ToArray();
log.Debug("Please select one of the available connections");
log.Debug("--");
for (int i = 0; i < connectionNames.Count(); i++)
log.Debug(i + ". " + connectionNames[i]);
log.Debug("--");
var key = Console.ReadLine();
int idx = 0;
Int32.TryParse(key, out idx);
string connectionName = connectionNames[idx];
var networkBytesSent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", connectionName);
var networkBytesReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", connectionName);
var networkBytesTotal = new PerformanceCounter("Network Interface", "Bytes Total/sec", connectionName);
这就是我选择可用适配器的方式
internal static IEnumerable<string> GetConnectedCardNames()
string query = String.Format(@"SELECT * FROM Win32_NetworkAdapter");
var searcher = new ManagementObjectSearcher
Query = new ObjectQuery(query)
;
try
log.Debug("Trying to select network adapters");
var adapterObjects = searcher.Get();
var names = (from ManagementObject o in adapterObjects
select o["Name"])
.Cast<string>();
return names;
catch (Exception ex)
log.Debug("Failed to get needed names, see Exception log for details");
log.Fatal(ex);
throw;
问题 鉴于我选择了所需的适配器,代码可以在我的机器上运行(Win 2008 R2 Ent x64)。它不适用于我用于测试的某些 VM(Win 2008 R1 DC x86)。那里的任何选择都会给我一个例外(仍然适用于我的 PC 和 VM Win 2008 R1 Std x86)
foreach (PerformanceCounter counter in counters)
float rawValue = counter.NextValue(); //thrown here
...
2011-06-10 11:08:20,505 [10] DEBUG TH.Exceptions Instance 'WAN Miniport (PPTP)' does not exist in the specified Category.
System.InvalidOperationException: Instance 'WAN Miniport (PPTP)' does not exist in the specified Category.
at System.Diagnostics.CounterDefinitionSample.GetInstanceValue(String instanceName)
at System.Diagnostics.PerformanceCounter.NextSample()
at System.Diagnostics.PerformanceCounter.NextValue()
at TH.PerformanceMonitor.API.Internal.PerformanceLogService.DoPerformanceLogging(IEnumerable`1 counters, Int32 interval, TimeSpan duration) in C:\Projects\...\PerformanceLogService.cs:line 122
at TH.PerformanceMonitor.API.PerformanceManager.DoPerformanceLogging() in C:\Projects\...\PerformanceManager.cs:line 294
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
问 可能是什么问题,或者我该如何追查原因?
【问题讨论】:
【参考方案1】:问题出在phantom network adapters。从 VM 中删除它们后,一切都开始工作了。
【讨论】:
以上是关于无法连接到一个 VM 上的网络适配器,相同的代码适用于另一个 VM的主要内容,如果未能解决你的问题,请参考以下文章
无法在 GCP 上打开 vm 实例的 ssh:无法连接到后端(代码:4003)