First I want to apologize for such a silly question but I'm quite new to scripting.
What I'm attempting to do: At my office, I'm attempting to automate the ability to factory reset switches. Its testing equipment for developers not production equipment fyi. I'm trying to dump a list of files from the telnet prompt of the switch into an array and then filter through them to keep our images and delete the rest.
The constants and variables of the switch: the switch prompt for this section is =>, the files both do and don't have extensions, folders are outputted as foo\\
My question: The sub isn't properly issuing the rm command, it works in a test (printing the value) but not in the actual deletion script.
New Update:
I know it looks like I abandoned this question, but I didn't! I've been monkeying around with this problem for the last month and I've achieved a major bit of success. I've been able to get my data into my initial array (yes I have 2, more on that in a bit). It will process the array and delete the contents except those I want to keep.
#!/usr/bin/perl
$| = 1;
use Net::Telnet;
use strict;
use CGI;
use warnings;
my $junk;
my $telnet;
my @array;
#establish connection
$telnet = new Net::Telnet (Timeout=> 10,
Errmode=> 'return',
Port => '2037');
$telnet->open('trn-13-mrv-con-07');
$telnet->put(chr(13));
#clear out buffered output for array to grab
$junk = $telnet->cmd(string => 'ls', prompt => '/=>/');
#array grabbing
@array = $telnet->cmd(string => 'ls', prompt => '/=>/');
#determining what the values are of the array
foreach my $item (@array) {
#removing the carriage return
chomp ($item);
#if it's a folder, this will shunt the to the sub
if ($item=~ /\//) {
subfolders ($item);
$telnet -> print("rm $item");
}
#checks to see if the file is the firmware for the switch
elsif ($item =~ /btm.swi|secondary.swi/) {
}
#deletes the rest
else {
$telnet -> print ("rm $item");
$telnet->put(chr(13));
}
}
sub subfolders {
my @subfolder_contents;
my $subfolder = $_[0];
my ($file, $sub_junk);
#attempting to clear out the array
undef @subfolder_contents;
#again grabing any cached output so array can do it's work
$sub_junk = $telnet ->cmd(string => "ls $subfolder", prompt => '/=>/');
#grabbing files from the folder
@subfolder_contents = $telnet ->cmd(string => "ls $subfolder", prompt => '/=>/');
#deleting all files in the subfolder
foreach $file (@subfolder_contents){
chomp ($file);
$telnet-> print ("rm $subfolder$file");
$telnet->put(chr(13));
}
};
exit;
That all works wonderfully, AND this will work for the folders as well, mostly... It works on the folders if the folder is empty or has just one item in it. When it has 2 or more items in the folder it doesn't issue the command to delete the files in that folder correctly.
Any thoughts would be appreciated. thanks! Thanks!
Answer edited due to discussion below
The problem is indeed the $t->put(chr(13));
Sending a \\r
causes the switch to respond with another =>
prompt which is now in the output. When you issue cmd
, the prompt
causes that first line of output to stop the reading.
empty_buffer
won't work because there's nothing in the input buffer until you call cmd()
; it isn't a multithreaded application. cmd()
is doing a write, then reading. That's the first read from the socket that is performed. If you were using getline
instead at that point you'd have the first line, and the input buffer would contain the rest (or at least most of it depending on how the socket read is done in the library)
If instead of using cmd
you wrote the command manually using print
or put
, then used a loop and getline()
, you could ignore the first =>
if you absolutely can't eliminate sending that \\r
You likely want to stop capturing the echoed input. Look at cmd_remove_mode in the Net::Telnet docs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.