Chinaunix

标题: 请问如何在linux下使用perl,通过WMI来获取远程windows的信息? [打印本页]

作者: jjqing    时间: 2010-05-30 13:29
标题: 请问如何在linux下使用perl,通过WMI来获取远程windows的信息?
请问如何在Linux下,通过perl模块,获取远程windows机器的信息?(不使用telnet)

我看了一些例子,通过Win32::OLE和DBD::WMI,来获取WINDOWS机器的CPU等信息,但是这些例子上的脚本,应该都是在windows上运行的,因为在linux上,无法安装Win32::OLE和DBD::WMI这两个模块。

请问在Linux下能够使用wmi来连接到远程windows吗?
或者有没有其他方法,能够在linux下使用perl,来获取远程windows信息?
比如说CPU使用率,某个进程状态,等等

谢谢!

以下是我查询到的windows上的perl脚本,希望能找到Linux上的相应解决方法,谢谢!

use Win32::OLE qw[in];
my $host = $ARGV[0] || '.';
my $wmi = Win32::OLE->GetObject( "winmgmts://$host/root/cimv2" )
        or die Win32::FormatMessage( Win32::OLE::LastError() );

my %instances = (
        Win32_PhysicalMemory => &get_pmem,
        Win32_PerfRawData_PerfOS_Memory => &get_amem,
        Win32_Processor => &get_load,
        Win32_LogicalDisk => &get_disk,
);

my $out = get_perf_data();
print "$out\n";

sub get_perf_data {
        my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
        $year = $year + 1900;
        $mon  = $mon + 1;
        my $str = sprintf "%4.4d-%2.2d-%2.2d",$year,$mon,$mday;
        my $timestr = sprintf "%2.2d:%2.2d:%2.2d",$hour,$min,$sec;

        my $mem;
        foreach ( keys %instances ) {
                my $class = $wmi->InstancesOf( $_ );
                $mem .= $instances{ $_ }->( $class );
        }

        my $out = "##nCollect Time: ".$str." ".$timestr."n".$mem."%%rn";
        return $out;
}

# get cpu loadavg
sub get_load {
        my $class = shift;
        my $total="";
        my $i = 0;
        $i++,$total = $total."CPU No. $i: ".$_->{LoadPercentage}."%n" foreach in($class);
        return $total;
}

# get total memory size
sub get_pmem {
        my $class = shift;
        my $total;
        $total += $_->{Capacity} foreach in($class);
        return "Physical Memory: $total Bytesn";
}

# get available memory size
sub get_amem {
        my $class = shift;
        my $amem;
        $amem .= join ' ', $_->{AvailableBytes} foreach in($class);
        return "Available Memory: $amem Bytesn";
}

# get free disk sizes
sub get_disk {
        my $class = shift;
        my $total = "";
        $total .= "DISK ".$_->{DeviceID}." Free: ".$_->{FreeSpace}." Bytesn" foreach in($class);

        return $total;
}
作者: dugu072_cu    时间: 2010-05-31 09:43
WMI 本来就是 windows 提供的功能,这些模块能否做到独立OS,你去看看模块的document嘛,个人感觉不太可能

至于除了 WMI 的其他方法,这就和是否远程连接没有多大关系了,远程OS自身提供很多CLI的管理工具,调用不就可以了?比如 PsList, PsKill,tasklist 等
作者: shijiang1130    时间: 2010-05-31 10:47
通过snmp协议吧
作者: iamlimeng    时间: 2010-05-31 12:33
本机WMI信息知道弄,远程的不知道。

也等答案。
作者: jjqing    时间: 2010-05-31 13:32
回复 4# iamlimeng


    远程的WMI,如果脚本是在windows上运行,也好办,使用Win32::OLE或DBD::WMI,就可以。

我的问题在于,脚本必须是得在Linux上运行,Linux上不知道能用什么模块,连接到远程windows的WMI。

期待。
作者: qnxchina    时间: 2010-05-31 16:21
回复  iamlimeng


    远程的WMI,如果脚本是在windows上运行,也好办,使用Win32::OLE或DBD::WMI,就 ...
jjqing 发表于 2010-05-31 13:32



    俺们用STAF
全部机器上安装STAF,然后加入信任你机器的STAF配置,远程调用staf process功能用wmic读东西
PS: STAF跨平台
作者: SNYH    时间: 2010-06-01 22:01
看看  samba套件里面的
rpcclient 这个工具 能满足你不

RPCCLIENT(1)                     User Commands                    RPCCLIENT(1)

NAME
       rpcclient - tool for executing client side MS-RPC functions

SYNOPSIS
       rpcclient [-A authfile] [-c <command string>] [-d debuglevel] [-h]
        [-l logdir] [-N] [-s <smb config file>] [-U username[%password]]
        [-W workgroup] [-I destinationIP] {server}

DESCRIPTION
       This tool is part of the samba(7) suite.

       rpcclient is a utility initially developed to test MS-RPC functionality
       in Samba itself. It has undergone several stages of development and
       stability. Many system administrators have now written scripts around
       it to manage Windows NT clients from their UNIX workstation.

作者: jjqing    时间: 2010-06-03 15:24
回复 7# SNYH


谢谢!
这个也不失为一个解决方法,目前在测试机上,关闭windows防火墙后,确实可以登录:
[root@dev snmp]# rpcclient 3.3.3.1 -U Administrator
Password:
rpcclient $> ?
---------------         ----------------------
        TESTING
         testme         Sample test
---------------         ----------------------
       SHUTDOWN
---------------         ----------------------
           ECHO
     echoaddone         Add one to a number
       echodata         Echo data
       sinkdata         Sink data
     sourcedata         Source data
---------------         ----------------------
作者: wind_ch    时间: 2010-06-03 17:37
通过snmp协议吧
shijiang1130 发表于 2010-05-31 10:47




作者: qwert_13    时间: 2012-11-07 17:45
在windows上用wmi生成文件,ftp传送到unix上监控呗。
不然你就windows上开启snmp管理吧,用snmp能监控部分信息,没有wmi的全。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2