深入分析bmc nsh下nexec runcmd和runscript指令使用例子

作者:袖梨 2022-06-30

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

一、单机普通指令执行

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

代码如下 复制代码

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

二、runcmd和runscript

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

1、runcmd

代码如下 复制代码
CMBBSA% runcmd -h 10.212.52.252 10.212.52.253 -e uptime
==> 10.212.52.252 16:50pm up 7:24, 1 user, load average: 0.20, 0.30, 0.28
==> 10.212.52.253 cd: no route to host: //10.212.52.253/

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

代码如下 复制代码
10% 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指定的是一个脚本,示例:

代码如下 复制代码
CMBBSA% runscript -f //@/C/361way/hosts.txt -e //@/C/361way/ifconfig.sh
==> 10.212.52.252 eth0 Link encap:Ethernet HWaddr 00:50:56:A8:65:7E
inet addr:10.212.52.252 Bcast:10.212.52.255 Mask:255.255.255.0
inet6 addr: fe80::250:56ff:fea8:657e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:83773 errors:0 dropped:9093 overruns:0 frame:0
TX packets:9869 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:23426732 (22.3 Mb) TX bytes:1292499 (1.2 Mb)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:38330 errors:0 dropped:0 overruns:0 frame:0
TX packets:38330 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2935885 (2.7 Mb) TX bytes:2935885 (2.7 Mb)
==> 10.211.160.211 eth0 Link encap:Ethernet HWaddr 00:50:56:9C:1B:00
inet addr:10.211.160.211 Bcast:10.211.160.255 Mask:255.255.255.0
inet6 addr: fe80::250:56ff:fe9c:1b00/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4940145 errors:0 dropped:103704 overruns:0 frame:0
TX packets:946952 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2195897939 (2094.1 Mb) TX bytes:97687372 (93.1 Mb)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:156694 errors:0 dropped:0 overruns:0 frame:0
TX packets:156694 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:12128544 (11.5 Mb) TX bytes:12128544 (11.5 Mb)

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

代码如下 复制代码

# hosts.txt文件
10.212.52.252
10.211.160.211
#ifconfig文件
#!/bin/bash
ifconfig

3、runcmd的帮助

代码如下 复制代码

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

三、nexec

先看下nexec的man帮助

代码如下 复制代码
CMBBSA% man nexec
nexec(1) nexec(1)
NAME
nexec - Engine to interface remote commands.
SYNOPSIS
nexec [-?] [-t term] [-o] [-i] [-l] [-nohup hostname "cmd &"] -e |
hostname cmd [args]
DESCRIPTION
The nexec program works in one of two ways. If the program is called
explicitly, it uses the syntax nexec ARG1 ARG2. The first argument is
either the name of the host on which the specified command should be
executed or the command option -e, which indicates that the command
should be executed on the current remote host, as determined by the
current working directory. The remaining arguments arels nex the name
and arguments of the remote program to be executed.
The other way to call the nexec program is by calling a command that is
implicitly linked to the nexec program. Invoking a command that is
linked to nexec automatically translates the command from to
nexec , where the host is determined by the program's
CMBBSA%

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

代码如下 复制代码
10% runcmd -h 10.212.52.252 -e rcsshd status
==> 10.212.52.252 nsh: command not found: rcsshd
10% nexec 10.212.52.252 rcsshd status
Checking for service sshd running
10%

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

代码如下 复制代码

10% nexec 10.212.52.252 service sshd restart
doneting down the listening SSH daemon
doneting SSH daemon
10%
10% nexec 10.212.52.252 service sshd restart |redi //10.211.160.211/opt/ssh.log

四、其他

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

bmc

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

代码如下 复制代码

C:/Documents and Settings/Administrator>cd /d c:/
C:/>cd 361way
C:/361way>nsh disk.nsh
c:/361way>more disk.nsh
echo '10.212.52.252' | redi //10.212.52.252/tmp/diskinfo.txt
nexec 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
echo '10.211.160.211' | redi -a //10.212.52.252/tmp/diskinfo.txt
nexec 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
echo '10.211.160.212' | redi -a //10.212.52.252/tmp/diskinfo.txt
nexec 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
echo '10.211.160.213' | redi -a //10.212.52.252/tmp/diskinfo.txt
nexec 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 ,一个是覆盖,一个是追加

相关文章

精彩推荐