-
-
Save me-manikanta/aa9e0fd11ba2757521b6e1c5265ac29a to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
######################### | |
# Proof of concept code # | |
######################### | |
import paramiko | |
import os | |
import sys | |
import time | |
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
import socket | |
HOST_NAME = 'test.rebex.net' | |
USER_NAME = 'demo' | |
PASSWORD = 'password' | |
try: | |
print 'Initializing the connection' | |
client = paramiko.SSHClient() | |
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
client.connect(hostname=HOST_NAME, username=USER_NAME, | |
password=PASSWORD) | |
print ('Connected to the server', HOST_NAME) | |
commands = ['ls', 'ls -l', 'cd pub/example', 'pwd'] | |
print 'Executing commands on the server' | |
for command in commands: | |
print 'Executing command --> {}'.format(command) | |
(stdin, stdout, stderr) = client.exec_command(command, | |
timeout=10) | |
ssh_output = stdout.read() | |
ssh_error = stderr.read() | |
if ssh_error: | |
print 'Error occurred while running command:' + command \ | |
+ ' The error is ' + ssh_error | |
result_flag = False | |
else: | |
print ('Command execution completed successfully', command) | |
print ('Output: ', ssh_output) | |
print 'Downloading files from server' | |
sftp_client = client.open_sftp() | |
sftp_client.get('readme.txt', './readme_downloaded.txt') | |
sftp_client.close() | |
# This code might not work if we are uploading to example server | |
sftp_client = client.open_sftp() | |
sftp_client.put('./local_file.txt', 'remote_file.txt') | |
sftp_client.close() | |
client.close() | |
except paramiko.AuthenticationException: | |
print 'Authentication failed, please verify your credentials' | |
except paramiko.SSHException, sshException: | |
print 'Could not establish SSH connection: %s' % sshException | |
except socket.timeout, e: | |
print 'Connection timed out' | |
except Exception, e: | |
print ('Stack trace:\n', e) | |
client.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment