示例#1
0
 def do_nvram(self, arg):
   # dump nvram
   if arg.startswith('dump'):
     bs = 2**9    # memory block size used for sampling
     max = 2**18  # maximum memory address for sampling
     steps = 2**9 # number of bytes to dump at once (feedback-performance trade-off)
     lpath = os.path.join('nvram', self.basename(self.target)) # local copy of nvram
     #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     # ******* sampling: populate memspace with valid addresses ******
     if len(re.split("\s+", arg, 1)) > 1:
       memspace = []
       commands = ['@PJL RNVRAM ADDRESS=' + str(n) for n in range(0, max, bs)]
       self.chitchat("Sampling memory space (bs=" + str(bs) + ", max=" + str(max) + ")")
       for chunk in (list(chunks(commands, steps))):
         str_recv = self.cmd(c.EOL.join(chunk))
         # break on unsupported printers
         if not str_recv: return
         # collect valid memory addresses
         blocks = re.findall('ADDRESS\s*=\s*(\d+)', str_recv)
         for addr in blocks: memspace += range(conv().int(addr), conv().int(addr) + bs)
         self.chitchat(str(len(blocks)) + " blocks found. ", '')
     else: # use fixed memspace (quick & dirty but might cover interesting stuff)
       memspace = range(0, 8192) + range(32768, 33792) + range(53248, 59648)
     #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     # ******* dumping: read nvram and write copy to local file ******
     commands = ['@PJL RNVRAM ADDRESS=' + str(n) for n in memspace]
     self.chitchat("Writing copy to " + lpath)
     if os.path.isfile(lpath): file().write(lpath, '') # empty file
     for chunk in (list(chunks(commands, steps))):
       str_recv = self.cmd(c.EOL.join(chunk))
       if not str_recv: return # break on unsupported printers
       else: self.makedirs('nvram') # create nvram directory
       data = ''.join([conv().chr(n) for n in re.findall('DATA\s*=\s*(\d+)', str_recv)])
       file().append(lpath, data) # write copy of nvram to disk
       self.logger.dump(data) # print asciified output to screen
     print
   #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   # read nvram (single byte)
   elif arg.startswith('read'):
     arg = re.split("\s+", arg, 1)
     if len(arg) > 1:
       arg, addr = arg
       self.logger.info(self.cmd('@PJL RNVRAM ADDRESS=' + addr))
     else: self.help_nvram()
   #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   # write nvram (single byte)
   elif arg.startswith('write'):
     arg = re.split("\s+", arg, 2)
     if len(arg) > 2:
       arg, addr, data = arg
       self.cmd('@PJL SUPERUSER PASSWORD=0' + c.EOL
              + '@PJL WNVRAM ADDRESS=' + addr + ' DATA=' + data + c.EOL
              + '@PJL SUPERUSEROFF', False)
     else: self.help_nvram()
   else:
     self.help_nvram()
示例#2
0
文件: printer.py 项目: xiju2003/PRET
 def do_print(self, arg):
     'Print image file or raw text:  print <file>|"text"'
     '''
 ┌──────────────────────────────────────────────────────────┐
 │ Poor man's driverless PCL based printing (experimental)  │
 ├──────────────────────────────────────────────────────────┤
 │ Warning: ImageMagick and Ghostscript are used to convert │
 │ the document to be printed into a language understood be │
 │ the printer. Don't print anything from untrusted sources │
 │ as it may be a security risk (CVE-2016–3714, 2016-7976). │
 └──────────────────────────────────────────────────────────┘
 '''
     if not arg: arg = raw_input('File or "text": ')
     if arg.startswith('"'): data = arg.strip('"')  # raw text string
     elif arg.endswith('.ps'): data = file().read(arg)  # postscript file
     else:  # anything else…
         try:
             self.chitchat("Converting '" + arg + "' to PCL")
             pdf = ['-density', '300'] if arg.endswith('.pdf') else []
             cmd = ['convert'
                    ] + pdf + [arg, '-quality', '100', 'pcl' + ':-']
             out, err = subprocess.PIPE, subprocess.PIPE
             p = subprocess.Popen(cmd, stdout=out, stderr=err)
             data, stderr = p.communicate()
         except:
             stderr = "ImageMagick or Ghostscript missing"
         if stderr:
             output().errmsg("Cannot convert", item(stderr.splitlines()))
     if data:
         self.send(c.UEL + data + c.UEL)  # send pcl datastream to printer
示例#3
0
 def do_get(self, arg, lpath="", r=True):
     "Receive file:  get <file>"
     if not arg:
         arg = raw_input("Remote file: ")
     if not lpath:
         lpath = self.basename(arg)
     path = self.rpath(arg) if r else arg
     str_recv = self.get(path)
     if str_recv != c.NONEXISTENT:
         rsize, data = str_recv
         lsize = len(data)
         # write to local file
         file().write(lpath, data)
         if lsize == rsize:
             print(str(lsize) + " bytes received.")
         else:
             self.size_mismatch(rsize, lsize)
示例#4
0
文件: printer.py 项目: xiju2003/PRET
 def do_load(self, arg):
     "Run commands from file:  load cmd.txt"
     if not arg:
         arg = raw_input("File: ")
     data = file().read(arg) or ""
     for cmd in data.splitlines():
         # simulate command prompt
         print(self.prompt + cmd)
         # execute command with premcd
         self.onecmd(self.precmd(cmd))
示例#5
0
文件: printer.py 项目: xiju2003/PRET
 def do_get(self, arg, lpath="", r=True):
     "Receive file:  get <file>"
     if not arg:
         arg = raw_input("Remote file: ")
     if not lpath:
         lpath = self.basename(arg)
     path = self.rpath(arg) if r else arg
     str_recv = self.get(path)
     if str_recv != c.NONEXISTENT:
         rsize, data = str_recv
         lsize = len(data)
         # fix carriage return chars added by some devices
         if lsize != rsize and len(conv().nstrip(data)) == rsize:
             lsize, data = rsize, conv().nstrip(data)
         # write to local file
         file().write(lpath, data)
         if lsize == rsize:
             print(str(lsize) + " bytes received.")
         else:
             self.size_mismatch(rsize, lsize)
示例#6
0
文件: printer.py 项目: xfox64x/PRET
 def do_print(self, arg):
   'Print image file or raw text:  print <file>|"text"'
   '''
   ┌──────────────────────────────────────────────────────────┐
   │ Poor man's driverless printing (PCL based, experimental) │
   └──────────────────────────────────────────────────────────┘
   '''
   if not arg: arg = raw_input('File or "text": ')
   if arg.startswith('"'): data = arg.strip('"')     # raw text string
   elif arg.endswith('.ps'): data = file().read(arg) # postscript file
   else: data = self.convert(arg, 'pcl')             # anything else…
   if data: self.send(c.UEL + data + c.UEL) # send pcl datastream to printer
示例#7
0
文件: printer.py 项目: xiju2003/PRET
 def do_put(self, arg, rpath=""):
     "Send file:  put <local file>"
     if not arg:
         arg = raw_input("Local file: ")
     if not rpath:
         rpath = os.path.basename(arg)
     rpath = self.rpath(rpath)
     lpath = os.path.abspath(arg)
     # read from local file
     data = file().read(lpath)
     if data != None:
         self.put(rpath, data)
         lsize = len(data)
         rsize = self.file_exists(rpath)
         if rsize == lsize:
             print(str(rsize) + " bytes transferred.")
         elif rsize == c.NONEXISTENT:
             print("Permission denied.")
         else:
             self.size_mismatch(lsize, rsize)