Created
November 14, 2015 04:26
-
-
Save jasonrahm/40b03b93a5ed9f1fdc8f to your computer and use it in GitHub Desktop.
Upload files to F5 BIG-IP with the iControl REST API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _upload(host, creds, fp): | |
chunk_size = 512 * 1024 | |
headers = { | |
'Content-Type': 'application/octet-stream' | |
} | |
fileobj = open(fp, 'rb') | |
filename = os.path.basename(fp) | |
if os.path.splitext(filename)[-1] == '.iso': | |
uri = 'https://%s/mgmt/cm/autodeploy/software-image-uploads/%s' % (host, filename) | |
else: | |
uri = 'https://%s/mgmt/shared/file-transfer/uploads/%s' % (host, filename) | |
requests.packages.urllib3.disable_warnings() | |
size = os.path.getsize(fp) | |
start = 0 | |
while True: | |
file_slice = fileobj.read(chunk_size) | |
if not file_slice: | |
break | |
current_bytes = len(file_slice) | |
if current_bytes < chunk_size: | |
end = size | |
else: | |
end = start + current_bytes | |
content_range = "%s-%s/%s" % (start, end - 1, size) | |
headers['Content-Range'] = content_range | |
requests.post(uri, | |
auth=creds, | |
data=file_slice, | |
headers=headers, | |
verify=False) | |
start += current_bytes | |
if __name__ == "__main__": | |
import os, requests, argparse, getpass | |
parser = argparse.ArgumentParser(description='Upload File to BIG-IP') | |
parser.add_argument("host", help='BIG-IP IP or Hostname', ) | |
parser.add_argument("username", help='BIG-IP Username') | |
parser.add_argument("filepath", help='Source Filename with Absolute Path') | |
args = vars(parser.parse_args()) | |
hostname = args['host'] | |
username = args['username'] | |
filepath = args['filepath'] | |
print "%s, enter your password: " % args['username'], | |
password = getpass.getpass() | |
_upload(hostname, (username, password), filepath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jasonrahm: is there a way we can pass auth token instead of (username, password) in _upload(hostname, (username, password), filepath) function
it would be looking like _upload(hostname, auth-token, filepath)