This repository has been archived on 2023-06-10. You can view files and clone it, but cannot push or open issues or pull requests.
2018-02-02 17:23:44 +01:00

69 lines
2.8 KiB
Python

from paramiko import SSHClient
##---------------------------------------------------------------------------------------##
class SSH(object):
##---------------------------------------------------------------------------------------##
##-----------------------------------------------------------------------------------##
def __init__(self, devices):
##-----------------------------------------------------------------------------------##
self.devices = {}
for device in devices:
self.devices[device] = {}
self.devices[device]['user'] = devices[device]['user']
self.devices[device]['ip'] = devices[device]['ip']
##-----------------------------------------------------------------------------------##
def command(self, device, command):
##-----------------------------------------------------------------------------------##
self.client = SSHClient()
self.client.load_system_host_keys()
self.client.connect(self.devices[device]['ip'], self.devices[device]['user'])
if(command=="shutdown"):
self.shutdown(device)
if(command=="reboot"):
self.reboot(device)
if(command=="upgrade"):
self.upgrade(device)
##-----------------------------------------------------------------------------------##
def config(self):
##-----------------------------------------------------------------------------------##
config = {
"device" : {
"name" : "",
"ip" : "",
"user" : ""
},
"shutdown" : {
"device" : "name",
"state" : "button"
},
"reboot" : {
"device" : "name",
"state" : "button"
},
"upgrade" : {
"device" : "name",
"state" : "button"
}
}
return config
##-----------------------------------------------------------------------------------##
def shutdown(self, device):
##-----------------------------------------------------------------------------------##
self.connect()
self.client.exec_command('poweroff')
##-----------------------------------------------------------------------------------##
def reboot(self, device):
##-----------------------------------------------------------------------------------##
self.connect()
self.client.exec_command('reboot')
##-----------------------------------------------------------------------------------##
def upgrade(self, device):
##-----------------------------------------------------------------------------------##
self.connect()
self.client.exec_command('apt update && apt upgrade -y')