Advertisement
Guest User

Untitled

a guest
Apr 1st, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. import sys, paramiko
  2. import getpass
  3.  
  4. #usage: provide hostname & port
  5. if len(sys.argv) < 3:
  6.     print "args missing"
  7.     sys.exit(1)
  8.  
  9. #Get info about server and credentials
  10. class Host:
  11.     port = 22
  12.  
  13.     def __init__(self, hostname, command, username, password, port):
  14.         self.hostname = sys.argv[1]
  15.         self.command = sys.argv[2]
  16.         self.username = getpass.getpass(prompt='Type a username: ')
  17.         self.password = getpass.getpass(prompt='Type a password: ', stream=None)
  18.         self.port = port
  19. #Initiate SSH connection
  20.     def sshInit(self):
  21.         client = paramiko.SSHClient()
  22.         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  23.  
  24.         client.connect(self.hostname, self.command, self.username, self.password, self.port)
  25.  
  26.     def executeCommand(self, cmd):
  27.         connection_client = self.sshInit()
  28.         if connection_client.get_transport().is_active():
  29.             print("OK")
  30.             stdin, stdout, stderr = connection_client.exec_command(command)
  31.             print (stdout)
  32.         connection_client.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement