Forum Discussion

mvo_65102's avatar
mvo_65102
Icon for Nimbostratus rankNimbostratus
Jan 30, 2017

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.

 

9 Replies

  • I am not sure if this ability has been provided in newer code versions. Last I remember from this thread, it isn't directly feasible but you can do something similar to the idea proposed in the thread.

     

  • If you are looking for the iControl REST endpoint for executing a Unix command, that's

    /mgmt/tm/util/bash
    . Here's an example (for
    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.

  • 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"
    }
    
  • Hi, is here any way how to call tmsh command within a partition?

     

    • Jason_Adams's avatar
      Jason_Adams
      Icon for Employee rankEmployee

      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"
  • To list virtuals under the partition /Test directly from bash, for example, you can use

    echo
    to pass multiple statements to tmsh:

     bash -c 'echo "cd /Test; list ltm virtual" | tmsh'
    

    That's what you want to send to the

    /mgmt/tm/util/bash
    endpoint. e.g.,

      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

    /mgmt/tm/cli/script
    endpoint: e.g.,

    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.

  • 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

     

  • You can use the

    /mgmt/tm/ltm/node
    endpoint for node creation instead of calling
    /mgmt/tm/util/bash
    to run the tmsh command. The equivalent
    curl
    command for
    tmsh create ltm node TEST3 address 3.3.3.3
    is

    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.