log_file output

M

Mike Rogers

Hello Folks,

I am new to perl and I have written a script that ssh's to a server and
then runs a command. I am trying to have the script send the output of
the command to a log file but am running into problems.

I have added the whole script to the bottom of the post, at the moment it
works fine and logs the output of "uname -a\r" to the log file dump.dat
but its pretty messy, for example the contents look like:-

^M
^M^[[3g^[[24;9H^[H^[[24;17H^[H^[[24;25H^[H^[[24;33H^[H^[[24;41H^[H^
[[24;49H^[H^[[24;57H^[H^[[24;65H^[H^[[24;73H^[H^M^Mtest-server[root]#
uname -a^M
IPSO test-server 3.7.1-BUILD013 releng 1279 07.21.2004-225500 i386^M
test-server[root]# ^M

Is there a clean way to send the output of "uname -a" to dump.dat without
using:

$command->log_file("dump.dat");
$command->log_file(undef);

Something like:

print $command "uname -a\r" >> dump.dat;

I know the above won't work but is there something like this so it just
appends dump.dat with:

test-server[root]# uname -a
IPSO test-server 3.7.1-BUILD013 releng 1279 07.21.2004-225500 i386

Many Thanks in advance,

Mike.

#!/usr/local/bin/perl

use Expect;

$fwlist= "fwlist";

open (FWLIST, $fwlist) or die ("Cannot open $fwlist - $!");

while ($fwname = <FWLIST>) {

print ("$fwname\n");

$ip = `getmip $fwname`;

$command = Expect->spawn("s0 ssh1 $ip")
or die "Couldn't start program: $!\n";

# prevent the program's output from being shown on the screen
$command->log_stdout(0);

# wait 60 seconds for "[vt100]" to appear
unless ($command->expect(60, '[vt100]')) {
# timed out
}

# send a carriage return to the program
print $command "\r";

$command->log_file("dump.dat");

# wait 10 seconds for "[root]#" to appear
unless ($command->expect(10, '[root]#')) {
# timed out
}

# send a "uname -a" and a carriage return to the program
print $command "uname -a\r";

# wait 10 seconds for "[root]#" to appear
unless ($command->expect(10, '[root]#')) {
# timed out
}


$command->log_file(undef);


# send "exit" and a carriage return to the program
print $command "exit\r";

print ("\n\n\n");

print ("Completed\n\n");

}


# close (FWLIST);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top