# iControlREST and Curl to save and download ASM policies

**URL:** <https://community.f5.com/t/icontrolrest-and-curl-to-save-and-download-asm-policies/13584>\
**Category:** Technical Forums\
**Tags:** asm-advanced-waf, icontrol, icontrolrest, security, download, export, curl\
**Created:** [December 6, 2016, 5:01am UTC](https://community.f5.com/t/icontrolrest-and-curl-to-save-and-download-asm-policies/13584 "2016-12-06T05:01:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![saidshow\_251381](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@saidshow\_251381](https://community.f5.com/u/saidshow_251381)\
**Post date:** [December 6, 2016, 5:01am UTC](https://community.f5.com/t/icontrolrest-and-curl-to-save-and-download-asm-policies/13584/1 "2016-12-06T05:01:52Z")

</div>

Hi,

I want to be able to save/export asm policies on the F5 and then download. I want to do this using iControlREST and curl.

I am able to save UCS files with the post shown below:

```markup
curl -v -sk -u admin:admin https://myF5/mgmt/tm/sys/ucs -H 'Content-Type: application/json' -X POST -d '{"command":"save","name":"blah.ucs"}' | jq

```

However if I try to do something similar for asm I get errors. Below is what I was trying with asm.

```markup
curl -v -sk -u admin:admin https://myF5/mgmt/tm/asm/policies/fn9GoMrandomGvoN2dD -H 'Content-Type: application/json' -X POST -d '{"command":"save","name":"as_test.xml"}' | jq

```

The error I get is: { “code”: 400, “message”: “Could not parse/validate the Policy ‘Security Policy /Common/as\_test’. Unknown field ‘command’”, “originalRequestBody”: “{\“command\”:\“save\”,\“name\”:\“as\_test.xml\””, “referer”: “x.x.x.x”, “restOperationId”: 59083, “kind”: “:resterrorresponse” }

Thank you

---

<div class="post-metadata">

**Author:** ![Hannes\_Rapp](https://avatars.discourse-cdn.com/v4/letter/h/779978/32.png) [@Hannes\_Rapp](https://community.f5.com/u/Hannes_Rapp)\
**Post date:** [December 6, 2016, 11:32am UTC](https://community.f5.com/t/icontrolrest-and-curl-to-save-and-download-asm-policies/13584/2 "2016-12-06T11:32:49Z")

</div>

```markup
1: Extract policy names and corresponding API IDs
  hannes@ubuntuBash$ curl -ku 'admin:admin' -X GET https://172.16.99.91/mgmt/tm/asm/policies | jq '.items[] | "pol_name:" + .name + ";api_id:" + .id'
  "pol_name:sp_fake.google.com;api_id:00TT7Lv-Yba2xcEZkjkc3A"
  "pol_name:sp_fake.github.com;api_id:cswJ2oCNNqbfdWMVWdzoTw"

2: Export policies natively on the appliance
  hannes@ubuntuBash$ curl -ku 'admin:admin' -X POST https://172.16.99.91/mgmt/tm/asm/tasks/export-policy -H 'Content-Type: application/json' -d '{"filename":"sp_fake.google.com.xml","policyReference":{"link":"https://localhost/mgmt/tm/asm/policies/00TT7Lv-Yba2xcEZkjkc3A"}}'
  hannes@ubuntuBash$ curl -ku 'admin:admin' -X POST https://172.16.99.91/mgmt/tm/asm/tasks/export-policy -H 'Content-Type: application/json' -d '{"filename":"sp_fake.github.com.xml","policyReference":{"link":"https://localhost/mgmt/tm/asm/policies/cswJ2oCNNqbfdWMVWdzoTw"}}'

3: Exported policies can be found on BigIP at '/var/ts/var/rest'
  [hannes@bip-01:Active:In-Sync] config ls /var/ts/var/rest
  admin~sp_fake.github.com.xml admin~sp_fake.google.com.xml

4: Download the exports: Issue a cURL to that policy, save output as a local file in your external file system.
  hannes@ubuntuBash$ curl -ku 'admin:admin' -X GET https://172.16.99.91/mgmt/tm/asm/file-transfer/downloads/sp_fake.google.com.xml >> sp_fake.google.com.xml
  hannes@ubuntuBash$ curl -ku 'admin:admin' -X GET https://172.16.99.91/mgmt/tm/asm/file-transfer/downloads/sp_fake.github.com.xml >> sp_fake.github.com.xml
  hannes@ubuntuBash$ ls
  sp_fake.google.com.xml sp_fake.github.com.xml

```

Voila! Note that this was done on 12.1.1. I think all of the above will work on 11.6.x too. On 11.5.x the 4th step will fail (not implemented error).

---

<div class="post-metadata">

**Author:** ![saidshow\_251381](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@saidshow\_251381](https://community.f5.com/u/saidshow_251381)\
**Post date:** [December 6, 2016, 11:45pm UTC](https://community.f5.com/t/icontrolrest-and-curl-to-save-and-download-asm-policies/13584/3 "2016-12-06T23:45:17Z")

</div>

Hannes Thank you! This is far more detailed than I expected. Thank you for your efforts. I have already upgraded to Ver12.1.1 so this will just perfect. I’ll give this a try as soon as time allows. This really is appreciated. Well Done.

---

<div class="post-metadata">

**Author:** ![saidshow\_251381](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@saidshow\_251381](https://community.f5.com/u/saidshow_251381)\
**Post date:** [March 6, 2017, 3:12am UTC](https://community.f5.com/t/icontrolrest-and-curl-to-save-and-download-asm-policies/13584/4 "2017-03-06T03:12:31Z")

</div>

Hi Hannes,

Great advice again. I have taken what you provided and scripted it up. Just replace username and password and the IPs and off it goes. This script will create a folder with the current AEST date and time and place all the xml backups in that folder.

Since I create 3 files in the script, I also remove them at the end. If I don’t remove them they will grow every time the script is executed.

I hope this helps someone else half as much as you helped me.

```markup
!/bin/bash

curl -ku 'username:password' -X GET https://x.x.x.x/mgmt/tm/asm/policies | jq '.items[] | "pol_name:" + .name + ";api_id:" + .id' >> asmDetailsLAB.txt
cat asmDetailsLAB.txt |grep pol_name |cut -d":" -f2 |cut -d";" -f1 >> asmPoliciesLAB.txt
cat asmDetailsLAB.txt |grep pol_name |cut -d":" -f3 |cut -d'"' -f1 >> asmIDsLAB.txt

folderName="$(zdump AEST)"
mkdir -p asmLabBackup
mkdir "asmLabBackup/""$folderName"

paste -d'\n' asmPoliciesLAB.txt asmIDsLAB.txt | while read asmPolicy && read asmIDs;do
echo $asmPolicy $asmIDs
curl -ku 'username:password' -X POST https://x.x.x.x/mgmt/tm/asm/tasks/export-policy -H 'Content-Type: application/json' -d '{"filename":"'$asmPolicy'","policyReference":{"link":"https://localhost/mgmt/tm/asm/policies/'$asmIDs'"}}'
curl -ku 'username:password' -X GET https://x.x.x.x/mgmt/tm/asm/file-transfer/downloads/$asmPolicy >> asmLabBackup/"$folderName"/$asmPolicy.xml
done

rm asmDetailsLAB.txt
rm asmPoliciesLAB.txt
rm asmIDsLAB.txt

```

---

<div class="post-metadata">

**Author:** ![Wojciech\_Wypior](https://avatars.discourse-cdn.com/v4/letter/w/59ef9b/32.png) [@Wojciech\_Wypior](https://community.f5.com/u/Wojciech_Wypior)\
**Post date:** [March 30, 2017, 12:32pm UTC](https://community.f5.com/t/icontrolrest-and-curl-to-save-and-download-asm-policies/13584/5 "2017-03-30T12:32:21Z")

</div>

Hello Saidshow,

For any export/import on ASM, you need to use its /tasks endpoint, and file transfer endpoint afterwards.

In your case it is:

```markup
/mgmt/tm/asm/tasks/export-policy
/mgmt/tm/asm/file-transfer/downloads

```

**NOTE:** This is not possible in 11.5.4 as file\_transfer endpoint for ASM was added in 11.6.x. You can attempt to use another endpoint for file transfer (see: [this article](https://devcentral.f5.com/s/articles/demystifying-icontrol-rest-part-5-transferring-files) )

If you are familiar with python, using this library would be your best choice:

```markup
https://github.com/F5Networks/f5-common-python

```

Please have a look at the functional and unit tests as they should give you idea how to use it.

If you really want to use CURL, then I guess something like this should work:

**1. Export to a file**

```markup
curl -v -sk -u admin:admin https://myF5/mgmt/tm/asm/tasks/export-policy -H 'Content-Type: application/json' -X POST -d '{"filename":"as_test.xml","policyReference":"{"link": "https://localhost/mgmt/tm/asm/policies/yutqTnGiAizDLpdd_mkRvg"}"}'

```

- **Policy link above would have to correspond to selfLink property of your intended exported policy.**

**2. Download the file**

```markup
curl -v -sk -u admin:admin https://myF5/mgmt/tm/asm/file-transfer/downloads/as_test.xml -H 'Content-Type': 'application/octet-stream' -X GET

```

I hope this helps

---

<div class="post-metadata">

**Author:** ![WildWeasel](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/wildweasel/32/16221_2.png) [@WildWeasel](https://community.f5.com/u/WildWeasel)\
**Post date:** [May 12, 2023, 12:40pm UTC](https://community.f5.com/t/icontrolrest-and-curl-to-save-and-download-asm-policies/13584/6 "2023-05-12T12:40:59Z")

</div>

Thank You for sharing this..&nbsp; It was a big help and got me halfway there.

Anyway you have a similar script to now IMPORT all those .xml files?
