"Run SSH Script" operator's sendline() vs. send()

Document ID : KB000020582
Last Modified Date : 14/02/2018
Show Technical Document Details

Description:

Using the 'Run SSH Script' operator, in some cases, when the user sends the commands using 'sendLine' in the "inline script" of the operator, the interaction with the remote machine seems to be out of sync.

For example, to run the "enable" command for a cisco router using the "Run SSH Script" operator, the user must also send a password for the command as follows:

 String wait_pass_output = "";     
 String after_pass_output = ""; 
 if(conn.waitFor(".*[$>?:#]",60)) 
 { 
 conn.sendLine("enable",true);
 conn.waitFor(".*assword:.*",60);
 wait_pass_output = conn.getLastOutput();
 conn.sendLine("cisco",true);
 conn.waitFor(".*[$>?#]",60);
 after_pass_output = conn.getLastOutput();
 }

However, the value of wait_pass_output after execution is:

Password:

 	% Access denied 

The value of after_pass_output after execution is:

 Translating "cisco"...domain server (<ip-address>)     
 (<ip-address>)
 Translating "cisco"...domain server (<ip-address>)

% Unknown command or computer name, or unable to find computer address.

Apparently the process fails with the 'enable' command before it receives "cisco" as password while the password "cisco" is accepted as a command.

Solution:

The sendLine function is nothing but a wrapper that sends the command with an additional "\n\r" at the end of the line i.e. both carriage return and new line.

Some remote machines interpret the "\n\r" as two new lines, as was the case in the example listed in the summary, where the \r was interpreted as a new line that corresponded to an empty password, hence, resulting in the 'Access denied' message.

You can solve this problem by using the 'send' command (instead of 'sendLine') and explicitly appending a carriage return at the end of your command. Here the output paramter bk_output is only to confirm the result.

String bk_output = ""; 
if(conn.waitFor(".*[$>?:#]",60))     
 { 
 conn.send("show clock\r");
 conn.waitFor(".*[$>?#]",60);
 bk_output = conn.getLastOutput();
 conn.send("enable\r");
 conn.waitFor(".*Password:.*",60);
 bk_output += conn.getLastOutput();
 conn.send("cisco\r");
 conn.waitFor(".*[$>?#]",60);
 bk_output += conn.getLastOutput();
 conn.send("config t\r");
 conn.waitFor(".*[$>?:#]",60);
 bk_output += conn.getLastOutput();
 conn.sendLine("snmp-server community keefer1 rw\r");
 conn.waitFor(".*[$>?:#]",60);
 bk_output += conn.getLastOutput();
 }