From 837ee7e9c2bdd9180be5fb1656b6c5cee6191a56 Mon Sep 17 00:00:00 2001 From: dev Date: Thu, 7 Jul 2022 20:23:31 +0930 Subject: [PATCH 1/3] Upgraded python version to python3. Python2 is no longer supported. --- capabilities.py | 13 ++++++------- helper.py | 2 +- lpd/lpdprint.py | 4 ++-- lpd/lpdtest.py | 16 ++++++++-------- pjl.py | 10 +++++----- postscript.py | 6 +++--- pret.py | 2 +- printer.py | 27 +++++++++++++-------------- requirements.txt | 2 ++ 9 files changed, 41 insertions(+), 41 deletions(-) create mode 100644 requirements.txt diff --git a/capabilities.py b/capabilities.py index b19d5fd..860cf56 100644 --- a/capabilities.py +++ b/capabilities.py @@ -4,10 +4,7 @@ # python standard library import re, os, sys #, urllib.error, urllib.parse -try: - import urllib.request as urllib_request -except ImportError: - import urllib as urllib_request +import requests # local pret classes from helper import output, item @@ -67,9 +64,11 @@ def ipp(self, host, lang): body = ("\x01\x01\x00\x0b\x00\x01\xab\x10\x01G\x00\x12attributes-charset\x00\x05utf-8H" + "\x00\x1battributes-natural-language\x00\x02enE\x00\x0bprinter-uri\x00\x14ipp:" + "//localhost/ipp/D\x00\x14requested-attributes\x00\x13printer-description\x03").encode() - request = urllib_request.Request("http://" + host + ":631/", + + response = requests.post("http://" + host + ":631/", data=body, headers={'Content-type': 'application/ipp'}) - response = urllib_request.urlopen(request, timeout=self.timeout).read().decode() + response = response.text + # get name of device model = item(re.findall("MDL:(.+?);", response)) # e.g. MDL:hp LaserJet 4250 # get language support @@ -86,7 +85,7 @@ def http(self, host): try: # poor man's way get http title sys.stdout.write("Checking for HTTP support: ") - html = urllib_request.urlopen("http://" + host, timeout=self.timeout).read().decode() + html = requests.get("http://" + host).text # cause we are to parsimonious to import BeautifulSoup ;) title = re.findall("\n?(.+?)\n?", html, re.I|re.M|re.S) # get name of device diff --git a/helper.py b/helper.py index 2169aad..19ba8fd 100644 --- a/helper.py +++ b/helper.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from __future__ import print_function + # python standard library from socket import socket diff --git a/lpd/lpdprint.py b/lpd/lpdprint.py index 30ac2e4..e4ac267 100755 --- a/lpd/lpdprint.py +++ b/lpd/lpdprint.py @@ -27,7 +27,7 @@ def usage(): data = f.read() f.close() except IOError as e: - print "Cannot read from file: " + str(e) + print(("Cannot read from file: " + str(e))) sys.exit() # ---------------------------------------------------------------------- @@ -35,7 +35,7 @@ def usage(): # get lpd acknowledgement def check_acknowledgement(): if s.recv(4096) != "\000": - print "Negative acknowledgement" + print("Negative acknowledgement") s.send("\001\n") # [ RFC 1179 | 6.1 01 - Abort job ] s.close() sys.exit() diff --git a/lpd/lpdtest.py b/lpd/lpdtest.py index 69afd43..8f2ffa7 100755 --- a/lpd/lpdtest.py +++ b/lpd/lpdtest.py @@ -108,34 +108,34 @@ def getFilequeueList(aFilename): if args.mode == 'get': getname = args.argument data += " If you can read this, the test failed." - print("[get] Trying to print file " + args.argument) + print(("[get] Trying to print file " + args.argument)) elif args.mode == 'put': ctrlfile = args.argument datafile = args.argument - print("[put] Trying to write to file " + args.argument) + print(("[put] Trying to write to file " + args.argument)) elif args.mode == 'rm': delname = args.argument - print("[rm] Trying to delete file " + args.argument) + print(("[rm] Trying to delete file " + args.argument)) elif args.mode == 'in': hostname = username = ctrlfile = datafile = delname = jobname \ = srcfile = jobtitle = classname = mailname = args.argument - print("[in] Trying to send user input '" + args.argument + "'") + print(("[in] Trying to send user input '" + args.argument + "'")) elif args.mode == 'mail': try: mailname = args.argument.split('@', 1)[0] hostname = args.argument.split('@', 1)[1] except Exception as e: - print("Bad argument" + str(e)) + print(("Bad argument" + str(e))) sys.exit() - print("[mail] Trying to send job information to " + args.argument) + print(("[mail] Trying to send job information to " + args.argument)) elif args.mode == 'brute': bruteQueueNameFile = args.argument - print("[brute] Trying to brute force queue file " + args.argument) + print(("[brute] Trying to brute force queue file " + args.argument)) # --------[ RFC 1179 | 6.2 02 - Receive control file ]------------------ @@ -174,7 +174,7 @@ def getFilequeueList(aFilename): continue else : - print("Printer found:",queueName) + print(("Printer found:",queueName)) break diff --git a/pjl.py b/pjl.py index 1256a1b..3b03838 100644 --- a/pjl.py +++ b/pjl.py @@ -203,7 +203,7 @@ def do_ls(self, arg): def do_mkdir(self, arg): "Create remote directory: mkdir " if not arg: - arg = eval(input("Directory: ")) + arg = (input("Directory: ")) path = self.rpath(arg) self.cmd('@PJL FSMKDIR NAME="' + path + '"', False) @@ -359,7 +359,7 @@ def complete_printenv(self, text, line, begidx, endidx): def do_set(self, arg, fb=True): "Set printer environment variable: set " if not arg: - arg = eval(input("Set variable (VAR=VALUE): ")) + arg = (input("Set variable (VAR=VALUE): ")) self.cmd('@PJL SET SERVICEMODE=HPBOISEID' + c.EOL + '@PJL DEFAULT ' + arg + c.EOL + '@PJL SET ' + arg + c.EOL @@ -393,7 +393,7 @@ def do_pagecount(self, arg): def do_display(self, arg): "Set printer's display message: display " if not arg: - arg = eval(input("Message: ")) + arg = input("Message: ") arg = arg.strip('"') # remove quotes self.chitchat("Setting printer's display message to \"" + arg + "\"") self.cmd('@PJL RDYMSG DISPLAY="' + arg + '"', False) @@ -402,7 +402,7 @@ def do_display(self, arg): def do_offline(self, arg): "Take printer offline and display message: offline " if not arg: - arg = eval(input("Offline display message: ")) + arg = (input("Offline display message: ")) arg = arg.strip('"') # remove quotes output().warning("Warning: Taking the printer offline will prevent yourself and others") output().warning("from printing or re-connecting to the device. Press CTRL+C to abort.") @@ -618,7 +618,7 @@ def complete_nvram(self, text, line, begidx, endidx): def do_lock(self, arg): "Lock control panel settings and disk write access." if not arg: - arg = eval(input("Enter PIN (1..65535): ")) + arg = (input("Enter PIN (1..65535): ")) self.cmd('@PJL DEFAULT PASSWORD=' + arg + c.EOL + '@PJL DEFAULT CPLOCK=ON' + c.EOL + '@PJL DEFAULT DISKLOCK=ON', False) diff --git a/postscript.py b/postscript.py index 057cc2d..950a979 100644 --- a/postscript.py +++ b/postscript.py @@ -194,7 +194,7 @@ def do_mirror(self, arg): def do_mkdir(self, arg): "Create remote directory: mkdir " if not arg: - arg = eval(input("Directory: ")) + arg = (input("Directory: ")) # writing to dir/file should automatically create dir/ # .dirfile is not deleted as empty dirs are not listed self.put(self.rpath(arg) + c.SEP + '.dirfile', '') @@ -369,7 +369,7 @@ def do_pagecount(self, arg): def do_lock(self, arg): "Set startjob and system parameters password." if not arg: - arg = eval(input("Enter password: ")) + arg = (input("Enter password: ")) self.cmd('<< /Password () ' '/SystemParamsPassword (' + arg + ') ' # harmless settings '/StartJobPassword (' + arg + ') ' # alter initial vm! @@ -504,7 +504,7 @@ def do_hang(self, arg): # ------------------------[ overlay ]-------------------------- def do_overlay(self, arg): "Put overlay image on all hard copies: overlay " - if not arg: arg = eval(input('File: ')) + if not arg: arg = (input('File: ')) if arg.endswith('ps'): data = file().read(arg) # already ps/eps file else: self.chitchat("For best results use a file from the overlays/ directory") diff --git a/pret.py b/pret.py index dec5493..b7994ae 100644 --- a/pret.py +++ b/pret.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from __future__ import print_function + # python standard library import os, sys, argparse diff --git a/printer.py b/printer.py index e2153f7..ca57f4e 100644 --- a/printer.py +++ b/printer.py @@ -104,10 +104,9 @@ def precmd(self, line): # -------------------------------------------------------------------- # catch-all wrapper to guarantee continuation on unhandled exceptions def onecmd(self, line): - try: - cmd.Cmd.onecmd(self, line) - except Exception as e: - output().errmsg("Program Error", e) + + cmd.Cmd.onecmd(self, line) + # ==================================================================== @@ -139,7 +138,7 @@ def do_debug(self, arg): def do_load(self, arg): "Run commands from file: load cmd.txt" if not arg: - arg = eval(input("File: ")) + arg = (input("File: ")) data = file().read(arg).decode() or "" for cmd in data.splitlines(): # simulate command prompt @@ -170,7 +169,7 @@ def do_discover(self, arg): def do_open(self, arg, mode=""): "Connect to remote device: open " if not arg: - arg = eval(input("Target: ")) + arg = (input("Target: ")) # open connection try: newtarget = (arg != self.target) @@ -265,7 +264,7 @@ def do_pwd(self, arg): def do_chvol(self, arg): "Change remote volume: chvol " if not arg: - arg = eval(input("Volume: ")) + arg = (input("Volume: ")) if arg and self.vol_exists(arg): if self.mode == 'ps': self.set_vol('%' + arg.strip('%') + '%') if self.mode == 'pjl': self.set_vol(arg[0] + ':' + c.SEP) @@ -396,7 +395,7 @@ def basename(self, path): def do_get(self, arg, lpath="", r=True): "Receive file: get " if not arg: - arg = eval(input("Remote file: ")) + arg = (input("Remote file: ")) if not lpath: lpath = self.basename(arg) path = self.rpath(arg) if r else arg @@ -422,7 +421,7 @@ def size_mismatch(self, size1, size2): def do_put(self, arg, rpath=""): "Send file: put " if not arg: - arg = eval(input("Local file: ")) + arg = (input("Local file: ")) if not rpath: rpath = os.path.basename(arg) rpath = self.rpath(rpath) @@ -456,14 +455,14 @@ def do_append(self, arg): def do_touch(self, arg): "Update file timestamps: touch " if not arg: - arg = eval(input("Remote file: ")) + arg = (input("Remote file: ")) rpath = self.rpath(arg) self.append(rpath, '') # ------------------------[ delete ]--------------------------- def do_delete(self, arg): if not arg: - arg = eval(input("File: ")) + arg = (input("File: ")) self.delete(arg) # define alias but do not show alias in help @@ -476,7 +475,7 @@ def help_delete(self): def do_cat(self, arg): "Output remote file to stdout: cat " if not arg: - arg = eval(input("Remote file: ")) + arg = (input("Remote file: ")) path = self.rpath(arg) str_recv = self.get(path) if str_recv != c.NONEXISTENT: @@ -729,7 +728,7 @@ def complete_fuzz(self, text, line, begidx, endidx): def do_site(self, arg): "Execute custom command on printer: site " if not arg: - arg = eval(input("Command: ")) + arg = (input("Command: ")) str_recv = self.cmd(arg) output().info(str_recv) @@ -741,7 +740,7 @@ def do_print(self, arg): │ Poor man's driverless printing (PCL based, experimental) │ └──────────────────────────────────────────────────────────┘ ''' - if not arg: arg = eval(input('File or "text": ')) + if not arg: arg = (input('File or "text": ')) if arg.startswith('"'): data = arg.strip('"') # raw text string elif arg.endswith('.ps'): data = file().read(arg).decode() # postscript file else: data = self.convert(arg, 'pcl') # anything else… diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2cc3582 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +snmp +colorama \ No newline at end of file From 4f41e4959382142b11dcc543fcb16da27bef0356 Mon Sep 17 00:00:00 2001 From: dev Date: Thu, 7 Jul 2022 20:28:16 +0930 Subject: [PATCH 2/3] some more --- README.md | 6 +++--- pret.py | 2 +- requirements.txt | 2 -- 3 files changed, 4 insertions(+), 6 deletions(-) delete mode 100644 requirements.txt diff --git a/README.md b/README.md index 634eb4b..93b7a23 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,13 @@ The main idea of PRET is to facilitate the communication between the end-user an ### Installation -PRET only requires a Python2 interpreter. For colored output and SNMP support however, third party modules need to be installed: +PRET only requires a Python3 interpreter. For colored output and SNMP support however, third party modules need to be installed: - # pip install colorama pysnmp + # pip3 install colorama pysnmp If running on a Windows console and Unicode characters are not displayed correctly, install the *win_unicode_console* module: - # pip install win_unicode_console + # pip3 install win_unicode_console For experimental, ‘driverless’ printing (see print command), ImageMagick and GhostScript need to be installed: diff --git a/pret.py b/pret.py index b7994ae..5d4dab6 100644 --- a/pret.py +++ b/pret.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 2cc3582..0000000 --- a/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -snmp -colorama \ No newline at end of file From 1335b148f4f28b6802beeaa275459c9562168a1d Mon Sep 17 00:00:00 2001 From: InsaneMiner Date: Thu, 7 Jul 2022 20:33:38 +0930 Subject: [PATCH 3/3] forgot to add some code back --- pjl.py | 8 ++++---- postscript.py | 6 +++--- printer.py | 27 ++++++++++++++------------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/pjl.py b/pjl.py index 3b03838..4857de2 100644 --- a/pjl.py +++ b/pjl.py @@ -203,7 +203,7 @@ def do_ls(self, arg): def do_mkdir(self, arg): "Create remote directory: mkdir " if not arg: - arg = (input("Directory: ")) + arg = eval(input("Directory: ")) path = self.rpath(arg) self.cmd('@PJL FSMKDIR NAME="' + path + '"', False) @@ -359,7 +359,7 @@ def complete_printenv(self, text, line, begidx, endidx): def do_set(self, arg, fb=True): "Set printer environment variable: set " if not arg: - arg = (input("Set variable (VAR=VALUE): ")) + arg = eval(input("Set variable (VAR=VALUE): ")) self.cmd('@PJL SET SERVICEMODE=HPBOISEID' + c.EOL + '@PJL DEFAULT ' + arg + c.EOL + '@PJL SET ' + arg + c.EOL @@ -402,7 +402,7 @@ def do_display(self, arg): def do_offline(self, arg): "Take printer offline and display message: offline " if not arg: - arg = (input("Offline display message: ")) + arg = eval(input("Offline display message: ")) arg = arg.strip('"') # remove quotes output().warning("Warning: Taking the printer offline will prevent yourself and others") output().warning("from printing or re-connecting to the device. Press CTRL+C to abort.") @@ -618,7 +618,7 @@ def complete_nvram(self, text, line, begidx, endidx): def do_lock(self, arg): "Lock control panel settings and disk write access." if not arg: - arg = (input("Enter PIN (1..65535): ")) + arg = eval(input("Enter PIN (1..65535): ")) self.cmd('@PJL DEFAULT PASSWORD=' + arg + c.EOL + '@PJL DEFAULT CPLOCK=ON' + c.EOL + '@PJL DEFAULT DISKLOCK=ON', False) diff --git a/postscript.py b/postscript.py index 950a979..057cc2d 100644 --- a/postscript.py +++ b/postscript.py @@ -194,7 +194,7 @@ def do_mirror(self, arg): def do_mkdir(self, arg): "Create remote directory: mkdir " if not arg: - arg = (input("Directory: ")) + arg = eval(input("Directory: ")) # writing to dir/file should automatically create dir/ # .dirfile is not deleted as empty dirs are not listed self.put(self.rpath(arg) + c.SEP + '.dirfile', '') @@ -369,7 +369,7 @@ def do_pagecount(self, arg): def do_lock(self, arg): "Set startjob and system parameters password." if not arg: - arg = (input("Enter password: ")) + arg = eval(input("Enter password: ")) self.cmd('<< /Password () ' '/SystemParamsPassword (' + arg + ') ' # harmless settings '/StartJobPassword (' + arg + ') ' # alter initial vm! @@ -504,7 +504,7 @@ def do_hang(self, arg): # ------------------------[ overlay ]-------------------------- def do_overlay(self, arg): "Put overlay image on all hard copies: overlay " - if not arg: arg = (input('File: ')) + if not arg: arg = eval(input('File: ')) if arg.endswith('ps'): data = file().read(arg) # already ps/eps file else: self.chitchat("For best results use a file from the overlays/ directory") diff --git a/printer.py b/printer.py index ca57f4e..e2153f7 100644 --- a/printer.py +++ b/printer.py @@ -104,9 +104,10 @@ def precmd(self, line): # -------------------------------------------------------------------- # catch-all wrapper to guarantee continuation on unhandled exceptions def onecmd(self, line): - - cmd.Cmd.onecmd(self, line) - + try: + cmd.Cmd.onecmd(self, line) + except Exception as e: + output().errmsg("Program Error", e) # ==================================================================== @@ -138,7 +139,7 @@ def do_debug(self, arg): def do_load(self, arg): "Run commands from file: load cmd.txt" if not arg: - arg = (input("File: ")) + arg = eval(input("File: ")) data = file().read(arg).decode() or "" for cmd in data.splitlines(): # simulate command prompt @@ -169,7 +170,7 @@ def do_discover(self, arg): def do_open(self, arg, mode=""): "Connect to remote device: open " if not arg: - arg = (input("Target: ")) + arg = eval(input("Target: ")) # open connection try: newtarget = (arg != self.target) @@ -264,7 +265,7 @@ def do_pwd(self, arg): def do_chvol(self, arg): "Change remote volume: chvol " if not arg: - arg = (input("Volume: ")) + arg = eval(input("Volume: ")) if arg and self.vol_exists(arg): if self.mode == 'ps': self.set_vol('%' + arg.strip('%') + '%') if self.mode == 'pjl': self.set_vol(arg[0] + ':' + c.SEP) @@ -395,7 +396,7 @@ def basename(self, path): def do_get(self, arg, lpath="", r=True): "Receive file: get " if not arg: - arg = (input("Remote file: ")) + arg = eval(input("Remote file: ")) if not lpath: lpath = self.basename(arg) path = self.rpath(arg) if r else arg @@ -421,7 +422,7 @@ def size_mismatch(self, size1, size2): def do_put(self, arg, rpath=""): "Send file: put " if not arg: - arg = (input("Local file: ")) + arg = eval(input("Local file: ")) if not rpath: rpath = os.path.basename(arg) rpath = self.rpath(rpath) @@ -455,14 +456,14 @@ def do_append(self, arg): def do_touch(self, arg): "Update file timestamps: touch " if not arg: - arg = (input("Remote file: ")) + arg = eval(input("Remote file: ")) rpath = self.rpath(arg) self.append(rpath, '') # ------------------------[ delete ]--------------------------- def do_delete(self, arg): if not arg: - arg = (input("File: ")) + arg = eval(input("File: ")) self.delete(arg) # define alias but do not show alias in help @@ -475,7 +476,7 @@ def help_delete(self): def do_cat(self, arg): "Output remote file to stdout: cat " if not arg: - arg = (input("Remote file: ")) + arg = eval(input("Remote file: ")) path = self.rpath(arg) str_recv = self.get(path) if str_recv != c.NONEXISTENT: @@ -728,7 +729,7 @@ def complete_fuzz(self, text, line, begidx, endidx): def do_site(self, arg): "Execute custom command on printer: site " if not arg: - arg = (input("Command: ")) + arg = eval(input("Command: ")) str_recv = self.cmd(arg) output().info(str_recv) @@ -740,7 +741,7 @@ def do_print(self, arg): │ Poor man's driverless printing (PCL based, experimental) │ └──────────────────────────────────────────────────────────┘ ''' - if not arg: arg = (input('File or "text": ')) + if not arg: arg = eval(input('File or "text": ')) if arg.startswith('"'): data = arg.strip('"') # raw text string elif arg.endswith('.ps'): data = file().read(arg).decode() # postscript file else: data = self.convert(arg, 'pcl') # anything else…