Junos Space Developer
Junos Space Developer

How to script a configlet to reboot switches

‎06-27-2019 07:48 AM

I need to reboot several EX switches during the night.

 

Can someone share with me how the configlet would look.

I have tried several times to make this work, and just cannot.

 

Thanks

JNCIA-Junos
5 REPLIES 5
Junos Space Developer

Re: How to script a configlet to reboot switches

‎06-27-2019 08:05 AM

You can use Python to power off a Junos device from a Windows PC.

1. Install Python (Refer to PYEZ cookbook, link at the bootom)

2. Install Junos PYEZ library on Windows PC. (Refer to PYEZ cookbook, link at the bootom)

3. Open PowerShell and run the following commands, you can use the same in a Python script as well

4. Ensure netconf over ssh is enabled on  EX device

> show configuration system services netconf 
ssh;

>> Script Code exec in Python interpreter

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\jnet> python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import jnpr.junos
>>> from jnpr.junos import Device
>>> from jnpr.junos.exception import ConnectError
>>> srx=Device(host='<IP address of host>',user='root',password='password') << non-root should work if it has permissions.
>>> from jnpr.junos.utils.sw import SW
>>> srx.open()
Device(<IP>)
>>> sw=SW(srx)
>>> print(sw.poweroff())
Shutdown NOW!
[pid 98168]
>>> print(sw.poweroff())

 

>>Verification

Stopping cron.
.
Feb 24 20:24:58 jlaunchd: database-replication (PID 90347) terminate signal 15 sent
Feb 24 20:24:59 jlaunchd: ethernet-connectivity-fault-management (PID 90348) terminate signal 15 sent
Feb 24 20:24:59 jlaunchd: stats-agent (PID 90349) terminate signal 15 sent
Feb 24 20:25:00 jlaunchd: transport-control (PID 90350) terminate signal 15 sent
Feb 24 20:25:00 jlaunchd: SDN-Telemetry (PID 90353) terminate signal 15 sent
Feb 24 20:25:00 jlaunchd: routing (PID 90354) terminate signal 15 sent
Feb 24 20:25:01 jlaunchd: firewall (PID 90355) terminate signal 15 sent
Feb 24 20:25:01 jlaunchd: chassis-control (PID 90356) terminate signal 15 sent
Feb 24 20:25:01 jlaunchd: alarm-management (PID 90358) terminate signal 15 sent
Feb 24 20:25:05 jlaunchd: craft-control (PID 18017) exited with status=0 Normal Exit
Waiting (max 60 seconds) for system process `vnlru' to stop...done
Waiting (max 60 seconds) for system process `bufdaemon' to stop...done
Waiting (max 60 seconds) for system process `syncer' to stop...
Syncing disks, vnodes remaining...0 0 0 0 done
All buffers synced.
Uptime: 49d5h6m19s
besw0: 4 Broadcom SDK kernel threads killed
Khelp module "jsocket" can't unload until its refcount drops from 5 to 0.

The operating system has halted.
Turning the system power off.

>> Running as Script

--> Shutdown.py

import jnpr.junos
from jnpr.junos import Device
from jnpr.junos.exception import ConnectError
ex=Device(host='<IP address of host>',user='root',password='password')
from jnpr.junos.utils.sw import SW
srx.open()
sw=SW(ex)
print(sw.poweroff())

>> Reference

https://www.juniper.net/us/en/training/jnbooks/day-one/automation-series/junos-pyez-cookbook/

https://junos-pyez.readthedocs.io/en/2.1.7/_modules/jnpr/junos/utils/sw.html

https://www.juniper.net/documentation/en_US/junos-pyez/topics/task/program/junos-pyez-device-rebooti...

 

============================

Please mark my solution as Accepted if it works for you.

Kudos are always apprecaited Smiley Happy

Junos Space Developer

Re: How to script a configlet to reboot switches

‎06-27-2019 08:08 AM

Was looking for something actually on the SPACE system to use .

 

But if nothing is provided, will dig into your solution and test it.

 

Thanks

JNCIA-Junos
Junos Space Developer

Re: How to script a configlet to reboot switches

‎06-27-2019 04:23 PM
Which version of Junos Space are you using? Also a few additional questions, how many devices would you expect to reboot? All at the same time, or scheduled to reboot at some defined time? Would you need to be able to cancel a scheduled reboot etc.

Thanks
Junos Space Developer

Re: How to script a configlet to reboot switches

‎06-28-2019 02:17 AM

Hi,

From Network Director, you can reboot the switches immediately.

Hope this helps.

https://www.juniper.net/documentation/en_US/junos-space-apps/network-director3.2/topics/task/operati...

-PL

Junos Space Developer

Re: How to script a configlet to reboot switches

‎06-28-2019 02:40 AM

If you are running a suitable version of Junos Space (I'm guessing 14.x or higher), then this functionality is available within the standard Network Management Platform.  No need to install anything else, unless the default functionality isn't sufficient.

 

In Space 19.1 for example you can navigate to the Device Management view, select the devices that you wish to reboot, and then select Actions, Device Operations, Reboot devices.   This gives you some limited options to schedule the task for a specific time, include a message, or alternatively power off the device(s).

 

This functionality might be sufficient for your needs, however if it isn't for example with a multi RE device you want to reboot a specific routing engine rather than both RE's, you can't do that with the built-in functionality of Junos Space.  However, you can implement the functionality in Junos Space using SLAX scripts.    Note that I say SLAX scripts and not CLI Configlets, because CLI Configlets can only push/modify/delete configuration on devices, they are not able to make RPC calls to a device or Junos Space for that matter to go and execute some set of tasks.   Whereas SLAX scripts can perform exactly that process.

 

In the past I've written SLAX scripts to include reboot and halt functionality for dual RE devices, so if you need any pointers on that I can share those scripts, but for basic reboot functionality Junos Space should be able to provide this functionality out of the box with a suitable version of the product.

 

Regards