source: ogAgent-Git/src/opengnsys/linux/operations.py @ 10fab78

configure-ptt-chedecorare-oglive-methodsejecutarscript-b64fix-cfg2objfixes-winlgromero-filebeatmainmodulesnew-browserno-ptt-paramogadmcliogadmclient-statusogagent-jobsogagent-macosogcore1oglogoglog2override-moduleping1ping2ping3ping4py3-winreport-progresstlsunification2unification3versionswindows-fixes
Last change on this file since 10fab78 was b84ab33, checked in by Natalia Serrano <natalia.serrano@…>, 11 months ago

refs #247 migrate agent from py2 & qt4/qt5 to py3 & qt6

  • Update installation document
  • No longer create rpm linux packages
  • Change deb maintainer from one person to one team
  • Remove stray debhelper files
  • Filter more stuff in .gitignore
  • Property mode set to 100644
File size: 9.4 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (c) 2014 Virtual Cable S.L.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without modification,
7# are permitted provided that the following conditions are met:
8#
9#    * Redistributions of source code must retain the above copyright notice,
10#      this list of conditions and the following disclaimer.
11#    * Redistributions in binary form must reproduce the above copyright notice,
12#      this list of conditions and the following disclaimer in the documentation
13#      and/or other materials provided with the distribution.
14#    * Neither the name of Virtual Cable S.L. nor the names of its contributors
15#      may be used to endorse or promote products derived from this software
16#      without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29'''
30@author: Adolfo Gómez, dkmaster at dkmon dot com
31'''
32
33
34import socket
35import platform
36import fcntl
37import os
38import locale
39import ctypes  # @UnusedImport
40import ctypes.util
41import subprocess
42import struct
43import array
44import six
45import distro
46from opengnsys import utils
47from .renamer import rename
48
49
50def _getMacAddr(ifname):
51    '''
52    Returns the mac address of an interface
53    Mac is returned as unicode utf-8 encoded
54    '''
55    if isinstance(ifname, list):
56        return dict([(name, _getMacAddr(name)) for name in ifname])
57    if isinstance(ifname, six.text_type):
58        ifname = ifname.encode('utf-8')  # If unicode, convert to bytes (or str in python 2.7)
59    try:
60        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
61        info = bytearray(fcntl.ioctl(s.fileno(), 0x8927, struct.pack(str('256s'), ifname[:15])))
62        return six.text_type(''.join(['%02x:' % char for char in info[18:24]])[:-1])
63    except Exception:
64        return None
65
66
67def _getIpAddr(ifname):
68    '''
69    Returns the ip address of an interface
70    Ip is returned as unicode utf-8 encoded
71    '''
72    if isinstance(ifname, list):
73        return dict([(name, _getIpAddr(name)) for name in ifname])
74    if isinstance(ifname, six.text_type):
75        ifname = ifname.encode('utf-8')  # If unicode, convert to bytes (or str in python 2.7)
76    try:
77        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
78        return six.text_type(socket.inet_ntoa(fcntl.ioctl(
79            s.fileno(),
80            0x8915,  # SIOCGIFADDR
81            struct.pack(str('256s'), ifname[:15])
82        )[20:24]))
83    except Exception:
84        return None
85
86
87def _getInterfaces():
88    '''
89    Returns a list of interfaces names coded in utf-8
90    '''
91    max_possible = 128  # arbitrary. raise if needed.
92    space = max_possible * 16
93    if platform.architecture()[0] == '32bit':
94        offset, length = 32, 32
95    elif platform.architecture()[0] == '64bit':
96        offset, length = 16, 40
97    else:
98        raise OSError('Unknown arquitecture {0}'.format(platform.architecture()[0]))
99
100    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
101    names = array.array(str('B'), b'\0' * space)
102    outbytes = struct.unpack(str('iL'), fcntl.ioctl(
103        s.fileno(),
104        0x8912,  # SIOCGIFCONF
105        struct.pack(str('iL'), space, names.buffer_info()[0])
106    ))[0]
107    namestr = names.tobytes()
108    # return namestr, outbytes
109    return [namestr[i:i + offset].split(b'\0', 1)[0].decode('utf-8') for i in range(0, outbytes, length)]
110
111
112def _getIpAndMac(ifname):
113    ip, mac = _getIpAddr(ifname), _getMacAddr(ifname)
114    return (ip, mac)
115
116
117def getComputerName():
118    '''
119    Returns computer name, with no domain
120    '''
121    return socket.gethostname().split('.')[0]
122
123
124def getNetworkInfo():
125    '''
126    Obtains a list of network interfaces
127    @return: A "generator" of elements, that are dict-as-object, with this elements:
128      name: Name of the interface
129      mac: mac of the interface
130      ip: ip of the interface
131    '''
132    for ifname in _getInterfaces():
133        ip, mac = _getIpAndMac(ifname)
134        if mac != '00:00:00:00:00:00':  # Skips local interfaces
135            yield utils.Bunch(name=ifname, mac=mac, ip=ip)
136
137
138def getDomainName():
139    return ''
140
141
142def getLinuxVersion():
143    """
144    Returns the version of the Linux distribution
145    """
146    return distro.os_release_attr('pretty_name')
147
148
149def reboot(flags=0):
150    '''
151    Simple reboot using os command
152    '''
153    # Workaround for dummy thread
154    if six.PY3 is False:
155        import threading
156        threading._DummyThread._Thread__stop = lambda x: 42
157
158    # Check for OpenGnsys Client or GNU/Linux distribution.
159    if os.path.exists('/scripts/oginit'):
160        subprocess.call('source /opt/opengnsys/etc/preinit/loadenviron.sh; /opt/opengnsys/scripts/reboot', shell=True)
161    else:
162        subprocess.call(['/sbin/reboot'])
163
164
165def poweroff(flags=0):
166    '''
167    Simple poweroff using os command
168    '''
169    # Workaround for dummy thread
170    if six.PY3 is False:
171        import threading
172        threading._DummyThread._Thread__stop = lambda x: 42
173
174    # Check for OpenGnsys Client or GNU/Linux distribution.
175    if os.path.exists('/scripts/oginit'):
176        subprocess.call('source /opt/opengnsys/etc/preinit/loadenviron.sh; /opt/opengnsys/scripts/poweroff', shell=True)
177    else:
178        subprocess.call(['/sbin/poweroff'])
179
180
181def logoff():
182    '''
183    Kills all curent user processes, which must send a logogof
184    caveat: If the user has other sessions, will also disconnect from them
185    '''
186    # Workaround for dummy thread
187    if six.PY3 is False:
188        import threading
189        threading._DummyThread._Thread__stop = lambda x: 42
190
191    subprocess.call(['/usr/bin/pkill', '-u', os.environ['USER']])
192
193
194def renameComputer(newName):
195    rename(newName)
196
197
198def joinDomain(domain, ou, account, password, executeInOneStep=False):
199    pass
200
201
202def changeUserPassword(user, oldPassword, newPassword):
203    '''
204    Simple password change for user using command line
205    '''
206    os.system('echo "{1}\n{1}" | /usr/bin/passwd {0} 2> /dev/null'.format(user, newPassword))
207
208
209class XScreenSaverInfo(ctypes.Structure):
210    _fields_ = [('window', ctypes.c_long),
211                ('state', ctypes.c_int),
212                ('kind', ctypes.c_int),
213                ('til_or_since', ctypes.c_ulong),
214                ('idle', ctypes.c_ulong),
215                ('eventMask', ctypes.c_ulong)]
216
217# Initialize xlib & xss
218try:
219    xlibPath = ctypes.util.find_library('X11')
220    xssPath = ctypes.util.find_library('Xss')
221    xlib = ctypes.cdll.LoadLibrary(xlibPath)
222    xss = ctypes.cdll.LoadLibrary(xssPath)
223
224    # Fix result type to XScreenSaverInfo Structure
225    xss.XScreenSaverQueryExtension.restype = ctypes.c_int
226    xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo)  # Result in a XScreenSaverInfo structure
227except Exception:  # Libraries not accesible, not found or whatever..
228    xlib = xss = None
229
230
231def initIdleDuration(atLeastSeconds):
232    '''
233    On linux we set the screensaver to at least required seconds, or we never will get "idle"
234    '''
235    # Workaround for dummy thread
236    if six.PY3 is False:
237        import threading
238        threading._DummyThread._Thread__stop = lambda x: 42
239
240    subprocess.call(['/usr/bin/xset', 's', '{}'.format(atLeastSeconds + 30)])
241    # And now reset it
242    subprocess.call(['/usr/bin/xset', 's', 'reset'])
243
244
245def getIdleDuration():
246    '''
247    Returns idle duration, in seconds
248    '''
249    if xlib is None or xss is None:
250        return 0  # Libraries not available
251
252    # production code might want to not hardcode the offset 16...
253    display = xlib.XOpenDisplay(None)
254
255    event_base = ctypes.c_int()
256    error_base = ctypes.c_int()
257
258    available = xss.XScreenSaverQueryExtension(display, ctypes.byref(event_base), ctypes.byref(error_base))
259    if available != 1:
260        return 0  # No screen saver is available, no way of getting idle
261
262    info = xss.XScreenSaverAllocInfo()
263    xss.XScreenSaverQueryInfo(display, xlib.XDefaultRootWindow(display), info)
264
265    if info.contents.state != 0:
266        return 3600 * 100 * 1000  # If screen saver is active, return a high enough value
267
268    return info.contents.idle / 1000.0
269
270
271def getCurrentUser():
272    '''
273    Returns current logged in user
274    '''
275    return os.environ['USER']
276
277
278def getSessionLanguage():
279    '''
280    Returns the user's session language
281    '''
282    return locale.getdefaultlocale()[0]
283
284
285def get_session_type():
286    """
287    Returns the user's session type (xrdp, wayland, x11, tty,...)
288    :return: string
289    """
290    return 'xrdp' if 'XRDP_SESSION' in os.environ else os.environ.get('XDG_SESSION_TYPE', 'unknown').lower()
291
292
293def showPopup(title, message):
294    '''
295    Displays a message box on user's session (during 1 min).
296    '''
297    return subprocess.call('zenity --info --timeout 60 --title "{}" --text "{}"'.format(title, message), shell=True)
298
299
300def get_etc_path():
301    """
302    :return:
303    Returns etc directory path.
304    """
305    return os.sep + 'etc'
Note: See TracBrowser for help on using the repository browser.