Sponsored Content
Top Forums Shell Programming and Scripting Perl: One action if an element doesn't exist in array Post 302487105 by ahmed_zaher on Wednesday 12th of January 2011 03:19:12 AM
Old 01-12-2011
Many thanks for your support.

It works fine with my simple example but I unfortunatly I still don't know how to make it for my actual script.

I want to run some cisco commands after checking that exact number doesn't exist in the following command output:

Code:
FWSMLAN1# sh vlan
203, 300-324 , 326, 329-331 , 333-340 , 342, 344, 346-351 , 353, 355-356 , 370, 388-390 , 399-401 , 404, 515-517 , 629, 658, 830, 834, 838, 843-844 

the commands want to be written if exact number doesn't exist:
Code:
@output = $con->cmd("show running-config interface vlan $vlan_id$");
print join("\n", @output);
my @output_line = split (/\s+/, $output[2]);
if ("$output_line[0]" eq "ERROR:")
{
 @output = $con->cmd("configure terminal");
 print join("\n", @output);
 @output = $con->cmd("interface vlan $vlan_id$");
 print join("\n", @output);
 @output = $con->cmd("no shut");
 print join("\n", @output);
 @output = $con->cmd("nameif $nameif$");
 print join("\n", @output);
 @output = $con->cmd("security-level $sec_level$");
 print join("\n", @output);
 @output = $con->cmd("ip address $IP$ $mask$ standby $standby_IP$");
 print join("\n", @output);
 @output = $con->cmd("exit");
 print join("\n", @output);
}


and here is my script:

Code:
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Opsware::NAS::Connect;
 
my($host, $port, $user, $pass) = ('localhost','$tc_proxy_telnet_port$','$tc_user_username$','$tc_user_password$');
my @output;
my $con = Opsware::NAS::Connect->new(-user => $user, -pass => $pass, -host => $host, -port => $port);
$con->login();
 
$con->connect( '192.168.15.15' ) or die "Failed to connect.";
$con->cmd("terminal length 0");
@output = $con->cmd("show vlan");
print join("\n", @output);
my @vlan = split (/,/, $output[1]);
for (my $i=0; $i <= $#vlan; $i++)
{
 if ( "$vlan[$i]" =~ /(\d+)-(\d+)/ )
 {
  print "start: $1, end: $2\n";
  for (my $j=$1; $j <= $2; $j++)
  {
   if ($j eq $vlan_id$)
   {
    print "vlan $j found\n";    
   }
  }
 }
 else
 {
  if ($vlan[$i] eq $vlan_id$)
  {
   print "vlan $vlan[$i] found\n";
  }
 }
} 
 
@output = $con->disconnect();

Appreciate your advise.
Thanks in advance,
Ahmed

---------- Post updated 01-12-11 at 03:19 AM ---------- Previous update was 01-11-11 at 11:42 AM ----------

Hello,

I think I make you confused.

in simple words,
I want to run multiple commands when "string" is member of array and then
run other multiple commands when "string" is NOT member of array.

Thanks in advance,

Appreciate your reply,
Ahmed
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

What hapens if a group member doesn't exist?

As part of a NIS implementation, (I think) I want to create a group (in /etc/group) that has users that do not exist on the target machine. What effect will this have? Will it cause any problems? Thanks, Gary Cooper (1 Reply)
Discussion started by: Gary Cooper
1 Replies

2. Shell Programming and Scripting

Perl delete an element from array

Probably I am not seeing it or I am not using the "delete" correctly I had the following codes but it does not work for me #!/bin/perl -w ... @sysFile1 = (a_b, a_c, a_d); @sysFile2 = (a_c, a_e, b_f); foreach $line1 (@sysFile1){ trim(\$line1); (my $tmp1, my $tmp2) = split/_/,... (6 Replies)
Discussion started by: ahtat99
6 Replies

3. Shell Programming and Scripting

Remove an element from an array in PERL

Hello guys, I have the following question. Consider the following code in PERL for($xeAnumber=0; $xeAnumber<@xeAnumber; $xeAnumber++) { if(@xeAnumber==@final_file) { @final_file=@xeTimeStamp; }... (3 Replies)
Discussion started by: chriss_58
3 Replies

4. Shell Programming and Scripting

sftp mget where file doesn't exist BASH

I have a script that is working: #!/bin/bash sftp user@domain.com <<EOF cd somedir mget *.csv quit EOF but on a crontab I want to only pull newer files, so I want to do something like: while read ls current dir local file != true do mget that new file but I'm not sure the syntax... (2 Replies)
Discussion started by: unclecameron
2 Replies

5. Programming

Kernel module - How to test if file doesn't exist

Hey, I'm currently getting into some kernel module progamming. As a little exercise I want to read the headers out of an ELF file. My code is very simple, here is the important part: struct file *fp; /* ... */ fp = filp_open("some/file/on/my/pc", O_RDONLY, 0); if(fp == NULL) { ... (15 Replies)
Discussion started by: disaster
15 Replies

6. Shell Programming and Scripting

previous element in the array perl

Hi, How to get previous/next element in the array perl Example @queue = (1, 2 ,3 , 4); I want to get value of 1 and 2, or, 2 and 3, or 3 and 4...etc and compare to value which one is greater to do that I need to get previous and next element of array ? (1 Reply)
Discussion started by: guidely
1 Replies

7. Solaris

User directory doesn't exist

Hii all, i create the user useradd -d /home/kk kk passwd kk when i tried to login to kk i get a error user directory doesn't exist then i tried useradd kkk passwd kkkwhen i tried to login to kkk i get the same error user directory doesn't exist. (4 Replies)
Discussion started by: vipinkumarr89
4 Replies

8. Shell Programming and Scripting

Perl: How to check whether my array contains element x

Hi All, I am new to perl I am stuck in simple problem I need your help I want to define a subroutine. sub check_if_entity_exists(@array_to_be_checked,$entityName) I have array as http-listener-1 http-listener-2 http-listener-3 http-listener-4 If i send http-listener-3 my... (1 Reply)
Discussion started by: javaholics
1 Replies

9. Shell Programming and Scripting

Perform an action if certain text exist in output (PERL)

Hello, I'm attempting to write a tool that checks an IP address for existing PTR records then if there are no PTR records does a ping to see if it response. Then if there is no response, it should print a message saying This is what I have so far. #!/usr/bin/perl $nxdomain =... (4 Replies)
Discussion started by: spartan22
4 Replies

10. Homework & Coursework Questions

Group Doesn't Exist

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I'm able to create a group but when I'm trying to delete the group it keeps stating Group Doesn't Exist. I know... (2 Replies)
Discussion started by: GoBoyGo
2 Replies
TRACE-CMD-EXTRACT(1)													      TRACE-CMD-EXTRACT(1)

NAME
trace-cmd-extract - extract out the data from the Ftrace Linux tracer. SYNOPSIS
trace-cmd extract [OPTIONS] DESCRIPTION
The trace-cmd(1) extract is usually used after trace-cmd-start(1) and trace-cmd-stop(1). It can be used after the Ftrace tracer has been started manually through the Ftrace pseudo file system. The extract command creates a trace.dat file that can be used by trace-cmd-report(1) to read from. It reads the kernel internal ring buffer to produce the trace.dat file. OPTIONS
-p plugin Although extract does not start any traces, some of the plugins require just reading the output in ASCII format. These are the latency tracers, since the latency tracers have a separate internal buffer. The plugin option is therefore only necessary for the wakeup, wakeup-rt, irqsoff, preemptoff and preemptirqsoff plugins. With out this option, the extract command will extract from the internal Ftrace buffers. -O option If a latency tracer is being extracted, and the -p option is used, then there are some Ftrace options that can change the format. This will update those options before extracting. To see the list of options see trace-cmd-list. To enable an option, write its name, to disable the option append the characters no to it. For example: noprint-parent will disable the print-parent option that prints the parent function in printing a function event. -o outputfile By default, the extract command will create a trace.dat file. This option will change where the file is written to. SEE ALSO
trace-cmd(1), trace-cmd-record(1), trace-cmd-report(1), trace-cmd-start(1), trace-cmd-stop(1), trace-cmd-reset(1), trace-cmd-split(1), trace-cmd-list(1), trace-cmd-listen(1) AUTHOR
Written by Steven Rostedt, <rostedt@goodmis.org[1]> RESOURCES
git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/trace-cmd.git COPYING
Copyright (C) 2010 Red Hat, Inc. Free use of this software is granted under the terms of the GNU Public License (GPL). NOTES
1. rostedt@goodmis.org mailto:rostedt@goodmis.org 06/11/2014 TRACE-CMD-EXTRACT(1)
All times are GMT -4. The time now is 07:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy