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.

75 lines
3.2 KiB
Python
Raw Permalink Normal View History

2018-02-02 00:21:59 +01:00
from paramiko import SSHClient
##---------------------------------------------------------------------------------------##
class SSH(object):
##---------------------------------------------------------------------------------------##
##-----------------------------------------------------------------------------------##
def __init__(self, devices):
##-----------------------------------------------------------------------------------##
2018-02-02 17:23:44 +01:00
self.devices = {}
2018-02-02 00:21:59 +01:00
for device in devices:
self.devices[device] = {}
self.devices[device]['user'] = devices[device]['user']
self.devices[device]['ip'] = devices[device]['ip']
##-----------------------------------------------------------------------------------##
2018-02-02 20:30:04 +01:00
def command(self, data):
2018-02-02 00:21:59 +01:00
##-----------------------------------------------------------------------------------##
2018-02-02 20:30:04 +01:00
for device in data:
if device in self.devices:
ip = self.devices[device]['ip']
user = self.devices[device]['user']
2018-02-03 23:02:02 +01:00
print(ip)
print(user)
2018-02-02 20:30:04 +01:00
try:
client = SSHClient()
client.load_system_host_keys()
2018-02-03 23:02:02 +01:00
client.connect(ip = "192.168.178.81", user = "root")
2018-02-02 20:30:04 +01:00
except:
print("Server nicht erreichbar")
finally:
if(data[device]=="shutdown"):
client.exec_command('poweroff')
if(data[device]=="reboot"):
client.exec_command('reboot')
if(data[device]=="upgrade"):
client.exec_command('apt update && apt upgrade -y')
2018-02-02 00:21:59 +01:00
2018-02-02 17:23:44 +01:00
##-----------------------------------------------------------------------------------##
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
2018-02-02 00:21:59 +01:00
##-----------------------------------------------------------------------------------##
def shutdown(self, device):
##-----------------------------------------------------------------------------------##
self.client.exec_command('poweroff')
##-----------------------------------------------------------------------------------##
def reboot(self, device):
##-----------------------------------------------------------------------------------##
self.client.exec_command('reboot')
##-----------------------------------------------------------------------------------##
def upgrade(self, device):
##-----------------------------------------------------------------------------------##
self.client.exec_command('apt update && apt upgrade -y')