cancel
Showing results for 
Search instead for 
Did you mean: 
Login & Join the DevCentral Connects Group to watch the Recorded LiveStream (May 12) on Basic iControl Security - show notes included.
JRahm
Community Manager
Community Manager

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.0
Comments
Adam_Burnett_18
Nimbostratus
Nimbostratus
Tried this on 11.5, no luck. Does it require 12.0?
JRahm
Community Manager
Community Manager
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
Nimbostratus
silly question i know but what is the location of these files on the device? cant find them after uploading.
JRahm
Community Manager
Community Manager
@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
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
Community Manager
Community Manager
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
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
Community Manager
Community Manager
Just got confirmation, this functionality was added for 12.0, so it will not work in previous versions.
kman_52500
Nimbostratus
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
Community Manager
Community Manager
ok, so change that "will not work" to "is not supported" before 12.0 🙂 Nice work guys.
Tony_D__Dodd
Nimbostratus
Nimbostratus

I don't suppose anyone has a powershell example of this? I am trying to create something to upload and also overwrite ifiles programatically.

 

JRahm
Community Manager
Community Manager

powershell is not my strong suit but I can probably hack one together for you. I'll be at F5 Agility this week though, so probably can't get to it until next Monday.

 

HariV_272940
Nimbostratus
Nimbostratus

groovy equivalent of it ?

 

adammdit_276749
Nimbostratus
Nimbostratus

Does anybody can provide curl command? I'm trying this: curl -sk -u admin:'*****' -H "Content-Type: application/octet‐stream" -d @my_file.xml -X POST

 

In response I'm receiving: {"code":400,"message":"Content-Range",...

 

But file size is just 74kB

 

adammdit_276749
Nimbostratus
Nimbostratus

Just found answer to my question. I'm using 11.6 and it's working fine:

 

curl -i -sk -u admin:'*****' -X POST -H "Expect:" -H "Content-Type: application/octet-stream" -H "Content-Range: 0-(SIZE-1)/SIZE" --data-binary "@my_file.xml"

 

File is located in: /ts/var/rest

 

Next step rewrite it to use LWP 🙂

 

vipy_300071
Nimbostratus
Nimbostratus

Does anyone have import certificate to F5 cert store using c.

 

Version history
Last update:
‎05-Nov-2015 07:51
Updated by:
Contributors