kburns1969 has asked for the wisdom of the Perl Monks concerning the following question:

I need to automate some commands on a load balancer that allow me to get a couple levels deep to the right vendor shell. This involves telling the appliance to go into bash and then to run another tmos shell. Manually it looks like this:

-bash-4.1$ ssh 10.10.10.1 Password: Last login: Tue Sep 10 22:01:12 2013 from 10.10.10.1 bp>!bash [kburns@ssl01:Active] ~ #
The module seems to hang after attempting to run the !bash command. Here is the code.
#!/usr/bin/perl use Net::OpenSSH; my $host = "10.10.10.1"; my $USERNAME = "kburns"; my $PASSWORD = "xxxx"; my $ssh = Net::OpenSSH->new($host, user => $USERNAME, password => $PAS +SWORD, strict_mode => 0); $ssh->error and die "Couldn't establish SSH connection: " . $ssh->erro +r; $ssh->system({quote_args => 0}, "!bash") or die "remote command failed +: " . $ssh->error; print "finished bash\n"; #never gets here. it hangs $ssh->system({stdin_data => \@output}, "ls") or die "remote command fa +iled: " . $ssh->error; foreach $line (@output) { print "$line\n"; }

Replies are listed 'Best First'.
Re: Net::OpenSSH Problem
by kcott (Archbishop) on Sep 11, 2013 at 00:30 UTC

    G'day kburns1969,

    I'm not really familiar with the Net::OpenSSH module, so I could be barking up the wrong tree here, but it seems to me that's sitting on the shell prompt ("[kburns@ssl01:Active] ~ #") waiting for user input (in much the same way as you show with the manual process).

    There's some other documented options that may be useful, e.g. async, stdin_fh and so on.

    -- Ken

Re: Net::OpenSSH Problem
by salva (Canon) on Sep 11, 2013 at 08:09 UTC
    system keeps waiting for the remote command (in your case !bash) to exit before returning control to your program and probably !bash is waiting for you to write some commands at the terminal.

    Read the first entry on the module FAQ for a description of the issue you are facing.

    Is there any online documentation for that device? maybe there is some way to run unix commands without going through the custom shell.

      Ok I am a bit closer. I know that !bash ran correctly in the remote session because while it was sitting there I typed the pwd and received the correct result. I guess now I am confused as to why the program is waiting on me to interact with it instead of continuing to run the other commands. Current code below

      -bash-4.1$ ./test1.pl # open_ex: ['ssh','-O','check','-T','-S','/home/kburns/.libnet-openssh +-perl/kburns-10.10.10.-20023-380016','-l','kburns','10.10.10.1','--'] # _waitpid(20025) => pid: 20025, rc: # open_ex: ['ssh','-S','/home/kburns/.libnet-openssh-perl/kburns-10.10 +.10.-20023-380016','-l','kburns','10.10.10.1','--','!bash']

      I typed pwd and it worked so I know !bash ran correctly on the remote end.

      pwd
      /home/kburns
      my $ssh = Net::OpenSSH->new($host, user => $USERNAME, password => $PAS +SWORD, str ict_mode => 0); $ssh->error and die "Couldn't establish SSH connection: " . $ssh->erro +r; $ssh->system('!bash') or die "remote command failed: " . $ssh->error; print "!bash done\n"; $ssh->system('pwd') or die "remote command failed: " . $ssh->error; print "pwd done\n"; # never gets here. it hangs
        what happens when you run the following code?
        my $ssh = Net::OpenSSH->new($host, user => $USERNAME, password => $PASSWORD, strict_mode => 0); $ssh->error and die "Couldn't establish SSH connection: " . $ssh->erro +r; $ssh->system('!pwd') or die "remote command failed: " . $ssh->error; print "pwd done\n";