Network Change & Configuration Management

Reply
Accepted Solution

Interactive commands OR How to set a password in Junos

sGardner
Techie
Posts: 6
5770     1

In Junos, to set a password for a local account, you have to enter the command:

 

set system login user snackycakes authentication plain-text-password[Enter]

When you press [Enter] you are prompted to enter the password interactively. I have created the following script to run in NetMRI, but it hangs on the password prompt. Does anyone know how to make this work?

# BEGIN-INTERNAL-SCRIPT-BLOCK
# Script: Testing NetMRI Junos Command Push
# Script-Description: 'Testing Perl library for pushing commands to Junos.'
# Script-Devices: 
#     10.56.133.100
# END-INTERNAL-SCRIPT-BLOCK
#----------------------------------------------------------------------
# BEGIN-SCRIPT-BLOCK
#
# Script-Filter:
#	$Vendor in ["Juniper", ]
#
# Script-Variables:
#	$username word "snackycakes"
#	$password password 
#      $uid word "3002"
#
# END-SCRIPT-BLOCK

use strict;
use warnings;
use NetMRI_Easy 0.6;

our $username;
our $password;
our $uid;

my $easy = new NetMRI_Easy({ api_version => 2.5 });

if($easy->vendor eq "Juniper") {
	$easy->send_command("configure");
	$easy->send_command("set system login user $username uid $uid");
	$easy->send_command("set system login user $username authentication plain-text-password");
	$easy->send_command($password);
	$easy->send_command("set system login user $username class super-user");
	$easy->send_command("show | compare");
	$easy->send_command("rollback 0");
	$easy->send_command("exit");
}
Scott Gardner
t. @scott_abt

Re: Interactive commands OR How to set a password in Junos

Adviser
Posts: 357
5770     1

Try sending a \r or \n right after the password. 

 

	$easy->send_command($password . '\r');

 

You may have to double-backslash it, I am not sure:

 

 

	$easy->send_command($password . '\\r');

 

Re: Interactive commands OR How to set a password in Junos

sGardner
Techie
Posts: 6
5770     1

That was a good thought, but it didn't work unfortunately. I tried \r \\r \n and \\n to no avail.

Scott Gardner
t. @scott_abt
Highlighted

Re: Interactive commands OR How to set a password in Junos

Adviser
Posts: 53
5770     1

Hi Scott,

 

Provided you are running NetMRI 7.0+, the following should do the trick. In NetMRI 7.0, a new optional second argument was added to the send_command and send_async_command methods of NetMRI_Easy.pm. This argument allows you to specify a regular expression that defines what the device prompt should look like after the command is sent. So, let's assume in this case the password prompt looks like "password:". In this case you could do the following:

  

#
# Note, the second argument of "password:". This instructs the job engine to pause when this prompt is
# seen following the sending of the set command. # $easy->send_command("set system login user $username authentication plain-text-password", "password:"); # # Now send the password. Since no second argument is specified here, the job engine reverts to the
# default, wherein, it will expect to see the normal device prompt following the sending of $password. # $easy->send_command("$password");

 

Hope this helps...

 

Thanks,

- Chris

 

Re: Interactive commands OR How to set a password in Junos

sGardner
Techie
Posts: 6
5771     1

I haven't yet upgraded. I did figure out a workaround, but it's specific to Junos and may not actually be a very good solution so I won't post it. Thanks for your advice.

Scott Gardner
t. @scott_abt

Re: Interactive commands OR How to set a password in Junos

[ Edited ]
Authority
Posts: 32
5771     1

Actually, you don't want to send the \r after the password, but before it.

 

To NetMRI, a "command" is something that gets executed and results in the device returning to a recognized prompt.  In this case, the "set system login user" command results in a "password" prompt, which NetMRI does not recognize (unless you upgrade to 7.x and follow Chris's suggestion above).

 

So the idea is to add the \r and the password itself to the same send_command() statement that you use for the "set system login user" command.  Something like this:

 

$easy->send_command("set system login user $username authentication plain-text-password\r$password");

 (As per JBelamaric's comment, double-escaping may be needed, I'm not sure either.)

Re: Interactive commands OR How to set a password in Junos

Expert
Posts: 262
5771     1

Chris,

I was sure I had read some earlier post by you that described this but I couldn't find it in any back email or docs.

 

The 7.0.2 admin guide and built-in help does not show this.

An export of the NetMRI_Easy.pm library doesn't list it unless I just couldn't spot it. The lib shows a last update of 11/25/2015.

The 7.0 release notes have this hint:

NETMRI-11964 – Requested to allow CCS and Perl scripts to override current prompt regular expression on a per send_command basis.

 

The improvements are definitely helpful but without documentation, we don't learn that they exist or how to use them.  I realize that's not your responsibility but PM needs to give it some attention.

 

Lastly, is there an equivalent directive in CCS for this?  Smiley Happy

 

Re: Interactive commands OR How to set a password in Junos

Authority
Posts: 37
5771     1

Can someone confirm that 6.9.4's WAPI's send_command also does not take REGEX's? That is, thatt here is no way to pull this off in 6.9.4?  (If the WAPI does take a REGEX, then it may be possible to write a custom send_command in Perl).

Showing results for 
Search instead for 
Do you mean 

Recommended for You