Use Junos PyEZ to Halt, Reboot, or Shut Down Junos Devices
Perform a System Halt, Reboot, or Shut Down
SUMMARY Use Junos PyEZ to halt, reboot, or shut down Junos devices.
The Junos PyEZ
jnpr.junos.utils.sw.SW
utility provides methods that enable you to perform the following
operations on Junos devices:
An immediate system halt, reboot, or shutdown
A halt, reboot, or shutdown operation with an optional delay
A halt, reboot, or shutdown operation scheduled at a specific date and time
Table 1 outlines the available methods. By default, the methods immediately execute the requested operation on all Routing Engines or nodes in the setup. You can include additional arguments to execute the operation at a later time as well as specify the affected Routing Engines or nodes.
Method |
Description |
---|---|
Gracefully shut down the Junos OS software but maintain system power |
|
Gracefully shut down the Junos OS software and power off the Routing Engines |
|
Reboot the Junos OS software |
Starting in Junos
PyEZ Release 2.1.0, the reboot()
and poweroff()
methods perform the requested operation
on all Routing Engines in a dual Routing Engine or Virtual Chassis
setup. In earlier releases, the methods perform
the operation only on the Routing Engine to which the application
is connected.
If a Junos PyEZ application reboots a device from a NETCONF-over-SSH
session or from a Telnet session through the management interface,
the application loses connectivity to the device when it reboots.
If the application requires access to the device after the reboot,
it must issue the Junos PyEZ open()
method
to restore connectivity.
The following Junos PyEZ application establishes a NETCONF session over SSH with a Junos device and reboots all Routing Engines, effective immediately.
#Python 3 from jnpr.junos import Device from jnpr.junos.utils.sw import SW from jnpr.junos.exception import ConnectError from getpass import getpass hostname = input("Device hostname: ") username = input("Device username: ") password = getpass("Device password: ") try: with Device(host=hostname, user=username, passwd=password) as dev: sw = SW(dev) print(sw.reboot()) except ConnectError as err: print (err)
The application prompts for the device hostname and user credentials. After requesting the system reboot, the application displays the reboot message and the process ID for the process on the connected Routing Engine.
user1@server:~$ python3 junos-pyez-reboot.py Device hostname: dc1a.example.com Device username: user1 Device password: Shutdown NOW! [pid 2358]
The following Junos PyEZ example shuts down all Routing Engines on the device, effective immediately.
#Python 3 from jnpr.junos import Device from jnpr.junos.utils.sw import SW from jnpr.junos.exception import ConnectError from getpass import getpass hostname = input("Device hostname: ") username = input("Device username: ") password = getpass("Device password: ") try: with Device(host=hostname, user=username, passwd=password) as dev: sw = SW(dev) print(sw.poweroff()) except ConnectError as err: print (err)
How to Halt, Reboot, or Shut Down the System with a Delay or at a Specified Time
By default, the halt()
, reboot()
, and poweroff()
methods immediately execute the requested operation. You can also
delay the operation or schedule it at a particular date and time.
To delay the operation by a specified number of minutes,
set the optional in_min
parameter to the
amount of time in minutes that the system should wait before executing
the operation. The following example requests a reboot of all Routing
Engines in 2 minutes:
from jnpr.junos import Device from jnpr.junos.utils.sw import SW with Device(host='dc1a.example.com') as dev: sw = SW(dev) sw.reboot(in_min=2)
The target device issues messages about the impending reboot to any users logged into the system. After the specified amount of time has passed, the system reboots.
*** System shutdown message from user1@dc1a *** System going down in 2 minutes
To schedule the operation at a specific time, include the at
parameter, which takes a string that can be specified
in one of the following ways:
now
—Immediately initiate the halt, reboot, or shut down of the software.+minutes
—Number of minutes from now when the requested operation is initiated.yymmddhhmm
—Absolute time at which to perform the operation, specified as year, month, day, hour, and minute.hh:mm
—Absolute time on the current day at which to perform the operation, specified in 24-hour time.
The following example schedules a system reboot of all Routing Engines at 22:30 on the current day:
from jnpr.junos import Device from jnpr.junos.utils.sw import SW with Device(host='dc1a.example.com') as dev: sw = SW(dev) sw.reboot(at='22:30')
Similarly, the following example schedules all Routing Engines to power off at 22:30 on the current day:
from jnpr.junos import Device from jnpr.junos.utils.sw import SW with Device(host='dc1a.example.com') as dev: sw = SW(dev) sw.poweroff(at='22:30')
How to Specify the Target Routing Engines or Nodes
By default, the halt()
, reboot()
, and poweroff()
methods perform the requested operation on all Routing Engines in
a dual Routing Engine or Virtual Chassis setup and on all nodes on
devices running Junos OS Evolved. You can also perform the operation
on a specific Routing Engine or node.
To specify the target Routing Engines, you use the all_re
and other_re
parameters. Table 2 summarizes
the all_re
and other_re
values that are required to execute the requested operation on specific
Routing Engines.
Affected Routing Engines |
|
|
---|---|---|
All Routing Engines (default) |
Omit or set to |
– |
Only the connected Routing Engine |
Set to |
– |
All Routing Engines except the Routing Engine to which the application is connected |
– |
Set to |
To explicitly indicate that the operation should be performed
on all Routing Engines in a dual Routing Engine or Virtual Chassis
setup, include the all_re=True
argument,
which is the default.
from jnpr.junos import Device from jnpr.junos.utils.sw import SW with Device(host='dc1a.example.com') as dev: sw = SW(dev) sw.reboot(all_re=True)
To perform the requested operation on only the Routing Engine
to which the application is connected, include the all_re=False
argument.
from jnpr.junos import Device from jnpr.junos.utils.sw import SW with Device(host='dc1a.example.com') as dev: sw = SW(dev) sw.reboot(all_re=False)
To perform the requested operation on all Routing Engines in
the system except for the Routing Engine to which the application
is connected, include the other_re=True
argument.
from jnpr.junos import Device from jnpr.junos.utils.sw import SW with Device(host='dc1a.example.com') as dev: sw = SW(dev) sw.reboot(other_re=True)
To reboot or shut down a specific node of a device running Junos
OS Evolved, include the on_node
argument,
and specify the node. For example:
from jnpr.junos import Device from jnpr.junos.utils.sw import SW with Device(host='router1.example.com') as dev: sw = SW(dev) sw.reboot(on_node='re0')
How to Reboot a VM Host
On devices that have Routing Engines with VM host support, Junos OS runs as a virtual machine
(VM) over a Linux-based host (VM host). The Junos PyEZ reboot()
method supports the vmhost
argument, which enables you to
reboot a VM Host. When you include the vmhost=True
argument,
the system reboots the host OS and compatible Junos OS on all Routing Engines by
executing the <request-vmhost-reboot>
RPC, which
corresponds to the request vmhost reboot
operational mode
command.
The following example reboots the Routing Engines on the VM Host, which reboots both the guest Junos OS and the host OS.
from jnpr.junos import Device from jnpr.junos.utils.sw import SW with Device(host='switch1.example.net') as dev: sw = SW(dev) sw.reboot(vmhost=True)
reboot()
and poweroff()
methods perform the requested operation
on all Routing Engines in a dual Routing Engine or Virtual Chassis
setup.