如何检测脚本是否在虚拟机上运行? [英] How to detect if the script is running on a virtual machine?

查看:574
本文介绍了如何检测脚本是否在虚拟机上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题不需要任何解释. 我正在写一个可以引导虚拟机来模拟网络的软件.如果该软件已经在虚拟机上运行,​​我想禁用此功能.

I don't think the question needs any explanation. I'm writing a software that can boot virtual machines to simulate a network. I would like to disable this feature if the software is already running on a virtual machine.

我已经阅读了这篇文章:如何检测我的应用程序是否在虚拟机中运行? 但是我需要在Linux上执行该操作,而该帖子仅涵盖Windows.

I already read this post: How to detect if my application is running in a virtual machine? But I need to do it on Linux and that post covers only Windows.

我在Perl中将其标记为Perl问题,但是proc中可能有一些文件或其他地方需要检查,并且所有语言都相同.我真的不知道.

I'm tagging it as a Perl question as I've to do it in Perl, but probably there are some file in proc or somewhere else to check and it's the same for all languages. I don't know actually.

我对主要检测VirtualBox和KVM感兴趣,因为这些是我正在使用的VM.

I'm interested in detecting mainly VirtualBox and KVM, as those are the VMs I'm using.

那么,有什么建议吗?

推荐答案

我将专门讨论作为来宾操作系统运行Linux的VMware和虚拟Box虚拟机. 如果使用以下命令运行,您将知道基础硬件是VMware/VirtualBox,它证明它是虚拟机.

I will talk specific to VMware and virtual Box Virtual Machines running Linux as guest Operating system. If you run below command, you will come to know that the underlying hardware is VMware/VirtualBox which certifies that it is a Virtual Machine.

对于VMware guest虚拟机:

For VMware guest:

# dmidecode  | grep -i product
        Product Name: VMware Virtual Platform

对于Virtual Box来宾:

For Virtual Box guest:

# dmidecode  | grep -i product
    Product Name: VirtualBox

"dmidecode"是linux系统命令.您可以让perl在脚本的开头运行dmidecode并提取值.如果它是虚拟机,则该脚本应退出而无需进一步执行.

"dmidecode" is a linux system command. You can have perl run dmidecode in the beginning of your script and extract the value. If it is a virtual machine then the script should exit without any further execution.

我没有任何其他虚拟机管理程序可以让您获得上面命令返回的内容.

I do not have any other hypervisor at my disposal to get you what above command return on them.

希望这会有所帮助.

这篇关于如何检测脚本是否在虚拟机上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆