forked from zjxde/serv00-autodeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.py
54 lines (43 loc) · 1.46 KB
/
ssh.py
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
50
51
# -*- coding:utf-8 -*-
import paramiko
class SSHClient(object):
err = "argument passwd or rsafile can not be None"
def __init__(self, host, port, user, passwd=None, rsafile=None):
self.h = host
self.p = port
self.u = user
self.w = passwd
self.rsa = rsafile
def _connect(self):
if self.w:
return self.pwd_connect()
elif self.rsa:
return self.rsa_connect()
else:
raise ConnectionError(self.err)
def pwd_connect(self):
conn = paramiko.SSHClient()
conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn.connect(self.h, self.p, self.u, self.w)
return conn
def rsa_connect(self):
pkey = paramiko.RSAKey.from_private_key_file(self.rsa)
conn = paramiko.SSHClient()
conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn.connect(hostname=self.h, port=self.p, username=self.u, pkey=pkey)
return conn
def run_cmd(self, cmd,close=0):
conn = self._connect()
stdin, stdout, stderr = conn.exec_command(cmd)
code = stdout.channel.recv_exit_status()
stdout, stderr = stdout.read(), stderr.read()
if close:
conn.close()
if not stderr:
return code, stdout.decode()
else:
return code, stderr.decode()
def close(self):
conn = self._connect()
if conn:
conn.close()