Search This Blog

Showing posts with label cli. Show all posts
Showing posts with label cli. Show all posts

Sunday, April 27, 2014

You can use bash shell instead of Cisco CLI on Nexus Switches

Every one who works on Linux and understand how to efficiently use Bash hates to work with the limited Cisco IOS CLI. The design objectives standing behind this CLI haven't changed for the last 20 years or so. It is obvious that this tools lacks plenty of features expected from a modern shell for many people.

But the evolution or even revolution that is happening in networking thanks to SDN is changing this terrible static network configuration landscape. The new generation of network devises like Cisco Nexus platform are going to support in the Cisco NX-OS :
  • Bash shell
  • Python shell 
  • API access
  • Linux containers for custom applications
For these who still don't believe you can read about this here:

References

http://www.cisco.com/c/en/us/products/switches/nexus-9000-series-switches/white-paper-listing.html
http://rtomaszewski.blogspot.co.uk/search/label/sdn
http://rtomaszewski.blogspot.co.uk/2013/09/cisco-cheat-sheet.html

Sunday, March 30, 2014

How to automatically prefill command on the Linux bash

Linux Bash is one of the most famous Linux shells. It offers a great number of features like for example spawning and controlling process, redirecting streams, supporting scripts and a flexible way to control you editing line.

Problem

How to automatically pre-populate a command on the shell after prompt.

Solution description

The shell has tree default streams: stdout, stdin and stderr. By manipulating the stdin of the process we can simulate typing a command.

Reference implementation

The original script can be found here: https://github.com/rtomaszewski/experiments/blob/master/type-command.c

Demonstration
  • Compile first the program
gcc -o type-command type-command.c
  • Run for the firs time
# ./type-command
type-command: the variable TYPE_CMD_ENABLED is not set, set it to 'no' to surpress this message; set the TYPE_CMD_TYPE for the command to type

Example: export TYPE_CMD_ENABLED=yes; export TYPE_CMD_TYPE=date
  • Export the variable to controls if the program should try to type a command or not
# export TYPE_CMD_ENABLED=yes
# ./type-command
#
  • Specify the command that you wish to be typed
# export TYPE_CMD_ENABLED=yes; export TYPE_CMD_TYPE=date
# ./type-command
# date
Sun Mar 30 19:27:55 UTC 2014>

References

http://stackoverflow.com/questions/10866005/bash-how-to-prefill-command-line-input
http://stackoverflow.com/questions/11198603/inject-keystroke-to-different-process-using-bash
http://unix.stackexchange.com/questions/48103/construct-a-command-by-putting-a-string-into-a-tty

http://fossies.org/linux/misc/old/console-tools-0.3.3.tar.gz%3at/console-tools-0.3.3/vttools/writevt.c

http://man7.org/linux/man-pages/man4/tty_ioctl.4.html
http://man7.org/linux/man-pages/man3/tcflush.3.html
http://www.tldp.org/LDP/lpg/node143.html

Wednesday, October 24, 2012

Example of nova boot command

Problem

There is a lot of written about how the Cloud API works. What the request has to look like and what the correct successes is. But there aren't many practical tutorials or howtos on how to use the tools in practice.

Below is an example of how to create a cloud server using the nova tool.

Solution

This is a link that describe how to install the nova tool [1]. I'm not going to cover this here and I assume you already have it.

  1. Creaet a simple cloud server

  2. root@server:~# nova help | grep image
    root@server:~# nova help | grep flavor
    root@server:~# nova help | grep meta
    root@server:~# nova boot --flavor 2 --image 5cebb13a-f783-4f8c-8058-c4182c724ccd test4 
    +-------------------------+--------------------------------------+
    | Property                | Value                                |
    +-------------------------+--------------------------------------+
    | OS-DCF:diskConfig       | AUTO                                 |
    | OS-EXT-STS:power_state  | 0                                    |
    | OS-EXT-STS:task_state   | scheduling                           |
    | OS-EXT-STS:vm_state     | building                             |
    | accessIPv4              |                                      |
    | accessIPv6              |                                      |
    | adminPass               | 7777                                 |
    | created                 | 2012-10-23T23:11:46Z                 |
    | flavor                  | 512MB Standard Instance              |
    | hostId                  |                                      |
    | id                      | db16f2bc-0f75-4215-927b-c8a34e165dce |
    | image                   | Ubuntu 12.04 LTS (Precise Pangolin)  |
    | metadata                | {}                                   |
    | name                    | test4                                |
    | progress                | 0                                    |
    | rax-bandwidth:bandwidth | []                                   |
    | status                  | BUILD                                |
    | tenant_id               | 7777                                 |
    | updated                 | 2012-10-23T23:11:47Z                 |
    | user_id                 | 7777                                 |
    +-------------------------+--------------------------------------+
    


  3. Creaet a simple cloud server with metadata
root@server:~# nova boot --flavor 2 --image 5cebb13a-f783-4f8c-8058-c4182c724ccd test4 --meta myke1='value1' --meta mykey2='value2 and 3'
+-------------------------+---------------------------------------------------+
| Property                | Value                                             |
+-------------------------+---------------------------------------------------+
| OS-DCF:diskConfig       | AUTO                                              |
| OS-EXT-STS:power_state  | 0                                                 |
| OS-EXT-STS:task_state   | scheduling                                        |
| OS-EXT-STS:vm_state     | building                                          |
| accessIPv4              |                                                   |
| accessIPv6              |                                                   |
| adminPass               | 777                                               |
| created                 | 2012-10-23T23:15:18Z                              |
| flavor                  | 512MB Standard Instance                           |
| hostId                  |                                                   |
| id                      | 3f6be5ae-9c4e-4b0f-880f-c4928e426864              |
| image                   | Ubuntu 12.04 LTS (Precise Pangolin)               |
| metadata                | {u'mykey2': u'value2 and 3', u'myke1': u'value1'} |
| name                    | test4                                             |
| progress                | 0                                                 |
| rax-bandwidth:bandwidth | []                                                |
| status                  | BUILD                                             |
| tenant_id               | 7777                                              |
| updated                 | 2012-10-23T23:15:19Z                              |
| user_id                 | 777                                               |
+-------------------------+---------------------------------------------------+

References
  1. http://docs.rackspace.com/servers/api/v2/cs-gettingstarted/content/section_gs_install_nova.html
  2. http://docs.openstack.org/api/quick-start/content/
  3. http://devops.rackspace.com/13/getting-started-using-python-novaclient-to-manage-cloud-servers
  4. http://vexxhost.com/blog/2012/09/10/installing-cpanel-on-a-cloud-server/

Example of nova credential command



If your credential settings are correct you are going to see this output

root@server:~# set | grep OS_
OS_AUTH_SYSTEM=rackspace_uk
OS_AUTH_URL=https://lon.identity.api.rackspacecloud.com/v2.0/
OS_NO_CACHE=1
OS_PASSWORD=777
OS_REGION_NAME=LON
OS_TENANT_NAME=777
OS_USERNAME=777

root@server:~# nova credentials
+------------------------+---------------------------------------------------------------------------------------+       22:51:35
| User Credentials       | Value                                                                                 |
+------------------------+---------------------------------------------------------------------------------------+
| RAX-AUTH:defaultRegion |                                                                                       |
| id                     | 777                                                                                 |
| name                   | name                                                                       |
| roles                  | [{u'description': u'User Admin Role.', u'id': u'3', u'name': u'identity:user-admin'}] |
+------------------------+---------------------------------------------------------------------------------------+
+---------+--------------------------------------------+
| Token   | Value                                      |
+---------+--------------------------------------------+
| expires | 2012-10-24T13:21:47.000+01:00              |
| id      | f6e9c367-7777-7777-7777-777777777777       |
| tenant  | {u'id': u'777', u'name': u'777'} |
+---------+--------------------------------------------+


If your credentials are wrong you will see this one instead

root@server:~# nova credentials
ERROR: Invalid OpenStack Nova credentials.

root@server:~# nova --debug credentials

REQ: curl -i https://lon.identity.api.rackspacecloud.com/v2.0/tokens -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "User-Agent: python-novaclient" -d '{"auth": {"RAX-KSKEY:apiKeyCredentials": {"username": "777", "apiKey": "777", "tenantName": "777"}}}'

connect: (lon.identity.api.rackspacecloud.com, 443)
send: 'POST /v2.0/tokens HTTP/1.1\r\nHost: lon.identity.api.rackspacecloud.com\r\nContent-Length: 154\r\ncontent-type: application/json\r\naccept-encoding: gzip, deflate\r\naccept: application/json\r\nuser-agent: python-novaclient\r\n\r\n{"auth": {"RAX-KSKEY:apiKeyCredentials": {"username": "777", "apiKey": "777", "tenantName": "777"}}}'
reply: 'HTTP/1.1 401 Unauthorized\r\n'
header: Server: nginx/0.8.55
header: Date: Tue, 23 Oct 2012 22:55:15 GMT
header: Content-Type: application/json
header: Transfer-Encoding: chunked
header: Connection: keep-alive
header: Content-Encoding: gzip
header: response-source: cloud-auth
header: vary: Accept, Accept-Encoding, X-Auth-Token
header: VIA: 1.0 Repose (Repose/2.3.5)
RESP:{'status': '401', 'content-length': '73', 'via': '1.0 Repose (Repose/2.3.5)', 'response-source': 'cloud-auth', 'transfer-encoding': 'chunked', 'vary': 'Accept, Accept-Encoding, X-Auth-Token', 'server': 'nginx/0.8.55', 'connection': 'keep-alive', '-content-encoding': 'gzip', 'date': 'Tue, 23 Oct 2012 22:55:15 GMT', 'content-type': 'application/json'} {"unauthorized":{"code":401,"message":"Username or api key is invalid"}}

DEBUG (shell:534) Invalid OpenStack Nova credentials.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/novaclient/shell.py", line 531, in main
    OpenStackComputeShell().main(sys.argv[1:])
  File "/usr/local/lib/python2.7/dist-packages/novaclient/shell.py", line 463, in main
    raise exc.CommandError("Invalid OpenStack Nova credentials.")
CommandError: Invalid OpenStack Nova credentials.
ERROR: Invalid OpenStack Nova credentials.