File Upload via iControl REST
Code :
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)
Tested this on version:
12.0Published Nov 05, 2015
Version 1.0JRahm
Admin
Joined January 20, 2005
JRahm
Admin
Joined January 20, 2005
- Adam_Burnett_18NimbostratusTried this on 11.5, no luck. Does it require 12.0?
- JRahmAdminI tested on 12.0, It's possible 11.6 might work, but I don't think the building blocks were in place.
- Maverick_80689Nimbostratussilly question i know but what is the location of these files on the device? cant find them after uploading.
- JRahmAdmin@Maverick, /shared/images for images, /var/config/rest/downloads for others. The writeup for this has details: https://devcentral.f5.com/s/articles/demystifying-icontrol-rest-part-5-transferring-files
- Maverick_80689NimbostratusThanks Jason, that is very useful. But it looks it didnt upload my files although python doesn't throw any errors. And I tried it on both version 11.5 and 11.6, do not have a version 12.0 device.
- JRahmAdmini thought this was supported before 12.0 but perhaps not. I'll download an 11.6 image next week and re-test. (hotel wifi today/tomorrow, doesn't sound promising)
- Maverick_80689NimbostratusThanks Jason, please ignore my previous comment. It works with version 11.5.1, just needed to adjust permissions on the destination folder.
- JRahmAdminJust got confirmation, this functionality was added for 12.0, so it will not work in previous versions.
- kman_52500NimbostratusAlong the lines of what Maverick said, I was also able to get this to work in 11.5.2, although instead of modifying permissions I had to create the directory: "/var/config/rest/downloads/tmp" with no special permissions. I was getting the following error back that lead me to investigate the creation of that directory. code: 400, reason: /var/config/rest/downloads/tmp/rest_upload_test.txt (No such file or directory) It seems like if a hot fix would simply create that directory this would work out of the box in the 11.5 or at least the >= 11.5.2 line.
- JRahmAdminok, so change that "will not work" to "is not supported" before 12.0 :) Nice work guys.