Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Remote killing of processes

by PhilHibbs (Hermit)
on Apr 08, 2005 at 11:47 UTC ( [id://445990]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info Phil Hibbs phil.hibbs@capgemini.com
Description: Opens a telnet session, lists processes, and prompts the user to kill each one. The code is wrapped in my version of the bat2pl framework and is saved as a .cmd file, but anyone using this in a non-MSWindows environment can delete up to the #!perl line and also the last line of the script. It also uses Win32::Console to wrap long lines in a more readable manner, but those 4 lines can be replaced with either a hard-coded value (e.g. $w=80) or the equivalent console-width-determining logic for the platform.

If your implementation of ps formats its output differently then the hard-coded value 47 may need to be adjusted for more pleasant display (hanging indent).

Also uses Term::ReadKey which is not a standard distro module.

@rem = '--*-Perl-*--
@echo off
perl "%~dpnx0" %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofperl
@rem ';
#!perl
#line 8
use strict;
use warnings;
use Term::ReadKey;
use Net::Telnet ();
use Win32::Console;

my $CON = new Win32::Console(STD_OUTPUT_HANDLE);
my @info = $CON->Info;
my $w = $info[0];

print 'Enter Username: ';
my $user = <STDIN>;
chomp $user;

print "Enter Password: ";
ReadMode ('noecho');
my $pass = <STDIN>;
chomp $pass;
ReadMode ('restore');
print "\n";

my $machine = 'corus';

my @lines;

my $t = new Net::Telnet (Timeout => 3600, Input_log  => "c:/ftp/telnet
+.log");
$t->open($machine);
$t->login($user, $pass);

@lines = $t->cmd("ps -ef | grep $user");
pop @lines;
for ( @lines ) {
  my $line = $_;
  chomp $line;
  my $prefix = substr($line,0,47);
  $line = substr($line,47);
  while ( length($line) > 0 ) {
    print $prefix.substr($line,0,$w-47);
    if ( length($line) > $w-47 ) {
      $line = substr($line,$w-47);
    } else {
      $line = '';
    }
    $prefix = ' 'x47;
  }
  print "\n";
}
for ( @lines ) {
  my $pid = $1 if /${user} ([0-9]+)/;
  print "Kill process $pid?";
  my $yn = <>;
  $t->cmd("kill $pid") if $yn =~ /^y$/i;
}

__END__
:endofperl
Replies are listed 'Best First'.
Re: Remote killing of processes
by cLive ;-) (Prior) on Apr 09, 2005 at 08:57 UTC
    Wow, people are *still* using Telnet?

    1997 called, they want their unencrypted shell emulator back (it's a joke goddammit :)

    cLive ;-)

      Hey, it's a local machine in the next room on a private LAN.
Re: Remote killing of processes
by cazz (Pilgrim) on Apr 08, 2005 at 15:45 UTC
       anyone using this in a non-MSWindows environment can delete up to the #!perl line and also the last line of the script

    Uh... You use Win32::Console. How is that portable to "non-MSWindows"?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://445990]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found