BMC软件公司[NYSE:BMC],是全球领先的云计算和IT管理解决方案提供商,其提供成套的应用解决方案,这里主要介绍的是BladeLogic Server Automation Suite下的几个指令。该软件实现的功能和saltstack、puppet等类似,用于服务器的自动化管理和批量操作。其相对于开源软件的优点是稳定、直观,缺点是源代码不公开(尽管提供了一些接口)。

一、单机普通指令执行

可以通过在安装有控制端软件的主机上通过cmd下执行nsh进入bmc network shell 界面,也可以通过在开始--程序中找到bmc network shell 并执行。这里以cmd下为例

1C:\Documents and Settings\Administrator>nsh
2CMBBSA% cd //10.211.160.211           /* cd //IP表示跳到相应的被纳管主机上,直接执行的指令会在该主机上执行 */
310% uptime     /* 在10.211.160.211主机上进行uptime指令查询 */
4 16:48pm  up 2 days  2:04,  3 users,  load average: 0.30, 1.47, 4.58

二、runcmd和runscript

当需要在多台主机上执行指令时,可以通过runcmd和runscript执行,具体如下:

1、runcmd

1CMBBSA% runcmd -h 10.212.52.252 10.212.52.253 -e uptime
2==> 10.212.52.252 <==
3 16:50pm  up   7:24,  1 user,  load average: 0.20, 0.30, 0.28
4==> 10.212.52.253 <==
5cd: no route to host: //10.212.52.253/

由于后面一台主机未安装client纳管,所以会报没有route到主机。也可以通过-f参数指定主机列表、通过redi指令将结果输出到文件。

110% runcmd -f //@/C/361way/hosts.txt -e uptime |redi //@/C/361way/uptime.txt

注://@指本机,这里也可以通过//IP指向一台被纳管主机;hosts.txt是主机列表,每行一个IP或每行一个主机名;redi 也可以输出到每纳管主机的一个分区,由于这里管理软件装在windows主机上,//@/C,指本机的c盘下的361way目录,当然也可以指定一台被纳管的linux,如:redi ///10.212.52.253/opt/361way/uptime.txt 。

2、runscript

其用法和runcmd基本一样,只不过其-e指定的是一个脚本,示例:

 1CMBBSA% runscript -f //@/C/361way/hosts.txt -e //@/C/361way/ifconfig.sh
 2==> 10.212.52.252 <==
 3eth0      Link encap:Ethernet  HWaddr 00:50:56:A8:65:7E
 4          inet addr:10.212.52.252  Bcast:10.212.52.255  Mask:255.255.255.0
 5          inet6 addr: fe80::250:56ff:fea8:657e/64 Scope:Link
 6          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 7          RX packets:83773 errors:0 dropped:9093 overruns:0 frame:0
 8          TX packets:9869 errors:0 dropped:0 overruns:0 carrier:0
 9          collisions:0 txqueuelen:1000
10          RX bytes:23426732 (22.3 Mb)  TX bytes:1292499 (1.2 Mb)
11lo        Link encap:Local Loopback
12          inet addr:127.0.0.1  Mask:255.0.0.0
13          inet6 addr: ::1/128 Scope:Host
14          UP LOOPBACK RUNNING  MTU:16436  Metric:1
15          RX packets:38330 errors:0 dropped:0 overruns:0 frame:0
16          TX packets:38330 errors:0 dropped:0 overruns:0 carrier:0
17          collisions:0 txqueuelen:0
18          RX bytes:2935885 (2.7 Mb)  TX bytes:2935885 (2.7 Mb)
19==> 10.211.160.211 <==
20eth0      Link encap:Ethernet  HWaddr 00:50:56:9C:1B:00
21          inet addr:10.211.160.211  Bcast:10.211.160.255  Mask:255.255.255.0
22          inet6 addr: fe80::250:56ff:fe9c:1b00/64 Scope:Link
23          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
24          RX packets:4940145 errors:0 dropped:103704 overruns:0 frame:0
25          TX packets:946952 errors:0 dropped:0 overruns:0 carrier:0
26          collisions:0 txqueuelen:1000
27          RX bytes:2195897939 (2094.1 Mb)  TX bytes:97687372 (93.1 Mb)
28lo        Link encap:Local Loopback
29          inet addr:127.0.0.1  Mask:255.0.0.0
30          inet6 addr: ::1/128 Scope:Host
31          UP LOOPBACK RUNNING  MTU:16436  Metric:1
32          RX packets:156694 errors:0 dropped:0 overruns:0 frame:0
33          TX packets:156694 errors:0 dropped:0 overruns:0 carrier:0
34          collisions:0 txqueuelen:0
35          RX bytes:12128544 (11.5 Mb)  TX bytes:12128544 (11.5 Mb)

hosts.txt和ifconfig.sh文件的内容为:

1# hosts.txt文件
210.212.52.252
310.211.160.211
4
5#ifconfig文件
6#!/bin/bash
7ifconfig

3、runcmd的帮助

 1CMBBSA% man runcmd
 2runcmd(1)                                                            runcmd(1)
 3NAME
 4       runcmd - Run a Network Shell command on one or more hosts
 5SYNOPSIS
 6       runcmd  [-v  -n  -p  n]  [-H header] [-NH] [-s | -c] [-d directory] [-f
 7       file] [-h host1 ... hostn] [-e command1 ... commandn]
 8       runscript [-v -n -p n] [-H header] [-NH] [-s | -c] [-d  directory]  [-f
 9       file] [-h host1 ... hostn] [-e command1 ... commandn]
10DESCRIPTION
11       The  programs runcmd and runscript let you run the same command on mul-
12       tiple machines. The difference between the two is that runcmd  executes
13       a shell command, while runscript runs the given Network Shell script on
14       each machine.
15       Depending on what action you are currently performing, you may want  to
16       know  which  host  you  are  dealing with. To this end, the environment
17       variable NSH_RUNCMD_HOST is set for each sub-command that is run.  Fur-
18       thermore the  environment variable NSH_RUNCMD_DIR is set indicating the
19CMBBSA%

三、nexec

先看下nexec的man帮助

 1CMBBSA% man nexec
 2nexec(1)                                                              nexec(1)
 3NAME
 4       nexec - Engine to interface remote commands.
 5SYNOPSIS
 6       nexec  [-?]  [-t  term]  [-o]  [-i] [-l] [-nohup hostname "cmd &"] -e |
 7       hostname cmd [args]
 8DESCRIPTION
 9       The nexec program works in one of two ways. If the  program  is  called
10       explicitly,  it uses the syntax nexec ARG1 ARG2.  The first argument is
11       either the name of the host on which the specified  command  should  be
12       executed  or  the  command  option -e, which indicates that the command
13       should be executed on the current remote host,  as  determined  by  the
14       current  working  directory. The remaining arguments arels nex the name
15       and arguments of the remote program to be executed.
16       The other way to call the nexec program is by calling a command that is
17       implicitly  linked  to  the  nexec  program. Invoking a command that is
18       linked to nexec automatically translates the command from <command>  to
19       nexec  <host>  <command>, where the host is determined by the program's
20CMBBSA%

直接看这段帮助信息可能有点懵懂,这里给个示例看下runcmd与nexec的区别

110% runcmd -h 10.212.52.252 -e rcsshd status
2==> 10.212.52.252 <==
3nsh: command not found: rcsshd
410% nexec 10.212.52.252 rcsshd status
5Checking for service sshd                                            running
610%

rcsshd是suse系统下的一个指令,使用runcmd时,发现无法执行,因为runcmd执行的是network shell指令;而nexec则直接通过接口可以调用主机上的指令并输出。同样可以利用redi 指令将结果输出到文件:

110% nexec 10.212.52.252 service sshd restart
2doneting down the listening SSH daemon
3doneting SSH daemon
410%
510% nexec 10.212.52.252 service sshd restart  |redi //10.211.160.211/opt/ssh.log

四、其他

通过管理界面执行任务相当简单和简洁,显示结果也比较直观,管理界面运行结果图如下 :

bmc

为什么我们还需要使用指令呢?由于默认情况下管理界面只简洁的显示成功与否,不会将结果输出到文件。而通过指令可以详细的查看执行过程和输出结果,这里以一个系统空间使用率为例,可能通为以下方法将每台主机超过50%的挂载点输出出来:

 1C:\Documents and Settings\Administrator>cd /d c:\
 2C:\>cd 361way
 3C:\361way>nsh disk.nsh
 4c:\361way>more disk.nsh
 5echo '10.212.52.252' | redi //10.212.52.252/tmp/diskinfo.txt
 6nexec 10.212.52.252 df -hl|grep '^/'|sed 's/%//g'|awk '{if($5>50) print $0}' | redi -a //10.212.52.252/tmp/diskinfo.txt
 7echo '10.211.160.211' | redi -a //10.212.52.252/tmp/diskinfo.txt
 8nexec 10.211.160.211 df -hl|grep '^/'|sed 's/%//g'|awk '{if($5>50) print $0}' | redi -a //10.212.52.252/tmp/diskinfo.txt
 9echo '10.211.160.212' | redi -a //10.212.52.252/tmp/diskinfo.txt
10nexec 10.211.160.212 df -hl|grep '^/'|sed 's/%//g'|awk '{if($5>50) print $0}' | redi -a //10.212.52.252/tmp/diskinfo.txt
11echo '10.211.160.213' | redi -a //10.212.52.252/tmp/diskinfo.txt
12nexec 10.211.160.213 df -hl|grep '^/'|sed 's/%//g'|awk '{if($5>50) print $0}' | redi -a //10.212.52.252/tmp/diskinfo.txt

注意:redi 与redi -a ,一个是覆盖,一个是追加。