Technical Forum
Ask questions. Discover Answers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Python script to save /sys config file

MaxMedov
Cirrus
Cirrus

Hi guys, I need help writing a dedicated script to do those things:

# tmsh save /sys config file bigip.conf no-passphrase

# netstat -nr > netroute.txt

Both of the files need to be sent to another server directory combining the device name + current date

Can anyone help, please? Thank you!

 

1 ACCEPTED SOLUTION

JRahm
Community Manager
Community Manager

Hi @MaxMedov, this works local for me, but you might need to flesh out a little functionality to meet your needs. I used bigrest for this sample:

from bigrest.bigip import BIGIP
from datetime import datetime

datestring = datetime.now().strftime('%Y-%m-%d')
hostname = 'ltm3.test.local'

br = BIGIP(hostname, 'admin', 'admin', request_token=True)

# Save Config
data = {'command': 'save'}
task = br.task_start("/mgmt/tm/task/sys/config", data)
print(f'Task {task.properties["_taskId"]} created at {datetime.now().strftime("%H:%M:%S")}')
br.task_wait(task)
if br.task_completed(task):
    br.task_result(task)
    print(f'Task {task.properties["_taskId"]} completed at {datetime.now().strftime("%H:%M:%S")}')
else:
    raise Exception()

# Copy config to /shared/images/
data = {'command': 'run', 'utilCmdArgs': f'-c "cp /config/bigip.conf /shared/images/{datestring}-{hostname}-bigip.conf"'}
br.command('/mgmt/tm/util/bash', data)

# Save netstat -rn data to /shared/images/
data = {'command': 'run', 'utilCmdArgs': f'-c "netstat -rn > /shared/images/{datestring}-{hostname}-netroute.txt"'}
br.command('/mgmt/tm/util/bash', data)

# Download files
br.download('/mgmt/cm/autodeploy/software-image-downloads/', f'{datestring}-{hostname}-netroute.txt')
br.download('/mgmt/cm/autodeploy/software-image-downloads/', f'{datestring}-{hostname}-bigip.conf')

# Remove files from /shared/images/
data = {'command': 'run', 'utilCmdArgs': f'/shared/images/{datestring}-{hostname}-netroute.txt'}
br.command('/mgmt/tm/util/unix-rm', data)
data = {'command': 'run', 'utilCmdArgs': f'/shared/images/{datestring}-{hostname}-bigip.conf'}
br.command('/mgmt/tm/util/unix-rm', data)

  And after running, I have both files downloaded for me in my local directory:

jrahm@mac scripts % ls -als | grep 2022-09-20
   968 -rw-r--r--    1 rahm  1437522721     495338 Sep 20 01:47 2022-09-20-ltm3.test.local-bigip.conf
     8 -rw-r--r--    1 rahm  1437522721        803 Sep 20 01:47 2022-09-20-ltm3.test.local-netroute.txt

View solution in original post

10 REPLIES 10

JRahm
Community Manager
Community Manager

Hi @MaxMedov, this works local for me, but you might need to flesh out a little functionality to meet your needs. I used bigrest for this sample:

from bigrest.bigip import BIGIP
from datetime import datetime

datestring = datetime.now().strftime('%Y-%m-%d')
hostname = 'ltm3.test.local'

br = BIGIP(hostname, 'admin', 'admin', request_token=True)

# Save Config
data = {'command': 'save'}
task = br.task_start("/mgmt/tm/task/sys/config", data)
print(f'Task {task.properties["_taskId"]} created at {datetime.now().strftime("%H:%M:%S")}')
br.task_wait(task)
if br.task_completed(task):
    br.task_result(task)
    print(f'Task {task.properties["_taskId"]} completed at {datetime.now().strftime("%H:%M:%S")}')
else:
    raise Exception()

# Copy config to /shared/images/
data = {'command': 'run', 'utilCmdArgs': f'-c "cp /config/bigip.conf /shared/images/{datestring}-{hostname}-bigip.conf"'}
br.command('/mgmt/tm/util/bash', data)

# Save netstat -rn data to /shared/images/
data = {'command': 'run', 'utilCmdArgs': f'-c "netstat -rn > /shared/images/{datestring}-{hostname}-netroute.txt"'}
br.command('/mgmt/tm/util/bash', data)

# Download files
br.download('/mgmt/cm/autodeploy/software-image-downloads/', f'{datestring}-{hostname}-netroute.txt')
br.download('/mgmt/cm/autodeploy/software-image-downloads/', f'{datestring}-{hostname}-bigip.conf')

# Remove files from /shared/images/
data = {'command': 'run', 'utilCmdArgs': f'/shared/images/{datestring}-{hostname}-netroute.txt'}
br.command('/mgmt/tm/util/unix-rm', data)
data = {'command': 'run', 'utilCmdArgs': f'/shared/images/{datestring}-{hostname}-bigip.conf'}
br.command('/mgmt/tm/util/unix-rm', data)

  And after running, I have both files downloaded for me in my local directory:

jrahm@mac scripts % ls -als | grep 2022-09-20
   968 -rw-r--r--    1 rahm  1437522721     495338 Sep 20 01:47 2022-09-20-ltm3.test.local-bigip.conf
     8 -rw-r--r--    1 rahm  1437522721        803 Sep 20 01:47 2022-09-20-ltm3.test.local-netroute.txt

Thank you very much! I'll try it and update 🙂

MaxMedov
Cirrus
Cirrus

Hi @JRahm, I modified the script, and this is working well, thank you!
One question, please, how do I add "no-passphrase" to the command generation of the conf file?
Thank you again

JRahm
Community Manager
Community Manager

Options can be added to the URL of the method, so in the case of creating a specific file of the config, I think it's just:

 

/mgmt/tm/task/sys/config?options=no-passphrase

 

Thank you @JRahm 
Unfortunately, it doesn't help, I received an error

JRahm
Community Manager
Community Manager

This worked for me in thunderclient in vscode:

 

POST https://ltm3.test.local/mgmt/tm/sys/config?options=file+myfile+no-passphrase

payload =  {"command": "save"}

 

Should I use the name of the file as "file+myfile" ? Like:

data = {'command': 'save'}
task = br.task_start("/mgmt/tm/sys/config?options=bigip.conf+no-passphrase", data) 

JRahm
Community Manager
Community Manager

No, my understanding is no passphrase is attached to the scf format, but verify that. The format for you in this matter should be:

?options=file+your_scf_filename+no-passphrase

and that would put your_scf_filename in /var/local/scf 

I've tried it, and this is not working for me (I got an error)
Also, I try:
{"command": "save","options":[{"file":"bigip.conf"},{"passphrase":"no-passphrase"}]} 
and
{"command":"save","options":[{"file":"/var/tmp/test.scf","no-passphrase":""}]}

It does not work too.

@JRahm Ok, I've tried again a few times, and it seems like this option was working for me:

data = {"command":"save","options":[{"file":"/config/bigip.conf","no-passphrase":""}]}
task = br.task_start("/mgmt/tm/task/sys/config", data)