从单个命令行操作确定操作系统 [英] Determine OS from a single command line operation

查看:223
本文介绍了从单个命令行操作确定操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介:



如果我提供IP,我有一个魔术工具可以在机器上执行命令行操作。
该工具知道机器正在使用的操作系统,并根据是否为Windows / linux在cmd / shell上执行命令,并盲目地返回该命令的输出。


C:> tool.exe 172.140.56.2ipconfig


假设


  1. 每台机器一个操作系统。工具在执行命令时没有问题(无论是否失败都是不同的问题)

  2. 操作系统始终是windows或linux。

  3. 根据命令结果确定操作系统

问题:



使用这个能力执行命令,我想确定操作系统



我的解决方案: >

执行ipconfig命令。如果结果是


-bash:ipconfig:command not found


这是linux。



否则如下:

  Windows IP配置
以太网适配器本地连接:
...

然后Windows。



问题:



知道这是否是一个万无一失的方法这样做。我想要一个命令,在某些情况下不会失败。 (例如cygwin安装在windows上允许linux命令成功)或者ipconfig成功在linux下的一些特殊的scneario。)



我可以用一些解析器处理命令输出,如果



只是为了消除任何混乱。它可以是任意命令。

解决方案

如果我正确理解你的问题,那么 uname 是理想的命令。如果它是任何Unix系统(包括OSX),它会返回正确的变量,如果是Windows,它将返回命令找不到或类似。


Introduction:

I have a 'magic' tool that can perform a command line operation on a machine if I provide the IP. The tool knows the OS that machine is using and executes the command on cmd/shell based on whether it is windows/linux and returns the output of the command back blindly.

C:> tool.exe 172.140.56.2 "ipconfig"

Assumptions:

  1. One OS per machine. Tool has no problem executing the command (whether it fails or not is a different problem)
  2. The OS is either windows or linux always.
  3. I determine the OS based on the command result

Problem:

Using this power of being able to execute a command, I want to determine the OS

My Solution:

Execute ipconfig command. If result is

-bash: ipconfig: command not found

It is linux.

Else if it is like this:

Windows IP Configuration
Ethernet adapter Local Area Connection:
...

then Windows.

Question:

I wanted to know if this is a foolproof way of doing this. I want a command which would not fail under certain scenarios. (say cygwin installed on windows allowing linux commands to succeed. Or ipconfig succeeding on linux under some special scneario.)

I can process the command output with some parser, if that helps in any way.

Just to clear any confusion. It can be ANY command. I ust used ipconfig in my example.

解决方案

If I understood your problem correctly, then uname is the ideal command. If it's any Unix-system (including OSX), it'll return the correct variable, and if it's Windows it'll return command not found or similar.

这篇关于从单个命令行操作确定操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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