Hello;

While this script working, in case of not to connect a node ("BADYK01","BAGRK01", "BAHLK01", "BAHLK02", "BAHLK03") , it stops.

I want it to be continue even if it doesnt connect a node. I mean if it couldnt connect to BAGRK01, I want it to be continue and connect the other node BAHLK01.

Thanks

# $interface = "1.0"

# This PerlScript example iterates through an array of three session names
# connecting to each one in turn. The unix 'df' command is 
# sent to each server and the output is captured to a logfile.
TEST1:

# Enable errors
#
use Win32::OLE;
Win32::OLE->Option(Warn => 3);

#unlink <*.txt>;
$crt->Session->Disconnect();
# An array of session names to connect to.

@sessions = ("BADYK01","BAGRK01", "BAHLK01", "BAHLK02", "BAHLK03");

# define some useful constants
#
$true = 1;
$false = 0;
$StartLog = $true;
$StopLog = $false;
$Append = $true;
$Overwrite = $false;
$Raw = $true;
$Not_raw = $false;

# NOTE: Set your logfile path here
#
#$LogFile = "LOG.txt";
#$LogFile = "$LOG.log";

#
$crt->Screen->{'Synchronous'} = $true;

# Loop thru the array of sessions
#
for ($i = 0; $i < 5; $i++) {

    # Connect to each session using the "/s sessionname" argument.
    #
    $crt->Session->Connect("/s " . $sessions[$i]);

    # Wait for 5 seconds, or until the login prompt appears.
    #
    $crt->Screen->WaitForString("MAIN LEVEL COMMAND <___>");

    # Set the name of the logfile for this session.
    #
    $crt->Session->{'LogFileName'} = LOG .  ".txt";

    # Enable logging
    #

    $crt->Session->Log($StartLog,$Append);
    # Send the 'df' command followed by a CR (octal 015)
    #
    $crt->Screen->Send("EOL:;\015");
    $crt->Screen->WaitForString("BASE TRANSCEIVER STATION ALARMS HANDLING COMMAND <EO_>");

    $crt->Session->Log($StopLog);

    $crt->Session->Disconnect();
}

$crt->Screen->{'Synchronous'} = $false;

Dani AI

Generated

Short summary: the script is stopping because a connection or the following wait is raising an error or hanging. correctly flagged the off‑by‑one/index problem and suggested enabling strict/warnings; pointed to a safer loop form. The immediate fixes are: iterate the actual session list (not a hardcoded 62), enable strict/warnings, and add per‑host error handling so a failed host is skipped instead of aborting the whole run.

A robust pattern is: loop over the host list with foreach, wrap the connect+wait+command sequence in an eval so exceptions are caught, and skip to the next host on failure. If the API you call supports a timeout on the “wait” call, pass a timeout value so it cannot block forever. Always log the host name and the error so you can diagnose recurring failures.

Example pattern to adapt (replace the stub with your existing connect/wait/send/disconnect calls):

use strict;
use warnings;

my @hosts = qw(host1 host2 host3);   # replace with your session names

foreach my $host (@hosts) {
    my $ok = eval {
        # Put your per-host connect + wait + commands here.
        # Die on error so eval can catch it.
        do_session_work($host);
        1;    # success
    };

    unless ($ok) {
        chomp(my $err = $@ || "unknown error");
        warn "Host $host: skipped - $err\n";
        # optional cleanup here (disconnect, close logfile)
        next;
    }

    # success path: do post-command logging/processing
}

sub do_session_work {
    my ($host) = @_;
    # REPLACE this stub with your actual session calls.
    # If the underlying API supports a wait timeout, use it here.
    die "not implemented";
}

Troubleshooting notes: if exceptions are coming from the OLE/com object, capture and log the $@; enable OLE warnings and the COM last‑error diagnostics when available to get more detail. If an operation can hang and the API does not support a timeout, run that host work in a separate short‑lived process and kill it if it exceeds your timeout.

Recommended Answers

All 4 Replies

For one thing the array starts a index 0 (zero) so your looping threw one that doesn't exist. Also you need to use the modules strict and warnings
to catch more problems...

#its really 4
#change the for ($i=0; $i <= 4; $i++){ }

for my $each (@sessions){
    #..change $sessions[$i] to $each ...
    #this will eliminate any other errors
}

#add at the top of the script
use strict;
use warnings;

Or, write the for loop like so:

for my $i(0..$#sessions){
  ...

  ... ( $sessions[$i])

  ...
}

Hello

I added what you said but i couldnt achieve it. Could you pls add me however you want and send me back pls..

Thanks

#
for ($i = 0; $i < 62; $i++) {

    # Connect to each session using the "/s sessionname" argument.
    #
    $crt->Session->Connect("/s " . $sessions[$i]);

    # Wait for 5 seconds, or until the login prompt appears.

where is the rest of the code? what module is $crt hooked to?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.