Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Need help with a grep script

by seenu18 (Initiate)
on Nov 08, 2013 at 10:32 UTC ( #1061697=perlquestion: print w/ replies, xml ) Need Help??
seenu18 has asked for the wisdom of the Perl Monks concerning the following question:

Hi I'm asked to write a sample script for the following scenario

PROCEDURE

Step 1: Login to the node using SSH or Telnet.

Step 2: Execute the below command

i. Show process cpu | grep Total get the 5s time value

[local]st-tb3-ssr1#show process cpustats | grep Total Total system CPU % usage (5s, 1m, 5m): 7.44, 8.13, 8.64

Note: Total CPU utilization is based on an average of all the cores.

ii. Show memory -> get the Total, Used, Free

[local]st-tb3-ssr1#show memory Memory: Total 24555360k, Used 1679228k, Free 22876132k, Reserved 0k

iii. Show process -> get the process name, CPU % and memory

[local]st-tb3-ssr1#show process Load Average : 0.92 0.88 0.93 NAME PID SPAWN MEMORY TIME %CPU STATE UP/DOWN pakio-protect 3905 1 7328K 00:04:50.10 1.17% run u2l 3568 1 4680K 00:00:00.84 0.00% run 09:47:01 metad 3569 1 34760K 00:00:02.24 0.00% run 09:47:01 evtmd 3605 1 5624K 00:00:04.67 0.02% run 09:46:51 pnsd 3609 1 5764K 00:00:03.42 0.01% run 09:46:51

iv. Show process card # -> get the process name, cpu % and memory

[local]st-tb3-ssr1#show process card 2 -------------------------------------------------------------- Slot number : 2/LP Card Type : 10ge-10-port Load Average : 0.55 0.78 0.89 NAME PID SPAWN MEMORY TIME %CPU STATE UP/DOWN ns 1530 1 4636K 00:01:06.91 0.15% run 09:42:23

Step 3: Wait for the interval (60s) and go in an infinite loop

OUTPUT

1. File name : CPU_Utilization_<NodeIP>_<Date>_<Time> This file will have the format as below

Time Cpu Utilization 11.5 1.10% 11.55 1.10%

2. File name : MEMORY_Utilization_<NodeIP>_<Date>_<Time> This file will have the format as below

Time Total Memory

I've scripted the login part so far

#!/usr/bin/perl use strict; use warnings; #use Net::OpenSSH; use Net::Telnet; my $hostname = 'xx.xxx.xxx.xxx'; my $hostname = 'xxxx: my $username = 'xxxx'; my $password = 'xxxx'; my $telnet = new Net::Telnet ( Timeout=>60,Errmode=>'die'); $telnet->open($hostname); print "Telnet Successful\n"; $telnet->waitfor('/login: $/i'); $telnet->print($username); print "Username entered\n"; $telnet->waitfor('/Password: $/i'); $telnet->print($password); print "Password entered\n"; $telnet->waitfor('/\[local\]xx\xxxx\xxxx\xxxx\> $/i'); print "Enable prompt\n"; $telnet->print('enable'); $telnet->waitfor('/Password: $/i'); print "CLI prompt\n"; $telnet->print($password); $telnet->waitfor('/\[local\]xx\xxxx\xxxx\xxxx\# $/i'); my @var1 = $telnet->print(" $telnet->close; print "Telnet session closed\n";

I need help with issuing commands and storing them in the file in the above specified format. At least help me with one show command and storing it's output in a file in the above specified format which is (I'm repeating here again).

h. File name : MEMORY_Utilization_<NodeIP>_<Date>_<Time> This file will have the format as below

Time Total Memory

Comment on Need help with a grep script
Select or Download Code
Re: Need help with a grep script
by roboticus (Canon) on Nov 08, 2013 at 11:32 UTC

    seenu18:

    The Net::Telnet page shows you how to get the output of the command into an array, so all you need to do is:

    • Delete the lines in the array you don't want, and
    • Format the lines you want to keep.

    I suggest using reading perldoc perlfunc and review the string-handling functions to find ideas on how to work with strings. Similarly, perldoc perlre could give you some pointers on how to use regular expressions for that task.

    Alternatively, you could hire someone to help you with that.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Need help with a grep script
by ww (Bishop) on Nov 08, 2013 at 13:37 UTC
    Minus the sugar coating, this is NOT a code writing service. If you want to learn, take the advice offered above.

    Also, please heed the directions at the text entry box where you created the OP: <p> para tags around paragraphs </p> and code tags, <c> around your code and data </c>

Re: Need help with a grep script
by kschwab (Priest) on Nov 08, 2013 at 14:39 UTC

    I'll help since this may push you back in the direction of using ssh instead of slinging router passwords around in cleartext :)

    use Net::OpenSSH; use Net::Telnet; my $promptre = '/\w+[\$\%\#\>]\s{0,1}$/o'; my $ssh = Net::OpenSSH->new('hostname', user => 'username', password => 'password', kill_ssh_on_timeout => 1, timeout => 30); my ($fh, $pid) = $ssh->open2pty({stderr_to_stdout => 1}); my $cn = Net::Telnet->new( fhopen => $fh, prompt => $promptre, timeout => 30, errmode => 'return', telnetmode => 0, cmd_remove_mode => 1, output_record_separator => "\r", ); my @memory = $cn->cmd("show memory"); my @process = $cn->cmd("show process");

      Using SSH is better than Telnet unless the assignment specifically says to use Telnet :-)

      -- FloydATC

      Time flies when you don't know what you're doing

        Uh, okay...from the OP:
        PROCEDURE
        
         Step 1: Login to the node using SSH or Telnet.
        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1061697]
Approved by marinersk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-10-24 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls