Forum Discussion
Steve_Brown_882
Feb 16, 2011Historic F5 Account
pyControl download_file
Has anyone used the config sync download file functionality in python. I have gotten the command to work as follows, but I am not really sure what I need to do to get the resulting data into a file on...
L4L7_53191
Mar 07, 2011Nimbostratus
Sigh. It's amazing what a little time away can do...I had an 'off-by' bug in my code above, and it's working just fine now. In the event that it's useful to someone, here's what happened. If you look at my while loop, I was terminating the loop (by setting poll=False), but I wasn't *actually flushing the last chunk of data to the file*. This caused the problem, and this is why my md5sum output was different every time I changed the chunk size. Essentially, I was discarding the data so I was always missing that last chunk. Here's an improved version- that is, a working one 🙂
!/bin/env python
import base64
import binascii
import io
def pc_downloader(b,remote_file,local_file):
'''
Handles downloading files via System.ConfigSync
Args:
b -- a pycontrol client object
remote_file -- the name of the file to fetch from BigIP
local_file -- the local file to write to.
'''
stream_io = io.open(local_file,'wb')
dl = b.System.ConfigSync
poll = True
chunk_size = 64*1024
foffset = 0 initial file offset to increment.
lines = []
while poll:
print "Foffset is: ", foffset
res = dl.download_file(remote_file,chunk_size = chunk_size,file_offset = foffset)
foffset = long(res.file_offset)
fdata = getattr(res,'return').file_data
chain_type = getattr(res, 'return').chain_type
print "Chain type is: ", chain_type
if (chain_type == 'FILE_LAST') or (chain_type == 'FILE_FIRST_AND_LAST'):
poll = False
Now write out the last bit of data.
lines.append(binascii.a2b_base64(fdata))
else:
lines.append(binascii.a2b_base64(fdata))
stream_io.writelines(lines)
if __name__ == '__main__':
import sys
import pycontrol.pycontrol as pc
host = sys.argv[1]
f = sys.argv[2]
b = pc.BIGIP(hostname=host,username='admin',password='admin',fromurl=True,wsdls=['System.ConfigSync'])
pc_downloader(b,'/var/tmp/'+f,'/var/tmp/'+f)
By the way, if I recall the io module requires 2.6. If you're running something lower, just use normal file io...
-Matt
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects