使用LINUX/EXPECT获取主机的总内存 [英] Get Total Memory of a host with LINUX/EXPECT

查看:52
本文介绍了使用LINUX/EXPECT获取主机的总内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用'.exp'脚本来获取主机的总内存.

I would like to get the total memory of a host by using a '.exp' script.

进入主机后,执行以下行:

After getting inside the host, I execute the following line:

"send "top -n 1 | grep Mem: | awk '{ print $(NF-7) }' | cut -d 'k' -f1\r""

在执行脚本时,它会返回以下错误:

While executing the script, it returns the following error:

 can't read "(NF-7)": no such variable

但是如果手动进入主机并执行同一行(不包含发送"部分),它将返回我期望的数字...

But if get manually inside the host, and execute the same line ( without the "send" part) , it returns me the expected number...

我做错了什么?任何想法?有其他建议可以这样做吗?

What I'm doing wrong? Any idea? Any proposal for other way of doing the same?

致以最诚挚的谢意,

安东尼奥

推荐答案

top 的输出并不是要以这种方式进行解析.您可以通过其他更简单(更好!)的方式获得内存量.

The output of top is not meant to be parsed this way. You can get the amount of memory in other, easier (better!) ways.

sed -n -e '/^MemTotal/s/^[^0-9]*//p' /proc/meminfo

或者您可以解析 free 的输出,该输出也知道如何以方便的尺寸显示:

Or you can parse the output of free, which also knows how to display in convenient sizes:

# total memory in megabytes
free -m | sed  -n -e '/^Mem:/s/^[^0-9]*\([0-9]*\) .*/\1/p'

您总是必须问自己谁扩展了什么?"

You always have to ask yourself "Who expands what?"

要按预期发送这些命令,请尝试

To send these commands with expect, try

send "sed -n -e '/^MemTotal/s/^\[^0-9\]*//p' /proc/meminfo\n"

# also removes kb suffix
send "sed -n -e '/^MemTotal/{s/^\[^0-9\]*//;s/ .*//p}' /proc/meminfo\n"

# displays in megabytes
send "free -m | sed -n -e '/^Mem:/{s/^\[^0-9\]*//;s/ .*//p}'\n"

这避免了 expect 可能会处理的原本用于 sed 的事情.

This escapes from expect things that it would otherwise process and which were intended for sed.

这是原件的(正确转义的)版本.

And here's a (properly escaped) version of your original.

send "top -n 1 | grep Mem: | awk '{ print \$(NF-7) }' | cut -d 'k' -f1\n"

这篇关于使用LINUX/EXPECT获取主机的总内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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