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

64 lines
2.6 KiB
Python

#!/usr/bin/env python
import requests, platform, subprocess, config, logging, simplejson as json, argparse
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig(level=logging.DEBUG,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
def request(resource, method='GET', data=None):
if data is None:
data = ''
url = 'https://{}/api/v1.0/{}'.format(config.HOSTNAME, resource)
logging.debug('Request URL: {}'.format(url))
logging.debug('Request Data: {}'.format(data))
logging.debug('CA Certificate Path: {}'.format(config.CA_CERT_PATH))
r = requests.request(
method,
url,
data=json.dumps(data),
headers={'Content-Type': "application/json"},
auth=('root', '{}'.format(config.ROOT_PASSWORD)),
verify='{}'.format(config.CA_CERT_PATH)
)
logging.debug('Request Status Code: {}'.format(r.status_code))
if r.ok:
try:
logging.debug('Request Returned JSON: {}'.format(r.json()))
return [r.ok, r.status_code, r.json()]
except:
logging.debug('Request Returned Text: {}'.format(r.text))
return [r.ok, r.status_code, r.text]
raise ValueError(r)
#if ping(config.HOSTNAME):
POOLS = request('storage/volume/', 'GET')
for pool in POOLS:
if pool['is_decrypted'] == False:
logging.info('Pool {} is locked'.format(pool['name']))
response = request('storage/volume/{}/unlock/'.format(pool['name']), 'POST', {'passphrase': '{}'.format(config.POOLS[pool['name']])})
if response[0]:
logging.info('Pool {} was unlocked successfully'.format(pool['name']))
else:
logging.error('Pool {} was NOT unlocked successfully'.format(pool['name']))
else:
logging.debug('Pool {} is already unlocked'.format(pool['name']))
#else:
#print("Host,{}, is not online".format(config.HOSTNAME))