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:
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')
fpath = tempfile.gettempdir() + '/server.crt'
f = open(fpath, 'w')
self.cert = f.name
raise IneError('Unable to store server certificate to establish SSL connection')
In summary, this process amounts to:
- 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.
- Store the certificate in a temp file with the name server.crt.
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:
- Create a socket:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- 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)
- Connect the SSL socket on port 7292:
- Send command and options:
- Read response:
data = sslsock.recv(4096).decode()
- Process the response and check for errors (--error).
- If you have further commands to send, repeat steps 4 through 7.
- Finally, close the socket: