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

Fellow Monks, I come again in search of enlightenment...

We have a *nix server with a shared login (call it abc). Recently, code was put into the .profile for abc (in /home/abc) that required users provide a unique name so that history files could be separated. This was also done to root.

In the meantime, I have been building a script to automate some work being done... and the addition of this code killed my script. I am now seeing intermittent timeouts during the login process. I went in tonight and commented that code in .profile, and all worked well (no more timeouts at all). Here's what the login process looks like now (new lines between "No mail..." and "Term Type...")...

login as: abc abc@1.2.6.8's password: Last login: Thu Jan 16 13:46:28 UTC 2014 from 1.2.3.8 on pts/0 WARNING! This computer system and network is PRIVATE and ... No mail for abc Please enter your unique ID: IJAF History will be kept in /home/abc/.history-IJAF Term type is now xterm hostname:/home/abc->

I am using Control::CLI to manage my interactions with the server. My current code looks like this:

$cli = new Control::CLI( Use => 'SSH', Timeout => 30, ); $cli->connect( Host => $host, Username => 'abc', Password => 'pass', ); $port = $cli->port; if ( defined($port) ) { print "\tSSH connection to server ($host) established!\n"; } else { print "\tSSH connection to server ($host) could not be established +... Exiting program.\n"; exit; }

Documentated examples of Control::CLI on the net are virtually non-existent (at least, in my searches). Do any of you Monks have any knowledge of how I can alter my code to deal with the additional prompt? Or, even any suggestions?

Thanks in advance!

Replies are listed 'Best First'.
Re: Control::CLI and Additional Prompt At Login
by robby_dobby (Hermit) on Jan 17, 2014 at 07:02 UTC

    Hello,

    all worked well (no more timeouts at all)
    Since you mention timeouts, I'm assuming that you were able to connect to your host using $cli->connect(). Now, this is just a wild guess on my part: Have you tried using the $cli->login() after your connect? Here's the source to help with login sub.

    The code will look something like this:
    if ( defined($port) ) { print "\tSSH connection to server ($host) established!\n"; $cli->login(...); # or $cli->put(string => "IJAF\n"), if it's wait +ing for user input - whichever works. You can also use $cli->print(li +ne => "IJAF"); } else { print "\tSSH connection to server ($host) could not be established +... Exiting program.\n"; exit; }


    HTH.

Re: Control::CLI and Additional Prompt At Login
by hdb (Monsignor) on Jan 17, 2014 at 07:22 UTC

    According to the documentation of Control::CLI, $cli->connect(...) returns an error code and $cli->errormsg() an error message. Did you check those?

Re: Control::CLI and Additional Prompt At Login
by oiskuu (Hermit) on Jan 17, 2014 at 17:04 UTC

    User and root accounts are freely shared on this system? Mhm.

    On a *nix system, setting up multiple logins for a single account is usually a trivial matter. Just saying.

Re: Control::CLI and Additional Prompt At Login
by ImJustAFriend (Scribe) on Jan 19, 2014 at 22:37 UTC

    I figured out quite an easy fix for this a while after posting. Numerous rounds of testing have proven it out. I asked for the extra prompt to be changed from "ID:" to "ID>". This made my code think the login was all good. From there, I added code (through trial and error) to handle the extra prompt after the code block in my original message, like so:

    $port = $cli->port; if ( defined($port) ) { print "\tSSH connection to server ($host) established!\n"; } else { print "\tSSH connection to server ($host) could not be established +... Exiting program.\n"; exit; } $output = $cli->cmd(""); $output = $cli->cmd("IJAF"); $output = $cli->cmd("clear"); $output = "";

    Thanks as usual, Monks!!

      Sorry I'm late, but better late than never!..

      The connect() method in Control::CLI only handles the connection, it does not try to lock onto a prompt; only login() and cmd() do that..

      So I think the original problem was that you were sending your first command while the *nix host was expecting the unique ID.

      So I would suggest a cleaner approach would be the following:

      $cli = new Control::CLI( Use => 'SSH', Timeout => 30, ); $cli->connect( Host => $host, Username => 'abc', Password => 'pass', ) or do { print "\tSSH connection to server ($host) could not b +e established\n"; exit; } $cli->login( Username => 'IJAF', Username_prompt => 'unique ID[:>]\s*$', ) or do { print "\tCould not get prompt from server ($host)\n"; exit; } print "\tSSH connection to server ($host) established!\n"; $output = $cli->cmd("clear"); ...

      The above login() method will now supply IJAF if it sees a prompt for the unique ID; once it sees a valid prompt (whether or not it got requested for the unique ID) it will return with a true value and you can then proceed to send the commands you need with cmd()..