cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2528
Views
10
Helpful
9
Replies

ACI - an alias comand

jchimenop
Level 1
Level 1

Hello all

 

As an example, I use a lot of times the command:

apic#show run leaf 109 interface ethernet 1/23;show run leaf 110 interface ethernet 1/23;

I want to create an alias, an script or some in my ssh profile that it could helps to get the output but with a few words

 

As an example

apic#show iconf 109 110 23

 

 

I tried with a bash without exit, because it didn't find the commands.

#!/bin/bash

 

show running-configuration leaf $1 interface ethernet 1/$3;show run leaf $2 interface ethernet 1/$3;

 

 

Thx

 

9 Replies 9

omz
VIP Alumni
VIP Alumni

Hi 

Are you using shell to ssh to the apic?

You could something like this - 

copy and paste in shell 

intconf () { 
ssh root@$1  << EOF
 show run leaf 109 interface ethernet 1/23
 show run leaf 110 interface ethernet 1/23
EOF
}

I don't have ssh access to an apic. Example on IOS-XE works like this - 

bash-ex.gif

you can add the function in .bashrc and it will available everytime you open terminal

 

hope this helps

Thank you for your post. It's amazing that what do you do.

Sergiu.Daniluk
VIP Alumni
VIP Alumni

Hi @jchimenop 

Uff.. I must admit, this was an interesting question.

So this is the script:

apic1# cat showif.sh 
#!/bin/sh
/controller/decoy/decoy-env/bin/python2.7 -m pyclient.remote exec terminal  114  show run leaf $1 interface eth 1/$3
/controller/decoy/decoy-env/bin/python2.7 -m pyclient.remote exec terminal  114  show run leaf $2 interface ethernet 1/$3 

Now, how did I came with this values script you may ask? oh boy, it was interesting.

First, let's see how the "show" commands are working on APIC:

apic1# alias | grep show
sh='_exec_cmd_pager "show" "$@"'
show='_exec_cmd_pager "show" "$@"'

Ok, so it seems there is a script reference named _exec_cmd_pager.

apic1# which _exec_cmd_pager
_exec_cmd_pager () {
        if [ "${_NO_PAGER}" = "yes" ]
        then
                _exec_cmd "$@" 2> /dev/null
        elif [ "${_TRACE}" = "yes" ]
        then
                _exec_cmd "$@"
        elif [ "${_PAGER_LEN}" = "0" ]
        then
                _exec_cmd "$@" 2> /dev/null | /bin/more
        else
                _exec_cmd "$@" 2> /dev/null | /bin/more -"${_PAGER_LEN}"
        fi
        return $?
}
apic1# which _exec_cmd
_exec_cmd () {
        if [ "$_TEST_DELIM" != "" ]
        then
                printf "$_TEST_DELIM"
                _exec_py "$@"
                local _retCode=$?
                printf "$_TEST_DELIM"
                return _retCode
        fi
        _exec_py "$@"
        return $?
}
apic1# which _exec_py
_exec_py () {
        if [ "$1" = "--local" ]
        then
                shift 1
                ${PYTHON} -m pyclient.remote exec terminal ${COLUMNS} "$@"
        else
                ${PYTHON} -m pyclient.remote exec server ${COLUMNS} "$@"
        fi
}

So we found the actual cmd which runs the "show" commands, or actually any other apic commands. 

Now if you want to use the line as it is in a bash script, it will still not work (I did not had time to check why), but if you change the PYTHON and COLUMNS with the real value, it works:

apic1# echo $PYTHON
/controller/decoy/decoy-env/bin/python2.7
apic1# echo $COLUMNS
114

Aaaaaand here is the result:

apic1# ./showif.sh 101 111 24 
# Command: show running-config leaf 101 interface eth 1/24
# Time: Tue Apr 28 22:14:14 2020
  leaf 101 
    interface ethernet 1/24
      # policy-group ansible_if_pg
      switchport access vlan 3802 tenant AutoLab application APP1 epg EPG2 
      exit
    exit
# Command: show running-config leaf 111 interface ethernet 1/24
# Time: Tue Apr 28 22:14:23 2020
  leaf 111 
    interface ethernet 1/24
      # policy-group ansible_if_pg
      switchport access vlan 3803 tenant AutoLab application APP1 epg EPG3 
      exit
    exit

Note: Is important to verify what environment is being used for python since you will require specific modules (like requests). Also, later version of APIC version, python will change to version 3 so double check which env is being used.

 

Hope it helps,

Sergiu

Thank you.
It works. I'm already using that script in my environment

Happy to hear it helps you ^_^

 

Stay safe,

Sergiu

Hi @Sergiu.Daniluk,

First of all, thank you for this great explanation. I know this post is a bit old, but I couldn't find anything on google relevant to my problem, so I decided to ask here.

I'm trying to basically the same thing, only not directly on the console of APIC instead, running this command via Ansible from a remote host.

If I try, for example, a basic command like"ls" and"pwd" or even a Moquery like this:

moquery -c ethpmPhysIf -f 'ethpm.PhysIf.operSt=="down" and ethpm.PhysIf.usage!="blacklist"' | egrep "dn" | grep node-1220

everything works as expected.

 

But when I try the run fabric command which looks like this:

/controller/decoy/decoy-env/bin/python -m pyclient.remote exec server $COLUMNS fabric 111 show vpc brief

I get a "non-zero return code" Which I can't understand. The response from APIC look like this:

 

msg": [
        {
            "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
            },
            "changed": true,
            "cmd": "/controller/decoy/decoy-env/bin/python -m pyclient.remote exec server $COLUMNS fabric 111 show vpc brief",
            "delta": "0:00:00.397055",
            "end": "2021-01-13 13:24:29.621140",
            "failed": true,
            "msg": "non-zero return code",
            "rc": 1,
            "start": "2021-01-13 13:24:29.224085"
            "stderr_lines": [
                "Traceback (most recent call last):",
                "  File \"/usr/lib64/python2.7/runpy.py\", line 174, in _run_module_as_main",
                "    \"__main__\", fname, loader, pkg_name)",
                "  File \"/usr/lib64/python2.7/runpy.py\", line 72, in _run_code",
                "    exec code in run_globals",
                "  File \"/controller/yaci/pyclient/remote.py\", line 210, in <module>",
                "    writeRetCode(e.errno)",
                "  File \"/controller/yaci/pyclient/_state.py\", line 37, in writeRetCode",
                "    with open(retFile, 'w') as f:",
                "IOError: [Errno 2] No such file or directory: '/home/svc_switch/.6168/RETCODE'"
            ],
            "stdout": "",
            "stdout_lines": []
        },

 If you have an idea and can point me in the solution's direction, I will be glad. Thank you!

 

Hi @O.K. 

Have you tried changing the $COLUMNS with the real value?

Check it using this command:

apic1# echo $COLUMNS

Regards,

Sergiu

Hi, thank you for your reply. I've already tried this; as you mentioned in your previous message, both of them on the console (real value or $COLUMNS) are work properly.  Columns value is different for every session, and I tried to set it dynamically in Ansible, which looks as follows:

 

tasks:
    - name: Get the $COLUMNS value
      shell: echo $COLUMNS
      args:
        executable: /bin/zsh
      register: stdout

    - name: save the columns value in a variable
      set_fact:
        columns: "{{ stdout.stdout }}"

    - name: Run command
      shell:  /controller/decoy/decoy-env/bin/python2.7 -m pyclient.remote exec server "{{columns}}" fabric 111 show vpc brief
      args:
        executable: /bin/zsh
      ignore_errors: true
      register: query_result

but without luck.

 

Regards,

Oguz

 

I think is a problem with the shell module. It doesn't work with command or exec either. I do not know any module which sends CLI commands to APIC, but i guess this is a good opportunity to write your own and then publish it

 

Cheers,

Sergiu

Review Cisco Networking for a $25 gift card

Save 25% on Day-2 Operations Add-On License