【发布时间】:2012-04-05 06:42:01
【问题描述】:
首先我想为这样一个愚蠢的问题道歉,但我对脚本很陌生。
我正在尝试做的事情: 在我的办公室,我正在尝试自动化恢复出厂重置开关的功能。其为开发商提供的测试设备不是生产设备仅供参考。我正在尝试将交换机的 telnet 提示符中的文件列表转储到一个数组中,然后过滤它们以保留我们的图像并删除其余的。
开关的常量和变量:本节的开关提示为=>,文件有扩展名和无扩展名,文件夹输出为foo\
我的问题: sub 没有正确发出 rm 命令,它在测试(打印值)中有效,但在实际删除脚本中无效。
新更新:
我知道我似乎放弃了这个问题,但我没有!上个月我一直在琢磨这个问题,并且取得了很大的成功。我已经能够将我的数据放入我的初始数组(是的,我有 2 个,稍后会详细介绍)。它将处理数组并删除我想要保留的内容。
#!/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;
这一切都非常好,而且这也适用于文件夹,主要是......如果文件夹为空或只有一个项目,它适用于文件夹。当文件夹中有 2 个或更多项目时,它不会发出正确删除该文件夹中文件的命令。
任何想法将不胜感激。谢谢! 谢谢!
【问题讨论】: