Running a command on a remote SSH server

This example imports the getpass module, which will prompt the host, user, and password, establish the connection, and run some commands on a remote server.

You can find the following code in the filename: pxsshConnection.py:

import pxsshimport getpasstry:     connection = pxssh.pxssh()    hostname = input('hostname: ')    username = input('username: ')    password = getpass.getpass('password: ')    connection.login (hostname, username, password)    connection.sendline ('ls -l')    connection.prompt()    print(connection.before)    connection.sendline ('df')    connection.prompt()    print(connection.before)    connection.logout()except pxssh.ExceptionPxssh as e:    print("pxssh failed on login.")    print(str(e))

We can create specific methods ...

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.