-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSH_Client.py
More file actions
49 lines (38 loc) · 1.43 KB
/
SSH_Client.py
File metadata and controls
49 lines (38 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Handle imports
import paramiko.paramiko as paramiko
# Create classes
class SSH():
"""
Paramiko SSH implementation
"""
def __init__(self, IP, username, password = None, key_filename = None):
"""
Either password or key_filename is required.
:param IP: The IP of the remote machine.
:param username: The username of a user on the remote machine.
:param password: The password of a user on a remote machine.
:param key_filename: The key_filename on a remote machine.
"""
assert not ((password is None) and (key_filename is None)), "Please provide a password or key_filename."
self.IP = IP
self.max_packet_size = 1024
# Start parakimo for commands
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
if password is None:
self.ssh.connect(self.IP, username=username, key_filename=key_filename, port=22)
else:
self.ssh.connect(self.IP, username=username, password=password, port=22)
def pipe_command(self, cmd):
"""
Run a command, cmd, on the server.
"""
stdin, stdout, stderr = self.ssh.exec_command(cmd)
data = stdout.read().splitlines()
output = ""
for line in data:
output += line.decode("utf-8")
return output
def stop(self):
self.ssh.close()