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
Christ Follower, Husband, Father, Technologist. I love community and I especially love THIS community. My background is networking, but I've dabbled in all the F5 iStuff, I'm a recovering Perl guy, and am very much a python enthusiast. Learning alongside all of you in this accelerating industry toward modern apps and architectures.JRahm
Admin
Christ Follower, Husband, Father, Technologist. I love community and I especially love THIS community. My background is networking, but I've dabbled in all the F5 iStuff, I'm a recovering Perl guy, and am very much a python enthusiast. Learning alongside all of you in this accelerating industry toward modern apps and architectures.16 Comments
- Adam_Burnett_18
Nimbostratus
Tried this on 11.5, no luck. Does it require 12.0? - JRahm
Admin
I tested on 12.0, It's possible 11.6 might work, but I don't think the building blocks were in place. - Maverick_80689
Nimbostratus
silly question i know but what is the location of these files on the device? cant find them after uploading. - JRahm
Admin
@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_80689
Nimbostratus
Thanks 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. - JRahm
Admin
i 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_80689
Nimbostratus
Thanks Jason, please ignore my previous comment. It works with version 11.5.1, just needed to adjust permissions on the destination folder. - JRahm
Admin
Just got confirmation, this functionality was added for 12.0, so it will not work in previous versions. - kman_52500
Nimbostratus
Along 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. - JRahm
Admin
ok, so change that "will not work" to "is not supported" before 12.0 :) Nice work guys.