Is there a CPAN Perl module to find total/used/free memory on a host (without running command line through a system() call)? - perl

Is there a CPAN Perl module which provides total/used/free memory (like vmstat does) on a host without running a command line through a system() call to get the info?
Ideally it should be cross-platform (Linux and Solaris) though please provide Linux or Solaris-only ones as well.

Looks like Sys::MemInfo is what I needed.
It is implemented in XS, using <sys/sysinfo.h> for Linux and <sys/stat.h> / <sys/swap.h> for Solaris.

Try the Sys::Statistics::Linux distrubition for linux.

Related

getopt on linux and solaris

On linux the following operation with getopt works fine:
TEMP=`getopt :mvfuhr:: --long "mask,verbose,force,unmask, help, remask::" -n 'test.sh' -- "$#"`
On Solaris, i am unable use the long arguments to process...
Though the this works:
TEMP=`getopt :mvfuhr:: "$#"`
Looks like the getopt bundled with the solaris is of an older version. How can i make it work like linux? or is there some setting that needs to be done to process long arguments?
Here is a clever (although currently with a minor bug) way to handle this issue:
Using getopts in bash shell script to get long and short command line options

Disk usage in Perl Core

I’m looking for a way to check the remaining free space on a disk within Perl. I can’t use CPAN since I have to deploy the script on many servers with different versions of Perl, and I can’t change it because my team leader ordered me that way.
Any idea? I tried File::stat but I can’t use it on D:\ (the script runs on Windows versions).
Thanks!
fsutil volume diskfree C:
For Windows servers you can run this cmd command from system() method.

How to set the cross platform interpreter path?

I would like my scripts works on Windows and UNIX system, so I want some code as blew(just a sample, not work)
if $^O eq 'MSWin32' {
#!/c:/perl/bin/perl.exe
}
else { / non-windows system
#!/usr/local/bin/perl
}
is there any way to do this in perl? it seems the #! line must be the first line of a perl script, so this is impossible?
The shebang (#!/path/to/interpreter) is only used on unix-like systems. And it has to be the first line. On Windows systems, the shebang is not used, instead file ending associations may be used: you can associate the .pl file ending with the perl interpreter.
Command line switches in the shebang will be interpreted by the perl interpreter, regardless of platform.
A safe way to launch perl scripts on all platforms is to actually use the perl command. It will be available in case of an successfull Perl installation. E.g. perl myscript.pl instead of ./myscript.pl. The second options requires that the file is set as "executable" on *nix systems.
#! must be the first two characters of the file, because that's where the kernel looks for them when it tries to launch your file. They're part of a directive for the OS (called the shebang line), and the OS knows nothing about Perl.
If you use a standard Perl installer (such as ExtUtils::MakeMaker) to installer your script, just use #!perl and the installer will adjust any existing #! line for your system.
It's not possible. The "#!" is looked at by the exec*() family of Unix functions, to determine how to run the file. So you can't do any scripting there.
It's not all that useful anyway, since the Windows command prompt (or CreateProc() function, but I'm not sure whether that can be used to launch a batch file) doesn't look for the #!.

How to make command line tool work in windows and linux?

Making my PHP Command line application support Linux and Windows. Currently it has this code below to work from command line on Linux/unix
How can I make it work on Windows? I lan on having a setting to determine if the sytem is Linux or Windows and using the correct commands based on that but I do not know how to make these function below work in Windows
exec() is a PHP function to run stuff through the command line
exec("rm -f $dest_file", $var);
exec("mv $quant_file {$this->tmp_path}/{$src_filename}-quant.png");
You could test which platform you're on using the PHP_OS constant and run commands accordingly.
I would, however, suggest that you use the PHP provided filesystem functions (if possible).
Here are some links:
http://www.php.net/manual/en/ref.filesystem.php
http://www.php.net/manual/en/ref.dir.php

How to open ssh session and execute commands from a Perl script?

I have a Perl script running on a Windows machine. I need this script to open a ssh session to a remote Unix machine, and to be able to execute certain commands on that Unix machine and to be able to get the output returned from these commands.
These commands are generated during the run-time of the script, and there are many of them executed at different times.
How can I do it?
Approach 1: Use CYGWIN: http://perlwin32ssh.blogspot.com/2007/07/test_4418.html
Approach 2: Use Net::SSH::W32Perl module.
This is one thread discussing how to install it: http://code.activestate.com/lists/perl-win32-users/29180/ (It seems to require downloading custom version of the module)
This thread should help with the problems arising from dependencies on math libraries needed for ssh calculations: http://www.issociate.de/board/post/494356/I%27m_trying_to_install_%27Net::SSH::Perl%27_on_a_Windows_Box..html
Caveat emptor: I never installed this, the above is just result of some analysis of google results.
#!/usr/bin/perl
system("ssh foo 'ls -l'");
Or go through the hassle of using ptmx(4) on the local side and ssh -t for remote.