Perl Foreach add "4" to the end of each element


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Foreach add "4" to the end of each element
# 1  
Old 01-02-2011
Perl Foreach add "4" to the end of each element

I wrote the following script configuring cisco router:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Opsware::NAS::Connect;
my @IP = split(/\./,"$tc_device_ip$");
my $O3 = $IP[2];
my($host, $port, $user, $pass) = ('localhost','$tc_proxy_telnet_port$','$tc_user_username$','$tc_user_password$');
my @output;
my $down_ip;
my $access_ip;
my $line;
my $line_output;
my $con = Opsware::NAS::Connect->new(-user => $user, -pass => $pass, -host => $host, -port => $port);
if ("$down_ip$" =~ /10\.14.*/)
{
 $access_ip = "172.18.$O3.1";
}
elsif ("$down_ip$" =~ /172\.18.*/)
{
 $access_ip = "10.14.$O3.1";
}
$con->login();
$con->connect( $access_ip ) or die "Failed to connect.";
$con->cmd("terminal length 0");
@output = $con->cmd("sh ip eigrp neighbors");
print join("\n", @output),"\n";
my $count = $#output;
print "\n$count\n";
if ("$count" == 4) 
{
 @output = $con->cmd("sh run | in distribute");
 print join("\n", @output),"\n";
 $con->cmd("conf t");
 print "conf t"; 
 foreach $line (@output)
 {
  print "\n$line";
  $line_output = $con->cmd("no $line");
  print "$line_output";
 }
 @output = $con->cmd("exit");
 print ("\n", @output);
}
@output = $con->disconnect();
$con->logout();
undef $con;
exit(0);

but foreach adds a number "4" at end of each element as follows:

Code:
sh ip eigrp neighbors
IP-EIGRP neighbors for process 1
H   Address                 Interface       Hold Uptime   SRTT   RTO  Q  Seq
                                            (sec)         (ms)       Cnt Num
0   10.214.199.1            Fa0/1             13 5d01h      30   450  0  21530009
4
sh run | in distribute
distribute-list 55 out FastEthernet0/1
distribute-list 55 in FastEthernet0/1
distribute-list 44 out Serial0/2/0
distribute-list 44 in Serial0/2/0
conf t
sh run | in distribute4
distribute-list 55 out FastEthernet0/14
distribute-list 55 in FastEthernet0/14
distribute-list 44 out Serial0/2/04
distribute-list 44 in Serial0/2/04
exit

Please advise.
# 2  
Old 01-02-2011
The "4" is coming from:
Quote:
Originally Posted by ahmed_zaher
<snip>
$line_output = $con->cmd("no $line");
print "$line_output";
<snip>
Change the print to:
Code:
   print "<($line_output)>";

to confirm. You should see:
Code:
<snip>
sh run | in distribute<(4)>
distribute-list 55 out FastEthernet0/1<(4)>
distribute-list 55 in FastEthernet0/1<(4)>
distribute-list 44 out Serial0/2/0<(4)>
distribute-list 44 in Serial0/2/0<(4)>
exit

Or you can run perl in debug mode and walk through each line.

I noticed that every other case, the return value of $con->cmd() is assigned to a list, not a scalar.
Does $line_output need to be a list?
This User Gave Thanks to m.d.ludwig For This Post:
# 3  
Old 01-02-2011
Hello,

I would like to thank you very much for your support.

it needs a list not a scalar as you menetion.

But do you know , why it considers "sh run | in distribute" an element of the array??
I want only the output of the command to be the array elements, and not the command itself.

I appeciate your support in this point.

Thanks again.

regards,
Ahmed
# 4  
Old 01-02-2011
Quote:
Originally Posted by ahmed_zaher
<SNIP>
But do you know , why it considers "sh run | in distribute" an element of the array??
I want only the output of the command to be the array elements, and not the command itself.
<SNIP>
To be honest, not a clue :-). But if you want to ignore this element, you can do:
Code:
(undef, @output) = $con->cmd(...);

or
Code:
@output = $con->cmd(...);
shift @output;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

write a matrix element, if the "if" condition satisfies

Hi All, I would like to check a condition in a nxm matrix and if the condition satisfies then, I need to write the matrix element in the output file. For example, I have a matrix like this: 1 2 3 4 5 2 3 4 5 1 3 4 5 1 ... (8 Replies)
Discussion started by: Fredrick
8 Replies

4. Shell Programming and Scripting

Removing "^M" from the end of a String (i.e. "Ctrl+M")?

Hello All, I have an Expect script that ssh's to a remote server and runs some commands before exiting. One of the commands I run is the "hostname" Command. After I run this command I save the output using this line in the code below... Basically it executes the hostname command, then I... (2 Replies)
Discussion started by: mrm5102
2 Replies

5. Shell Programming and Scripting

Purpose of "read" and "$END$" in ksh ?

Hi, Could anyone please shed some light on the following script lines and what is it doing as it was written by an ex-administrator? cat $AMS/version|read a b verno d DBVer=$(/usr/bin/printf "%7s" $verno) I checked that the cat $AMS/version command returns following output: ... (10 Replies)
Discussion started by: dbadmin100
10 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

Error to "find" a matching array element in a directory

Hi, I have defined an array which holds a couple of elements which are nothing but files names. I want to find the files in a directory for the matching file name(array elements) with less than 1 day old. When I am trying to execute the code (as below), it gives an error. Your help in this... (1 Reply)
Discussion started by: mkbaral
1 Replies

8. UNIX for Advanced & Expert Users

add seconds to: date"|"time"|"HHMMSS

Hey all, I have a shell that invokes a AWK. In this AWK i want invoke a function that receives 3 parameters: date: 20080831 time: 235901 duration: 00023 that function receive this 3 parameters and sum to this value two more seconds: 2008083123590100025 Remember that in case that... (3 Replies)
Discussion started by: anaconga
3 Replies

9. Shell Programming and Scripting

acessing awk array element while getline < "file"

I am attempting to write a awk script that reads in a file after awk array elements are assigned and using those elements while reading in the new file. Does this make sense? /pattern/ {tst=$3} (( getline < "file" ) > 0 ) { x=x " "tst } When I print tst in the END statement it... (9 Replies)
Discussion started by: timj123
9 Replies

10. Shell Programming and Scripting

foreach loop using a "*" for args

have a script that copies a file to a bunch of hosts. works great with a single file. How do I make it copy all files in a source directory? or a *txt pattern, or anything using a * ??? name of script: cp2all #!/bin/csh foreach host ( host1 host2 host3 host4 host5 host6 host7) rcp $argv... (1 Reply)
Discussion started by: ajp7701
1 Replies
Login or Register to Ask a Question