Issuing Eggplant Network Emulator Commands

If you want to operate Eggplant Network from a command-line interface (CLI), you have two methods of sending commands:

  • From the command line using epncli.py
  • Using the socket API

From the Command Line

If you want to control the Emulator from the command line, you can use the provided Python script, epncli.py. You can download this script from the Eggplant downloads page for Eggplant Network; the file is called API Client v2.1.0 (epncli.py). You need an installed Python language interpreter to run the script. The scripts have been tested with both Python 2.x and Python 3.x.

You must also supply necessary parameters for the user and password you want to use, as well as the IP address of the Emulator you want to control. Finally, you include the task you want to perform.

In generic terms, that list of requirements looks like this:

$ python epncli.py --host <Emulator IP address> --user <username> --password <password> --command [--options . . .]

You’ll be interested in the result of your commands, so sending the output to a file by adding >filename (and then scanning this file) or piping the output to another program is a good idea.

To see what commands you can use with the Emulator, see Starting, Stopping, and Listing Emulations from the Command Line and Additional Eggplant Network Emulator Commands.

The Socket API

If you use the direct TCP socket method, a script is not used and instead just the command and options are specified and sent through a socket.

There is a wrinkle, however: The communication must be done using Secure Sockets Layer (SSL), and this requirement creates a few extra steps. The first thing you need to do is to get the remote certificate for the SSL connection. Then you can issue commands. The two sections below detail these steps with Python source examples.

Get the Remote Certificate

You retrieve the SSL certificate from the Emulator and store it in a file where it can be used later. In our sample Python script, that is done like this:

def getRemoteCert(self):

certData = ssl.get_server_certificate((self.host, self.port), ssl_version=ssl.PROTOCOL_TLSv1)

 

if not len(certData):

raise IneError('Unable to retrieve certificate from the server')

 

try:

fpath = tempfile.gettempdir() + '/server.crt'

f = open(fpath, 'w')

f.write(certData)

self.cert = f.name

f.close()

except:

raise IneError('Unable to store server certificate to establish SSL connection')

In summary, this process amounts to:

  1. Request the remote certificate from the Emulator with IP address self.host and using port self.port (7292); the request uses the Python library method ssl.get_server_certificate with SSL version set to TLSv1.
  2. Store the certificate in a temp file with the name server.crt.

Issue Commands

After you have the certificate (which is clearly not necessary to do for each command), you can send commands to the Emulator.

The steps for sending commands, showing Python source examples, is as follows:

  1. Create a socket:

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  2. Wrap the socket with the SSL layer:

    sslsock = ssl.wrap_socket(s, ca_certs=self.cert, cert_reqs=ssl.CERT_REQUIRED,ssl_version=ssl.PROTOCOL _TLSv1)

  3. Connect the SSL socket on port 7292:

    sslsock.connect((self.host, self.port))

  4. Send command and options:

    sslsock.sendall(c)

  5. Read response:

    data = sslsock.recv(4096).decode()

  6. Process the response and check for errors (--error).
  7. If you have further commands to send, repeat steps 4 through 7.
  8. Finally, close the socket:

    sslsock.close()

 

This topic was last updated on November 20, 2019, at 03:32:06 PM.

Eggplant icon Eggplantsoftware.com | Documentation Home | User Forums | Support | Copyright © 2020 Eggplant