Showing posts with label ssh. Show all posts
Showing posts with label ssh. Show all posts

Sunday, May 01, 2016

What did you Expect - a look at Python module Pexpect

On Tuesday I presented on Pexpect at the Grenoble Python User Group, a short presentation as Rene Ribaud was presenting DocOpt and staticjinja.

Pexpect is a pure-Python module implementing Expect functionality.  Expect is functionality to enable to automate interactions with some interactive process where the user is obliged to interact with the program.  Expect was originally written in tcl but versions have since appeared in many other languages.

When using expect you spawn a new process and then interact with that process through a series of interactions where the expect() is called and blocks until a suitable match occurs, e.g. a password prompt, and then send() is used to send text to the process as if sent by the user typing at the keyboard.

I started using pexpect because I had a special use case.  When installing a particular N-node OpenStack platform it was necessary to pass by several machines/accounts to reach the controller or compute nodes.  As machines and user accounts were automatically created with unknown passwords and installed ssh keys, it was necessary to perform a sequence of 3 ssh hops and 3 sudo calls to reach a compute or controller node.

Using pexpect it was possible to auto-login across those 6 accounts, by sending the password when necessary.  A script was written mhop_ssh.py to perform such multi-hop logins and even to install ssh keys along the way thus allowing traditional direct ssh connection to the controller or compute nodes.

I've placed my presentation notebook and the mhop_ssh.py script on github here.

For testing purposes I created a group of virtual servers using Docker, described here.



Creating serverless virtual servers with Docker

Well not serverless exactly as serverless is a popular term referring to the deploying of code in the cloud which does not require a dedicated server, as the AWS Lambda service or Google App Engine (GAE) for example.

In this case I needed some ssh addressable servers for testing my Pexpect scripts.  It occured to me that rather than using physical servers, or even creating virtual servers using Vagrant/VirtualBox I could create some Docker containers configured with openssh to act as if they were physical servers.

Whilst not the recommended use of Docker - where typically one or more application processes are created in a container - where "docker exec" rather than ssh would be used if there were a need to connect to the container, it fits perfectly my test use case.

I've placed my Dockerfile in this github repo.
It is used to create an container image running OpenSSH, and with a user 'user' and password 'password' - clearly security was not a concern for my testing !!

I intend to develop this repo making use of more Docker features to allow more complete server/network emulation for tests, demos, labs, presentations.


[Conference - CodeEurope.pl] Developing Micro-services on Kubernetes

In April I had the chance to present at CodeEurope.pl , first in Warsaw on Apr 24th, and then in Wroclaw ("wroslof" was my best at...