Forum Discussion
Native tmsh/bash commands via REST API
Hi all,
is there a way to execute native tmsh or bash commands using the REST API? As long as there are not all configurations controllable by REST i would like to execute some commands directly.
- Satoshi_Toyosa1Ret. Employee
If you are looking for the iControl REST endpoint for executing a Unix command, that's
. Here's an example (for/mgmt/tm/util/bash
😞ls /tmp
curl -sku user:password -X POST -H "Content-type: application/json" \ -d "{\"command\":\"run\", \"utilCmdArgs\": \"-c 'ls /tmp'\"}" \ https:///mgmt/tm/util/bash
You can run a tmsh command in the same manner (e.g.,
😞tmsh list ltm virtual
curl -sku user:password -X POST -H "Content-type: application/json" \ -d "{\"command\":\"run\", \"utilCmdArgs\": \"-c 'tmsh list ltm virtual'\"}" \ https:///mgmt/tm/util/bash
Output lines are concatenated (not easy to read), so you may want to parse it by piping the result to
python -m json.tool | sed 's/\\n/\n/g'
Note that you will get 50x error when the execution of the specified command takes longer than a predefined timeout (about 60-90 seconds).
For more on the API, please refer to iControl REST API Reference Version 12.0.
- Eric_Flores_131Cirrostratus
Here is an example of bash over REST -
$ curl -sk -u 'admin:admin' -H 'Content-Type: application/json' -X POST \ -d '{"command": "run", "utilCmdArgs": "-c \"date -u\""}' \ https://$HOST/mgmt/tm/util/bash | jq . { "kind": "tm:util:bash:runstate", "command": "run", "utilCmdArgs": "-c \"date -u\"", "commandResult": "Fri Feb 3 01:11:56 UTC 2017\n" }
- ZdendaCirrus
Hi, is here any way how to call tmsh command within a partition?
- Jason_AdamsEmployee
Hello ,
You can absolutely call a tmsh command from within a partition. You can use the tmsh -c flag to run multiple tmsh commands in a single instance.
So you first cd to the partition, then run the desired command:
tmsh -c "cd /<partition>; <command>
For Example, if I want to list all Virtual Servers in the /test partition:
tmsh -c "cd /test; list ltm virtual"
- Satoshi_Toyosa1Ret. Employee
To list virtuals under the partition /Test directly from bash, for example, you can use
to pass multiple statements to tmsh:echo
bash -c 'echo "cd /Test; list ltm virtual" | tmsh'
That's what you want to send to the
endpoint. e.g.,/mgmt/tm/util/bash
curl -sku admin:admin https://192.168.184.30/mgmt/tm/util/bash \ -X POST -H "Content-Type: application/json" \ -d "{\"command\":\"run\", \"utilCmdArgs\":\"-c 'echo \\\"cd /Test; list ltm virtual\\\" | tmsh'\"}"
Example output:
{ "command": "run", "commandResult": "ltm virtual testvs {\n destination 172.16.10.45:http\n ip-protocol tcp\n mask 255.255.255.255\n partition Test\n pool /Common/CentOS-All\n profiles {\n /Common/tcp { }\n }\n source 0.0.0.0/0\n translate-address enabled\n translate-port enabled\n vs-index 7\n}\n", "kind": "tm:util:bash:runstate", "utilCmdArgs": "-c 'echo \"cd /Test; list ltm virtual\" | tmsh'" }
Please be careful with the escaping sequences.
If you have a more complex tmsh call, consider writing a tmsh script and call it via the
endpoint: e.g.,/mgmt/tm/cli/script
curl -sku admin:admin https://192.168.184.30/mgmt/tm/cli/script
Obviously, it does not make any sense calling tmsh via bash via tmsh (the iControl REST is just a wrapper for tmsh). Look for an endpoint that you can call directly for your purpose.
- Vikas7999_31548Nimbostratus
I am trying tmsh_commandlist = open("tmsh_commandlist.txt") from the python program specified above. I created command file tmsh_commandlist.txt which has a tmsh command i want to execute "create ltm node TEST3 address 3.3.3.3". When i run the program it completes successfully however does not create the new node on the LTM itself. As per my understanding i just have to specify the ltm commands as we type it on the F5 CLI to the text file and this program will configure it on the device. Any suggestions What could be the problem.
Thanks
- Satoshi_Toyosa1Ret. Employee
You can use the
endpoint for node creation instead of calling/mgmt/tm/ltm/node
to run the tmsh command. The equivalent/mgmt/tm/util/bash
command forcurl
istmsh create ltm node TEST3 address 3.3.3.3
curl -sku : https:///mgmt/tm/ltm/node \ -X POST -H 'Content-type: application/json' \ -d '{"name":"TEST3", "address":"3.3.3.3"}'
If Python is your choice, you may want to check the F5 Python SDK. See F5 Python SDK Documentation.
- PSilvaRet. Employee
Just making folks on this thread aware of: https://support.f5.com/csp/article/K23605346
Recent Discussions
Related Content
* 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