Created
December 1, 2019 07:50
-
-
Save tbhaxor/4caad387a8e47efda5a696edd6a1d1e8 to your computer and use it in GitHub Desktop.
Simple SSH Client by Paramiko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import subprocess | |
from paramiko import SSHClient, AutoAddPolicy | |
from argparse import ArgumentParser, RawDescriptionHelpFormatter | |
# config the arguments | |
parser = ArgumentParser(description="SSH Client", | |
formatter_class=RawDescriptionHelpFormatter) | |
parser.add_argument("--host", | |
help="hostname to connect", | |
required=True, | |
metavar="HOST/IP") | |
parser.add_argument("--port", | |
help="portname to connect", | |
metavar="NUMBER", | |
required=True, | |
type=int) | |
parser.add_argument("--user", | |
help="username of ssh server", | |
metavar="USERNAME") | |
parser.add_argument("--pswd", | |
help="password of ssh server", | |
metavar="PASSWORD") | |
parser.add_argument("--command", | |
help="command to run", | |
metavar="CMD", | |
required=True) | |
# parse arguments | |
args = parser.parse_args() | |
# create ssh client | |
ssh = SSHClient() | |
# setting up the auto add policy | |
ssh.set_missing_host_key_policy(AutoAddPolicy()) | |
# connecting to the ssh server | |
ssh.connect(args.host, args.port, args.user, args.pswd) | |
# getting transport session | |
ssh_session = ssh.get_transport().open_session() | |
# checking if session is alive | |
if ssh_session.active: | |
# executing the command | |
ssh_session.exec_command(args.command) | |
# recieving the output | |
print(ssh_session.recv(4096).decode()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment