This repository has been archived on 2020-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
Files
FreeNAS-Network-Unlock/freenas_network_unlock.py

47 lines
1.7 KiB
Python

import requests # For the api calls
import platform # For getting the operating system name
import subprocess # For executing a shell command
import config # configuration file
import logging
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig(level=logging.INFO,format='%(asctime)s - [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
#logging.getLogger().setLevel(logging.DEBUG)
#requests_log = logging.getLogger("requests.packages.urllib3")
#requests_log.setLevel(logging.DEBUG)
#requests_log.propagate = True
def ping(host):
"""
Returns True if host (str) responds to a ping request.
Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
"""
# Option for the number of packets as a function of
param = '-n' if platform.system().lower()=='windows' else '-c'
# Building the command. Ex: "ping -c 1 google.com"
command = ['ping', param, '1', host]
return subprocess.call(command) == 0
#if ping(config.HOSTNAME):
VOLUMES = requests.get(
'https://{}/api/v1.0/storage/volume/'.format(config.HOSTNAME),
auth=('root', '{}'.format(config.ROOT_PASSWORD)),
verify='{}'.format(config.CA_CERT_PATH),
)
for volume in VOLUMES.json():
if volume['is_decrypted'] == False:
logging.info('Pool {} is locked'.format(volume['name']))
response = requests.post(
'https://{}/api/v1.0/storage/volume/{}/unlock/'.format(config.HOSTNAME,volume['name']),
json={'passphrase': '{}'.format(config.POOLS[volume['name']])},
auth=('root', '{}'.format(config.ROOT_PASSWORD)),
verify='{}'.format(config.CA_CERT_PATH),
)
print(response.status_code)
print(response.text)
#else:
#print("Host,{}, is not online".format(config.HOSTNAME))