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
|
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (c) 2011 Zikzakmedia S.L. (http://zikzakmedia.com) All Rights Reserved.
# Raimon Esteve <resteve@zikzakmedia.com>
# Jesus MartÃn <jmartin@zikzakmedia.com>
# 2013 Enterprise Objects Consulting
# Mariano Ruiz <mrsarm@gmail.com>
# $Id$
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.osv import osv
from openerp.tools.translate import _
import paramiko
import os
import logging
class django_connect(osv.osv):
_name = 'django.connect'
_description = "Django Connect"
_logger = logging.getLogger('django.connect')
def ssh_command(self, cr, uid, ids, values, context={}):
result = True
basepath = values['basepath']
if not basepath [-1] == '/':
basepath += "/"
if context['command']:
client = paramiko.SSHClient()
if values['key']:
key = values['ssh_key']
if not os.path.exists(key):
raise osv.except_osv(_('Error!'), _('Key SSH %s not avaible. See documentation') % key)
client.load_system_host_keys(filename=key)
else:
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(str(values['ip']), port=int(values['port']), username=str(values['username']), password=str(values['password']))
stdin, stdout, stderr = client.exec_command(basepath+context['command'])
err = ''
for line in stderr:
err += line
result = False
if err != '':
self._logger.error(err)
out = ''
for line in stdout:
# print "Exit -> "+line
out += line
client.close()
else:
result = False
if result:
out2 = out.strip().lower()
if out2 == "false":
result = False
else:
try:
if int(out2) != 0:
result = False
except:
pass
else:
out2 = out.strip().lower()
if out2 in ("true", "success"):
result = True
else:
try:
if int(out2) == 0:
result = True
except:
pass
return result, out, err
django_connect()
|