An SSH library for Python3 to execute commands in a remote shell.
This library lets you open a remote shell through SSH. It was tested with Linux and Cisco IOS remotes and worked perfectly.
It doesn't support Python2.
First install libssh2
. The name of the library may depend on your OS, you can also try the libssh2
name. PySSH2 will need th libssh2.so
file.
# apt-get install libssh2-1
Then you can install PySSH2:
pip3 install git+https://github.com/ihrwein/PySsh2
The following example works like the ssh
command. It reads from your stdin and sends the commands to the remote computer. You can find this example as the clissh.py
file.
import pyssh2
import time
HOST_NAME = "localhost"
USER_NAME = "sshuser"
PASSWORD = ""
PORT = 10022
def main():
ssh = pyssh2.connect(USER_NAME, PASSWORD, HOST_NAME, PORT)
shell = ssh.shell()
print(shell.read(), end="")
while True:
try:
command = input()
shell.write(command)
print(shell.read(), end="")
except EOFError as err:
ssh.close()
break
if __name__ == "__main__":
main()