Commit 3c276634 authored by Alban Bruder's avatar Alban Bruder

Add new workstation reset command

parent 6c52154f
import click
import socket
import subprocess
import json
import fcntl
import struct
def salt(target, command):
salt_command = "salt --out=json --static --hide-timeout '{target}' {command}".format(
target=target, command=command)
process = subprocess.Popen(["sshpass", "-e", "ssh", "-tt", "", salt_command],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
return "\n".join(stdout.decode('utf8').split('\n')[1:])
def salt_cmd_run(target, command):
return salt(target, " '{}'".format(command))
def get_default_route():
process = subprocess.Popen("ip route | grep default", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
parts = stdout.decode('utf8').split(" ")
return parts[4], parts[2]
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
struct.pack('256s', ifname.encode('utf8')[:15])
def get_mac(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack(
'256s', ifname.encode('utf8')[:15]))
return ''.join(['%02x:' % char for char in info[18:24]])[:-1]
def reset_ws():
"""Commands to reset a workstation."""
@reset_ws.command(short_help='Print info')
def info():
data = dict()
data["hostname"] = socket.getfqdn()
data["address"] = socket.gethostbyname(data["hostname"])
interface, gateway = get_default_route()
data["interface"] = interface
data["gateway"] = gateway
data["address"] = get_ip_address(interface)
data["mac"] = get_mac(interface)
@reset_ws.command(short_help='Reset a workstation local')
@click.option('-f', '--force', is_flag=True, default=False, help='Always yes.')
def local(force):
click.echo("Running locally")
@reset_ws.command(short_help='Reset a workstation remotly')
@click.option('-f', '--force', is_flag=True, default=False, help='Always yes.')
def remote(target, force):
click.echo("Running on " + target)
minions = json.loads(salt_cmd_run(target, "webis reset-ws info"))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment