Brycen-walk
Brycen-walk

Reputation: 11

How can I shutdown all PCs(Linux) with a python script via a Linux terminal (all with ssh enabled and same username and password)

I would like to run a python script that shuts down all PCs on my network. They all are Linux machines with SSH enabled and the same username and password. How can I go about doing this?

Upvotes: 1

Views: 1596

Answers (3)

azbarcea
azbarcea

Reputation: 3657

Another option is to use Ansible (written in Python, with Python modules), with which you can manage remote servers (almost all OS-es).

more: ansible: reboot_module

Here are some examples of how the Ansible code looks like for multiple reboot scenarios (e.g. test.yml):

- name: Unconditionally reboot the machine with all defaults
  reboot:

- name: Reboot a slow machine that might have lots of updates to apply
  reboot:
    reboot_timeout: 3600

- name: Reboot a machine with shutdown command in unusual place
  reboot:
    search_paths:
     - '/lib/molly-guard'

- name: Reboot machine using a custom reboot command
  reboot:
    reboot_command: launchctl reboot userspace
    boot_time_command: uptime | cut -d ' ' -f 5

Ansible needs to be installed only on your computer (desktop/laptop/server) that will control all other nodes. There is almost no restriction on the OS-es or distros that you can control (this includes Linux, UNIX, Windows etc). The ssh connection needs to be configured (the user and password). Your code will not have to hardcode the list of nodes, username or password, it will be only a configuration.

This setup will probably be the easiest to manage multiple nodes at scale, and can provide capability add additional node management functionality.

To run ansible from Python, Ansible provides the ansible-runner Python package (PyPI, GitHub), which can be used for this.

import ansible_runner
r = ansible_runner.run(private_data_dir='/tmp/demo', playbook='test.yml')
print("{}: {}".format(r.status, r.rc))
# successful: 0
for each_host_event in r.events:
    print(each_host_event['event'])
print("Final status:")
print(r.stats)

more docs: ansible-runner: python_interface

Upvotes: 0

Pedro Fontes
Pedro Fontes

Reputation: 174

In my opinion, is better to use a bash script for that, since you can input commands through the ssh command on the machines.

for exemple, a line in bash to execute the command for shutting down a PC through ssh will be:

ssh user1@server1 "sudo shutdown -h now"

If you still want to do it in Python, Try using the subprocess Module or the os Module to execute shell commands.

Upvotes: 3

ex4
ex4

Reputation: 2428

First at all, it is much better to use public key authentication for this than store your password somewhere (https://serverpilot.io/docs/how-to-use-ssh-public-key-authentication/).

Then you simply have to call shutdown command through ssh.

import os
os.system("ssh user@host 'shutdown now'")

Obviously your user in your remote system must have privileges to shut down the computer.

Upvotes: 1

Related Questions