# Script for Memory calculation

**URL:** <https://forums.suse.com/t/script-for-memory-calculation/23597>\
**Category:** SLES Configure-Administer\
**Created:** [February 7, 2013, 5:50am UTC](https://forums.suse.com/t/script-for-memory-calculation/23597 "2013-02-07T05:50:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oj43085](https://avatars.discourse-cdn.com/v4/letter/o/e19b73/32.png) [@oj43085](https://forums.suse.com/u/oj43085)\
**Post date:** [February 7, 2013, 5:50am UTC](https://forums.suse.com/t/script-for-memory-calculation/23597/1 "2013-02-07T05:50:36Z")

</div>

Hi,

Admins, forgive me if I am posting a scripting question here, I think this is the most relevant sub-category I found.

I would like to know if there is a way to script:

1)ssh to server IP

2)free -m \> file

1. Perform this operation: [(used buffer/cached)/(memory used)+(memory available)]\*100

2. dump output to $serverip\_free.log

3. end ssh

4. repeat for next server

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![malcolmlewis](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/malcolmlewis/32/11375_2.png) [@malcolmlewis](https://forums.suse.com/u/malcolmlewis)\
**Post date:** [February 7, 2013, 6:27am UTC](https://forums.suse.com/t/script-for-memory-calculation/23597/2 "2013-02-07T06:27:39Z")

</div>

> [@](#):
>
> On Thu 07 Feb 2013 03:54:02 AM CST, oj43085 wrote:
> 
> Hi,
> 
> Admins, forgive me if I am posting a scripting question here, I think  
> this is the most relevant sub-category I found.
> 
> I would like to know if there is a way to script:
> 
> 1)ssh to server IP
> 
> 2)free -m \> file
> 
> 1. Perform this operation: [(used buffer/cached)/(memory used)+(memory  
> available)]\*100
> 
> 2. dump output to $serverip\_free.log
> 
> 3. end ssh
> 
> 4. repeat for next server
> 
> Any help would be greatly appreciated.

Hi  
Do you have to use ssh? The nagios webserver application and the nagios  
plugin client and scripts will do this for use.

You could use expect or a perl script with Net::ssh;  
[http://search.cpan.org/~ivan/Net-SSH-0.09/SSH.pm](http://search.cpan.org/~ivan/Net-SSH-0.09/SSH.pm)

–  
Cheers Malcolm Â°Â¿Â° (Linux Counter #276890)  
openSUSE 12.2 (x86\_64) Kernel 3.4.11-2.16-desktop  
up 14:37, 3 users, load average: 0.01, 0.03, 0.05  
CPU IntelÂ® i5 CPU M520@2.40GHz | GPU IntelÂ® Ironlake Mobile

---

<div class="post-metadata">

**Author:** ![Jens-U](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@Jens-U](https://forums.suse.com/u/Jens-U)\
**Post date:** [February 7, 2013, 2:10pm UTC](https://forums.suse.com/t/script-for-memory-calculation/23597/3 "2013-02-07T14:10:41Z")

</div>

Hi oj43085,

yes, that is possible. You can run any scripted code on the remote server by giving that command list to the ssh client, something along the lines

for i in $hostlist ; do  
ssh $i “my cmd1; mycmd2;…”  
done

In order not to have to type in each remote’s password, setting up an appropriate public key environment (“authorized\_keys”) would be really helpful. And of course you can decide which remote user to contact etc.

Just for completeness: In case you’d be starting remote jobs as background tasks, it’s advisable to locally detach stdin (“ssh -n”) and to properly redirect the output of the remote process at the remote machine. A “hanging” ssh session (your remote script has completed, but the session will not terminate) is a typical indicator for such a situation.

As you have not indicated your scripting skills, I assume your question was actually only concerning the “ssh” remote invocation part.

And like Malcolm already pointed out: If that’s some regular task, in terms of continuous monitoring, there are far better tools to do this, Nagios being near or at the top of such a list.

Regards,  
Jens
