Home > Enterprise >  How can I shutdown all PCs(Linux) with a python script via a Linux terminal (all with ssh enabled an
How can I shutdown all PCs(Linux) with a python script via a Linux terminal (all with ssh enabled an

Time:09-22

I would like to run a python script that shutdown all the on PCs on my network. They all are Linux machines with ssh enabled and the same username and password. I'm pretty new and can't find anything anywhere on how I'll go about doing this.

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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

  • Related