perl expect script to login to multiple machines

2010-01-03 Thread perl_haxor 123
Hi All,

   I'm new to perl and facing a problem I'm using a perl
expect script to login to multiple machines (having same username and
password) and execute the date command, initially i tested on one machine,
it worked fine...but when i use a foreach loop to test the same
thing on two machines (the code is shown below), it didn't work...it
just worked on the first machine (10.10.10.1) can someone please
help me out with this problem?..how can i use the perl expect script to
login to the multiple machines (assuming they have same username and
password) and execute commands?.. Kindly help me with this problem



#
#!/usr/bin/perl -w
use strict;
use Expect;


foreach ("10.10.10.1", "10.10.10.2") {
print "Spawning to the $_\n";
$exp->spawn("ssh -l username $_") or warn "unable to spawn: $!";



#This part checks for the password prompt, if it doesn't get the password
prompt the errors are displayed.
unless($exp->expect(10, '-re','.*password:\s*')){

if( $exp->before()) {
print $exp->before();
print "\n";
}
else {

print "Timed out\n";
}
next;
}


print "Sending password to the host\n";

$exp->send("password\r");
print "\n";


#This code will handle any errors after the password is sent

unless($exp->expect(10, '-re', '.*[#$>]')) {



if($exp->before() =~ /(Permission.*\s*.*)/) {
print "\n";
print "Invalid password\n";
}
else {

print "timed out\n";

}
exit;

}




print "sending date command\n";

$exp->send("date\r");
$exp->expect(15, '-re', '.*[#$>]' );
$exp->soft_close();

}
#


Re: perl expect script to login to multiple machines

2010-01-05 Thread perl_haxor 123
Hi all,

   I got the solution for this problem..the reason is that
the new expect object had to be created for each machine that is

$exp = new Expect has to be inside the foreach loop, in my earlier code it
was outside the foreach loop.Thanks you all for the solutions..i
tried using Net::SSH2 and Net::SSH::Perl and it didn't work for me so i had
to use expect.



Thanks

On Tue, Jan 5, 2010 at 8:30 PM, Salvador Fandino  wrote:

> perl_haxor 123 wrote:
>
>> Hi All,
>>
>>   I'm new to perl and facing a problem I'm using a perl
>> expect script to login to multiple machines (having same username and
>> password) and execute the date command, initially i tested on one machine,
>> it worked fine...but when i use a foreach loop to test the same
>> thing on two machines (the code is shown below), it didn't work...it
>> just worked on the first machine (10.10.10.1) can someone please
>> help me out with this problem?..how can i use the perl expect script
>> to
>> login to the multiple machines (assuming they have same username and
>> password) and execute commands?.. Kindly help me with this problem
>>
>
> There are several CPAN modules (Net::OpenSSH, Net::SSH2, Net::SSH::Perl,
> Net::SSH::Expect) that allow to run commands in remote machines through SSH.
> Any one of them will release you of the burden of handling authentication
> and capturing the output of the remote command.
>
>
> For instance:
>
>  use Net::OpenSSH;
>
>  for my $host (@hosts) {
>my $ssh = Net::OpenSSH->new($host, user => $user, passwd => $passwd);
>if ($ssh->error) {
>  warn "unable to connect to $host: ". $ssh->error;
>  next;
>}
>my $date = $ssh->capture('date');
>print "$host: $date";
>  }
>
> - Salva
>


stripped output from Net::SSH2

2010-03-10 Thread perl_haxor 123
Hi All,

  I'm using Net::SSH2 module on windows machine, the purpose of this
script is to login to multiple devices and execute "show run" command and
print the output of the command to a text file,  ...the script login to
multiple devices and executes "show run" command", reads from the channel
buffer and dumps the output to a text file.but the problem is the text
file doesn't contain the full output, the last few lines are stripped
out..Does anybody know what the problem could be?


Thanks


Re: stripped output from Net::SSH2

2010-03-10 Thread perl_haxor 123
find the script below:

#!/usr/bin/perl -w
use strict;

use Term::ReadKey;
use Net::SSH2;

use constant BUFLEN => 10_;


my $user = "user";

print "Enter the the password: ";
ReadMode 'noecho';
my $pass = ReadLine 0;
print "\n";
chomp $pass;
ReadMode 'normal';

open INPUT, "<", "input.txt";
open STDERR, ">", "Error.txt";


while() {


chomp $_;

my $ssh2 = Net::SSH2->new();

unless ($ssh2->connect("$_")) {

print "unable to connect to Host: $_\n";
print STDERR "Unable to connect to $_: $!\n";
print STDERR
"#\n\n";
next;

}

print "connecting to $_\n";


unless ($ssh2->auth_password("$user","$pass")) {


print "Invalid Password\n";
exit;
}

my $chan = $ssh2->channel;
$chan->exec('sh run');

my $buf;
my $read = $chan->read($buf, BUFLEN);
warn 'More than ', BUFLEN, ' characters in listing' if $read >= BUFLEN;
open OUTPUT, ">", "$_.txt";
print OUTPUT "HOST: $_\n\n";
print OUTPUT "$buf\n";
print OUTPUT "\n\n\n";
print OUTPUT
"##\n\n";
close OUTPUT;
$chan->close();
}



Thanks





On Thu, Mar 11, 2010 at 12:58 AM, Sisyphus wrote:

>
> - Original Message - From: "perl_haxor 123" 
> To: 
> Sent: Thursday, March 11, 2010 6:07 AM
> Subject: stripped output from Net::SSH2
>
>
>
>  Hi All,
>>
>> I'm using Net::SSH2 module on windows machine, the purpose of this
>> script is to login to multiple devices and execute "show run" command and
>> print the output of the command to a text file,  ...the script login
>> to
>> multiple devices and executes "show run" command", reads from the channel
>> buffer and dumps the output to a text file.but the problem is the text
>> file doesn't contain the full output, the last few lines are stripped
>> out..Does anybody know what the problem could be?
>>
>>
> Could you provide a small (minimal) script that demonstrates the problem ?
> (Remember to remove password and any other sensitive information before
> posting.)
>
> Cheers,
> Rob
>


Re: stripped output from Net::SSH2

2010-03-11 Thread perl_haxor 123
Hi Rob,


 Thanks a lot for looking into thisi can't execute cat command on
the routing devices, I was trying "sh run" on two devices, the show run
command dumps the current configuration of the routing device..i tried
executing "sh run" on two devices, one of the device has less configuration
and other one has more configuration.when i ran this script on the
device with less configuration, i got the full output, but when i run the
same script on device with more configuration, i got the stripped
output.it looks like it is getting timed out..is there a way
to increase the timeout? (i tried setting $ssh->poll(0), but it didn't
help)..an example would be really helpful.


Thanks,




On Thu, Mar 11, 2010 at 4:31 PM, Sisyphus  wrote:

>
> - Original Message - From: "perl_haxor 123" 
> To: "Sisyphus" 
> Cc: 
> Sent: Thursday, March 11, 2010 6:38 AM
> Subject: Re: stripped output from Net::SSH2
>
>
>
>  find the script below:
>>
>> #!/usr/bin/perl -w
>> use strict;
>>
>> use Term::ReadKey;
>> use Net::SSH2;
>>
>> use constant BUFLEN => 10_;
>>
>>
>> my $user = "user";
>>
>> print "Enter the the password: ";
>> ReadMode 'noecho';
>> my $pass = ReadLine 0;
>> print "\n";
>> chomp $pass;
>> ReadMode 'normal';
>>
>> open INPUT, "<", "input.txt";
>> open STDERR, ">", "Error.txt";
>>
>>
>> while() {
>>
>>
>>   chomp $_;
>>
>> my $ssh2 = Net::SSH2->new();
>>
>> unless ($ssh2->connect("$_")) {
>>
>> print "unable to connect to Host: $_\n";
>> print STDERR "Unable to connect to $_: $!\n";
>> print STDERR
>>
>> "#\n\n";
>> next;
>>
>> }
>>
>> print "connecting to $_\n";
>>
>>
>> unless ($ssh2->auth_password("$user","$pass")) {
>>
>>
>>   print "Invalid Password\n";
>>   exit;
>> }
>>
>> my $chan = $ssh2->channel;
>> $chan->exec('sh run');
>>
>> my $buf;
>> my $read = $chan->read($buf, BUFLEN);
>> warn 'More than ', BUFLEN, ' characters in listing' if $read >= BUFLEN;
>> open OUTPUT, ">", "$_.txt";
>> print OUTPUT "HOST: $_\n\n";
>> print OUTPUT "$buf\n";
>> print OUTPUT "\n\n\n";
>> print OUTPUT
>>
>> "##\n\n";
>> close OUTPUT;
>> $chan->close();
>> }
>>
>
>
> I can't reproduce the problem.
>
> However, I have access to only one SSH2 server, so my "input.txt" simply
> contained 3 identical lines - namely "192.168.0.3" (which is the IP address
> of the machine running the server).
>
> Does your problem go away if *you* connect to the same server at each
> iteration ?
>
> For me "sh run" just produces a single line of output - namely "run: run:
> No such file or directory". Not very useful, so I changed the command to
> "cat pscrpt/hello.pl" which prints out a multiline file. If you change
> your command to "cat some_multiline_file", does your problem go away ?
>
> Finally because I was connecting to the same server each time, I had to
> modify the script to create separate files for each iteration of the while
> loop. First iteration writes to 192.168.0.30.txt, second iteration to
> 192.168.0.31.txt and the third iteration to 192.168.0.32.txt.
>
> For each of those 3 iterations, pscrpt/hello.pl is correctly reproduced.
> In order to get the same error as you, do I need to:
> a) cat a bigger file ? (hello.pl is only about 15 lines)
> b) run some command other than cat ?
> c) iterate through the loop more than 3 times ?
> d) connect to different servers each time ?
>
> Do you get the error for *every* server you connect to ?
> If it's just *some* servers that are giving the problem, is it the *same*
> servers each time ? And do each of those "same" servers produce the error
> *every* time you run the script ?
>
> Finally, which version of perl are you running ?
> I have version 5.10.1, and am using Net-SSH2-0.28 (built against
> libssh2-1.2.2).
>
> There's a ppm for that version of Net-SSH2-0.28 (also built against
> libs

parsing a log file with junk characters

2010-07-02 Thread perl_haxor 123
Hi All,

I have been asked to parse the log file (find the attached file)
which logs every keystroke, if you look at the log file you will see lot of
characters like RETURN, BACKSCAPE etc etcIs there a way by which i
can make sense of this log file, so that i can write a script to parse
it.It would be great if someone can help me solve this problem.


Thanks


log_file1
Description: Binary data
-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Re: parsing a log file with junk characters

2010-07-04 Thread perl_haxor 123
Hi,

Thanks a lot for the solution.i'l give it  a try.


Thanks,
Monnappa


On Fri, Jul 2, 2010 at 6:29 PM, Chas. Owens  wrote:

> On Fri, Jul 2, 2010 at 07:59, perl_haxor 123  wrote:
> > Hi All,
> >
> > I have been asked to parse the log file (find the attached
> file)
> > which logs every keystroke, if you look at the log file you will see lot
> of
> > characters like RETURN, BACKSCAPE etc etcIs there a way by which
> i
> > can make sense of this log file, so that i can write a script to parse
> > it.It would be great if someone can help me solve this problem.
> snip
>
> I would say you can get 80% of the way there by
>
>  * treating each line as if it where a separate command
>  * treating the backspace (^H) and delete (^?) characters as backspaces
>  * ignoring (ESC)
>
> but there will certainly be problems with this solution.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> while (<>) {
>s/\(ESC\)//g; #remove escapes
>my @visible;
>for my $char (split //) {
>if (ord $char == 8 or ord $char == 127) {
>pop @visible;
>next;
>}
>push @visible, $char;
>}
>print @visible;
> }
>
> --
> Chas. Owens
> wonkden.net
> The most important skill a programmer can have is the ability to read.
>


how to remove (^M ^G ) characters

2010-10-28 Thread perl_haxor 123
Hi All,

   I'm a beginner in perl and i try to read a file, but these files
contains characters like (^M ^G)..I wanted to know what are these
characters and how can remove them using perl?.and also please let me if
there is any link form where i can find what these characters are and their
ascii values?..any sugges would be really helpful






Thanks


checking if a file is in use

2010-11-02 Thread perl_haxor 123
Hi All,

  I have a directory in which i have multiple files, i have to read
each one of them and parse the data..but there could be some files which
are in use by some other program, how can i find which files are in use by
other program in perl?...any suggestions would be really helpful.



Thanks,
Monnappa


Re: Re: checking if a file is in use

2010-11-02 Thread perl_haxor 123
Hi All,

 Thanks a lot for the suggestion,  It looks like flock is used if my
perl script want to lock a file, In this case the perl script would be
copying files from one directory to an another one, but some process (let
say xyz process) might be using these files, in that case i don't want to
copy the file used by another process (xyz process)...please let me know
how can i find out if  file is locked by the process (xyz)?..can flock
be used for thatand also the system is linux system.


Thanks,
Monnappa

On Wed, Nov 3, 2010 at 1:31 AM, shawn wilson  wrote:

> -- Forwarded message --
> From: "shawn wilson" 
> Date: Nov 2, 2010 3:12 PM
> Subject: Re: checking if a file is in use
> To: "perl_haxor 123" 
>
> perlmonks has a nice article on file locks in general that might be useful
> to you. i assume what you want to do is lock the file or die.
> http://www.perlmonks.org/?node_id=7058
>
>
> On Tue, Nov 2, 2010 at 2:49 PM, perl_haxor 123 
> wrote:
>
> > Hi All,
> >
> >  I have a directory in which i have multiple files, i have to
> read
> > each one of them and parse the data..but there could be some files
> > which
> > are in use by some other program, how can i find which files are in use
> by
> > other program in perl?...any suggestions would be really helpful.
> >
> >
> >
> > Thanks,
> > Monnappa
> >
>