Repository navigation
Expand file tree
/
Copy pathSshUtil.py
More file actions
132 lines (108 loc) · 4.35 KB
/
Copy pathSshUtil.py
File metadata and controls
132 lines (108 loc) · 4.35 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#author: june
import os
import select
import paramiko
from abstracttool import Tool
from minisftp import Minisftp
import globals as G
########################################################################
class SSHOptionUtil(Tool):
""""""
#----------------------------------------------------------------------
def __init__(self, host, usr, psw, sftp_port):
"""Constructor"""
self.host = host
self.username = usr
self.password = psw
self.sftp_port = sftp_port
#self.minisftp = Minisftp(self.host, self.sftp_port, self.username, self.password)
#if not self.minisftp.sftp_conn():
# self.minisftp = None
if not self.ssh_conn():
self.ssh = None
return
if self.ssh is not None:
self.minisftp = Minisftp(self.ssh)#self.ssh.open_sftp()
else:
self.minisftp = None
#self.minisftp.put('/Users/june/tmp/mail.py', '/var/root/x.py', None)
#print '123'
#----------------------------------------------------------------------
def ssh_conn(self):
""""""
try:
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(self.host, port=self.sftp_port, username=self.username, password=self.password)
return True
except paramiko.AuthenticationException:
G.log(G.ERROR, "SSH Authentication failed when connecting to host")
return False
except:
G.log(G.ERROR, "SSH Could not SSH to %s, waiting for it to start host" % self.host)
return False
#----------------------------------------------------------------------
def ssh_exec(self, cmd, buffsize=-1, timeout=None, get_pty=False):
""""""
try:
stdin, stdout, stderr = self.ssh.exec_command(cmd, buffsize, timeout, get_pty)
return stdout.readlines()
except paramiko.SSHException:
return -1
#----------------------------------------------------------------------
def ssh_exec_last(self, cmd, buffsize=-1, timeout=None, get_pty=False):
""""""
try:
stdin, stdout, stderr = self.ssh.exec_command(cmd, buffsize, timeout, get_pty)
return stdout
except paramiko.SSHException:
return -1
#----------------------------------------------------------------------
def ssh_close(self):
""""""
self.minisftp.sftp_close()
G.log(G.INFO, 'close ssh connection')
self.ssh.close()
#----------------------------------------------------------------------
def file_exists(self, remote_path):
""""""
return self.minisftp.file_exists(remote_path)
#----------------------------------------------------------------------
def cache_file(self, remote_path, local_path):
""""""
self.mkdir_p(local_path[:local_path.rfind("/")])
if not self.file_exists(remote_path):
return None
if not os.path.exists(local_path):
self.minisftp.getfile(remote_path, local_path, None)
return os.path.realpath(local_path.encode("utf-8").strip())
#----------------------------------------------------------------------
def glob(self, path, pattern=".*"):
""""""
return self.minisftp.glob(path, pattern)
def _ssh_exec(self, cmd, print_flag=None, end_flag=None):
""""""
#import time
if cmd is None:
G.log(G.INFO, 'Shell command is None.')
transport = self.ssh.get_transport()
channel = transport.open_session()
channel.get_pty()
channel.exec_command(cmd)
content = ''
while True:
if channel.exit_status_ready():
break
rl, wl, xl = select.select([channel], [], [], 0.0)
if len(rl) > 0:
l = channel.recv(1024)
if print_flag is not None and l.find(print_flag) != None:
print l
else:
#print '.',
pass
#elif len(rl) == 0:
#print '.',
#time.sleep(3)
#if end_flag is not None:
# pass