Description: Convert scripts to python3 Author: Valentin Vidic Last-Update: 2019-06-26 --- This patch header follows DEP-3: http://dep.debian.net/deps/dep3/ --- a/lib/plugins/stonith/external/dracmc-telnet +++ b/lib/plugins/stonith/external/dracmc-telnet @@ -75,7 +75,7 @@ def _get_timestamp(self): ct = time.time() - msecs = (ct - long(ct)) * 1000 + msecs = (ct - int(ct)) * 1000 return "%s,%03d" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ct)), msecs) @@ -93,48 +93,48 @@ def login(self, user, passwd): time.sleep(0.3) try: - line = self.read_until('Login: ', self._timeout) - self.write(user) - self.write('\r') - line = self.read_until('Password: ', self._timeout) - self.write(passwd) - self.write('\r') + line = self.read_until(b'Login: ', self._timeout) + self.write(user.encode("utf-8")) + self.write(b'\r') + line = self.read_until(b'Password: ', self._timeout) + self.write(passwd.encode("utf-8")) + self.write(b'\r') except: - self.write("\r") - line = self.read_until('Login: ', self._timeout) - self.write(user) - self.write('\r') - line = self.read_until('Password: ', self._timeout) - self.write(passwd) - self.write('\r') + self.write(b"\r") + line = self.read_until(b'Login: ', self._timeout) + self.write(user.encode("utf-8")) + self.write(b'\r') + line = self.read_until(b'Password: ', self._timeout) + self.write(passwd.encode("utf-8")) + self.write('b\r') try: - line = self.read_until('DRAC/MC:', self._timeout) + line = self.read_until(b'DRAC/MC:', self._timeout) except: - self.write("\r") - line = self.read_until('DRAC/MC:', self._timeout) + self.write(b"\r") + line = self.read_until(b'DRAC/MC:', self._timeout) def hardreset(self): - self.write('serveraction -s %s hardreset\r' % self._server) - line = self.read_until('OK', 10) - line = self.read_until('DRAC/MC:', self._timeout) + self.write(b'serveraction -s %b hardreset\r' % self._server.encode("utf-8")) + line = self.read_until(b'OK', 10) + line = self.read_until(b'DRAC/MC:', self._timeout) def powercycle(self): - self.write('serveraction -s %s powercycle\r' % self._server) - line = self.read_until('OK', 10) - line = self.read_until('DRAC/MC:', self._timeout) + self.write(b'serveraction -s %b powercycle\r' % self._server.encode("utf-8")) + line = self.read_until(b'OK', 10) + line = self.read_until(b'DRAC/MC:', self._timeout) def on(self): - self.write('serveraction -s %s powerup\r' % self._server) - line = self.read_until('OK', 10) - line = self.read_until('DRAC/MC:', self._timeout) + self.write(b'serveraction -s %b powerup\r' % self._server.encode("utf-8")) + line = self.read_until(b'OK', 10) + line = self.read_until(b'DRAC/MC:', self._timeout) def off(self): - self.write('serveraction -s %s powerdown\r' % self._server) - line = self.read_until('OK', 10) - line = self.read_until('DRAC/MC:', self._timeout) + self.write(b'serveraction -s %b powerdown\r' % self._server.encode("utf-8")) + line = self.read_until(b'OK', 10) + line = self.read_until(b'DRAC/MC:', self._timeout) def exit(self): - self.write('exit\r') + self.write(b'exit\r') def get_history(self): return "\n".join(self._history) @@ -171,7 +171,7 @@ def _get_timestamp(self): ct = time.time() - msecs = (ct - long(ct)) * 1000 + msecs = (ct - int(ct)) * 1000 return "%s,%03d" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ct)), msecs) @@ -201,7 +201,7 @@ self._parameters['cyclades_port']) c.login(self._parameters['username'], self._parameters['password']) - except Exception, args: + except Exception as args: if "Connection reset by peer" in str(args): self._echo_debug("Someone is already logged in... retry=%s" % tries) c.close() @@ -363,7 +363,7 @@ func = getattr(self, cmd, self.not_implemented) rc = func() return(rc) - except Exception, args: + except Exception as args: self.echo_log("err", 'Exception raised:', str(args)) if self._connection: self.echo_log("err", self._connection.get_history()) --- a/lib/plugins/stonith/external/ibmrsa-telnet +++ b/lib/plugins/stonith/external/ibmrsa-telnet @@ -72,7 +72,7 @@ def _get_timestamp(self): ct = time.time() - msecs = (ct - long(ct)) * 1000 + msecs = (ct - int(ct)) * 1000 return "%s,%03d" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ct)), msecs) @@ -90,31 +90,31 @@ def login(self, user, passwd): time.sleep(1) - line = self.expect(['\nlogin : ', '\nusername: '], self._timeout) - self.write(user) - self.write('\r') - line = self.expect(['\nPassword: ', '\npassword: '], self._timeout) - self.write(passwd, nolog = True) - self.write('\r') - line = self.expect(['\nsystem>', '> '], self._timeout) + line = self.expect([b'\nlogin : ', b'\nusername: '], self._timeout) + self.write(user.encode("utf-8")) + self.write(b'\r') + line = self.expect([b'\nPassword: ', b'\npassword: '], self._timeout) + self.write(passwd.encode("utf-8"), nolog = True) + self.write(b'\r') + line = self.expect([b'\nsystem>', b'> '], self._timeout) def reset(self): - self.write('power cycle\r') - line = self.expect(['\nok'], self._timeout) - line = self.expect(['\nsystem>', '> '], self._timeout) + self.write(b'power cycle\r') + line = self.expect([b'\nok'], self._timeout) + line = self.expect([b'\nsystem>', b'> '], self._timeout) def on(self): - self.write('power on\r') - line = self.expect(['\nok'], self._timeout) - line = self.expect(['\nsystem>', '> '], self._timeout) + self.write(b'power on\r') + line = self.expect([b'\nok'], self._timeout) + line = self.expect([b'\nsystem>', b'> '], self._timeout) def off(self): - self.write('power off\r') - line = self.expect(['\nok'], self._timeout) - line = self.expect(['\nsystem>', '> '], self._timeout) + self.write(b'power off\r') + line = self.expect([b'\nok'], self._timeout) + line = self.expect([b'\nsystem>', b'> '], self._timeout) def exit(self): - self.write('exit\r') + self.write(b'exit\r') def get_history(self): return "\n".join(self._history) @@ -150,7 +150,7 @@ def _get_timestamp(self): ct = time.time() - msecs = (ct - long(ct)) * 1000 + msecs = (ct - int(ct)) * 1000 return "%s,%03d" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ct)), msecs) @@ -306,7 +306,7 @@ func = getattr(self, cmd, self.not_implemented) rc = func() return(rc) - except Exception, args: + except Exception as args: self.echo_log("err", 'Exception raised:', str(args)) if self._connection: self.echo_log("err", self._connection.get_history()) --- a/lib/plugins/stonith/ribcl.py.in +++ b/lib/plugins/stonith/ribcl.py.in @@ -19,7 +19,7 @@ import sys import socket -from httplib import * +from http.client import * from time import sleep @@ -30,30 +30,30 @@ host = argv[1].split('.')[0]+'-rm' cmd = argv[2] except IndexError: - print "Not enough arguments" + print("Not enough arguments") sys.exit(1) -login = [ '', - '' ] +login = [ b'', + b'' ] -logout = [ '', '' ] +logout = [ b'', b'' ] -status = [ '', '', - '' ] +status = [ b'', b'', + b'' ] -reset = [ '', '', '' ] +reset = [ b'', b'', b'' ] -off = [ '', '', - '' ] +off = [ b'', b'', + b'' ] -on = [ '', '', - '' ] +on = [ b'', b'', + b'' ] todo = { 'reset':reset, 'on':on, 'off':off, 'status':status } @@ -67,7 +67,7 @@ else: acmds.append(login + todo[cmd] + logout) except KeyError: - print "Invalid command: "+ cmd + print("Invalid command: "+ cmd) sys.exit(1) @@ -76,12 +76,12 @@ c=HTTPSConnection(host) - c.send('\r\n') + c.send(b'\r\n') c.sock.recv(1024) for line in cmds: - c.send(line+'\r\n') + c.send(line+b'\r\n') c.sock.recv(1024) @@ -89,13 +89,9 @@ sleep(1) -except socket.gaierror, msg: - print msg +except socket.gaierror as msg: + print(msg) sys.exit(1) -except socket.sslerror, msg: - print msg +except socket.error as msg: + print(msg) sys.exit(1) -except socket.error, msg: - print msg - sys.exit(1) - --- a/lib/plugins/stonith/external/riloe +++ b/lib/plugins/stonith/external/riloe @@ -36,7 +36,7 @@ import socket import subprocess import xml.dom.minidom -import httplib +import http.client import time import re @@ -164,12 +164,12 @@ } if cmd in info: - print info[cmd] + print(info[cmd]) sys.exit(0) if cmd == 'getconfignames': for arg in [ "hostlist", "ilo_hostname", "ilo_user", "ilo_password", "ilo_can_reset", "ilo_protocol", "ilo_powerdown_method", "ilo_proxyhost", "ilo_proxyport"]: - print arg + print(arg) sys.exit(0) if not rihost: @@ -258,7 +258,7 @@ ''' msg = "" str_status = "" - for attr in node.attributes.keys(): + for attr in list(node.attributes.keys()): if attr == A_STATUS: str_status = node.getAttribute(attr) elif attr == A_MSG: @@ -286,7 +286,7 @@ variable correspondingly. ''' global power - for attr in node.attributes.keys(): + for attr in list(node.attributes.keys()): if attr == A_POWER_STATE: power_state = node.getAttribute(attr).upper() else: @@ -333,25 +333,23 @@ proxy_connect='CONNECT %s:%s HTTP/1.1\r\n'%(host,443) user_agent='User-Agent: python\r\n' proxy_pieces=proxy_connect+user_agent+'\r\n' - proxy.sendall(proxy_pieces) - response=proxy.recv(8192) + proxy.sendall(proxy_pieces.encode("utf-8")) + response=proxy.recv(8192).decode("utf-8") status=response.split()[1] if status!=str(200): fatal("Error status=: %s" %(response)) import ssl sock = ssl.wrap_socket(proxy) - h=httplib.HTTPConnection('localhost') + h=http.client.HTTPConnection('localhost') h.sock=sock return h else: - return httplib.HTTPSConnection(host) - except socket.gaierror, msg: + return http.client.HTTPSConnection(host) + except socket.gaierror as msg: fatal("%s: %s" %(msg,host)) - except socket.sslerror, msg: - fatal("%s for %s" %(msg,host)) - except socket.error, msg: + except socket.error as msg: fatal("%s while talking to %s" %(msg,host)) - except ImportError, msg: + except ImportError as msg: fatal("ssl support missing (%s)" %msg) def send_request(req,proc_f): @@ -364,8 +362,8 @@ t_begin = time.time() c = open_ilo(rihost) try: - c.send(req+'\r\n') - except socket.error, msg: + c.send(req.encode("utf-8") + b'\r\n') + except socket.error as msg: fatal("%s, while talking to %s" %(msg,rihost)) t_end = time.time() my_debug("request sent in %0.2f s" % ((t_end-t_begin))) @@ -378,7 +376,7 @@ if not reply: break result.append(reply) - except socket.error, msg: + except socket.error as msg: if msg[0] == 6: # connection closed break my_err("%s, while talking to %s" %(msg,rihost)) @@ -389,12 +387,13 @@ if not result: fatal("no response from %s within %0.2f s"%(rihost,(t_end-t_begin))) for reply in result: + reply = reply.decode("utf-8") # work around the iLO bug, i.e. element RIBCL closed twice if re.search("", reply): reply = re.sub("<(RIBCL.*)/>", r"<\1>", reply) try: doc = xml.dom.minidom.parseString(reply) - except xml.parsers.expat.ExpatError,msg: + except xml.parsers.expat.ExpatError as msg: fatal("malformed response: %s\n%s"%(msg,reply)) rc = proc_f(doc) doc.unlink()