Adding a command
(verified 2010 Apr. / EOS 4.3.0 / DCS-7120T-4S 03.01)
Command
Basically, EOS commands are written in Python. When user login to the system, CLI (command line interpreter) comes up and interacts with user. Then user type into a command, the associated Python program will be called. That is command.
If we understand the detail structure and the path of this process, we can add our own command to the system.
Files
The actual command files are located under /usr/lib/python2.5/site-packages/CliPlugin directory. For example, Ntp.py implements about ntp and related commands.
(You can find DonkeyCli.py hidden command and it is also a good sample code of the EOS CLI command. Must see.)
Therefore, if you write a new command imitate Ntp.py and place it to that directory, it will be available at the CLI which starts after that process.
In this document, we show the registeration path of command from Python based program to EOS CLI.
from login
When user login to the system, /usr/bin/Cli program will be kicked as a login shell, through /usr/bin/RunCli program. (/usr/bin/Cli is also included in /etc/shells )
/usr/bin/Cli is a wrapper to Cli.py. For instance, you can just start /usr/bin/Cli from bash then you see the EOS CLI prompt.
bash-3.2# /usr/bin/Cli localhost>show vlan VLAN Name Status Ports ---- -------------------------------- --------- ------------------------------- 1 vlan1 active Cpu, Et1 2 vlan2 active Cpu, Et11, Et12, Et13 3 vlan3 active Cpu, Et17 localhost>
Scan and Registration
After loading of /usr/lib/python2.5/site-packages/Cli.py, it calls loadPlugins() function at __init__() function. It scans /usr/lib/python2.5/site-packages/CliPlugin directory to register all commands.
For example, Ntp.py includes BasicCli.registerShowCommand() function call for adding NTP options to SHOW command. And also includes BasicCli.GlobalConfigMode.addCommand() function call for adding NTP configuration subcommands to configure mode of EOS CLI.
It means if you want to register your own command, you need to write some command registration procedure in Python and need to place it in this Plugin directory.
Preparations
Here are some preparation processes to place a file on the system directory, such as bash access, filesystem remounting and etc.
bash command is available on enable mode of EOS CLI. In almost case, it will be required the administrator permission, so just do su as follows;
mypc$ slogin -l admin arista.example.com Password: localhost>enable localhost#bash Arista Networks EOS shell [admin@localhost ~]$ su Password: bash-3.2#
root file system is mounted as read only mode. It needs to remount as read write mode as follows;
bash-3.2# mount -o rw,remount / bash-3.2#
Place a program
Place a Python program which includes command registration process into /usr/lib/python2.5/site-packages/CliPlugin directory. There are several pathway to place your program to Arista directory, as follows;
Sample : Hello World
We start from the old tradition, Hello World. Make a HelloCli.py file as follows. (left number is the line number. )
2: tokenHello = CliParser.KeywordRule( 'hello', helpdesc='Says hello' )
3: def doHello( mode ):
4: print "World.\n"
line 1 : this is the minimum import of the system API.
Sample operation
Here is an example to write a program by vi editor.
bash-3.2# cd /usr/lib/python2.5/site-packages/CliPlugin/
bash-3.2# vi HelloCli.py
bash-3.2# cat HelloCli.py
import BasicCli, CliParser
tokenHello = CliParser.KeywordRule( 'hello', helpdesc='Says hello' )
def doHello( mode ):
print "World.\n"
BasicCli.UnprivMode.addCommand( ( tokenHello, doHello ) )
bash-3.2#
Confirmation
Invoke /usr/bin/Cli from bash then try hello command.
bash-3.2# /usr/bin/Cli localhost>hello World. localhost>exit bash-3.2#
And type ? key on localhost> prompt. You can see the hello command and help message that you have registered also included as follows;
bash-3.2# /usr/bin/Cli localhost>? .... hello Says hello .... localhost>
So we have succeed to add a new command to EOS CLI.
next step
Of course it is too simple and useless command. There are some steps to climb up.
- make command persistently : adding commands as Extension with SWIX format.
- make a command with options
- use Arista oriented services such as SysDB
We hope we have a time to try them soon.