Advertisement
FlyFar

lpdprint.py

Jul 28th, 2023
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | Cybersecurity | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3.  
  4. ########################################################################
  5. ########################## [ poor man's lpr ] ##########################
  6. ########################################################################
  7.  
  8. # python standard library
  9. import sys, socket, argparse
  10.  
  11. # ----------------------------------------------------------------------
  12.  
  13. def usage():
  14.   parser = argparse.ArgumentParser(description="Line Printer Daemon Protocol (RFC 1179) Print.")
  15.   parser.add_argument("hostname", help="printer ip address or hostname")
  16.   parser.add_argument("filename", help="file to be delivered to printer")
  17.   return parser.parse_args()
  18.  
  19. # ----------------------------------------------------------------------
  20.  
  21. # parse args
  22. args = usage()      
  23.  
  24. # read local file
  25. try:
  26.   with open(args.filename, mode='rb') as f:
  27.     data = f.read()
  28.   f.close()
  29. except IOError as e:
  30.   print "Cannot read from file: " + str(e)
  31.   sys.exit()
  32.  
  33. # ----------------------------------------------------------------------
  34.  
  35. # get lpd acknowledgement
  36. def check_acknowledgement():
  37.   if s.recv(4096) != "\000":
  38.     print "Negative acknowledgement"
  39.     s.send("\001\n") # [ RFC 1179 | 6.1 01 - Abort job ]
  40.     s.close()
  41.     sys.exit()
  42.  
  43. # ----------------------------------------------------------------------
  44.  
  45. # connect to printer
  46. s = socket.socket()
  47. s.connect((args.hostname, 515))
  48.  
  49. # --------[ RFC 1179 | 5.2 02 - Receive a printer job ]-----------------
  50.  
  51. queue = "lp" # see https://www.brooksnet.com/content/faq-lpd-queue-names-network-print-servers
  52. s.send("\002"+queue+"\n")
  53. check_acknowledgement()
  54.  
  55. # --------[ RFC 1179 | 6.2 02 - Receive control file ]------------------
  56.  
  57. hostname = socket.gethostname()
  58. username = "nobody"
  59.  
  60. ctrl = 'H'+hostname[:31]+"\n" \
  61.      + 'P'+username[:31]+"\n"
  62.  
  63. s.send("\002"+str(len(ctrl))+" "+"cfA001"+hostname+"\n")
  64. check_acknowledgement()
  65. s.send(ctrl + "\000") # send control file || file complete indicator
  66. check_acknowledgement()
  67.  
  68. # --------[ RFC 1179 | 6.3 03 - Receive data file ]---------------------
  69.  
  70. s.send("\003"+str(len(data))+" "+"dfA001"+hostname+"\n")
  71. check_acknowledgement()
  72. s.sendall(data + "\000") # send data file || file complete indicator
  73. check_acknowledgement()
  74.  
  75. # ----------------------------------------------------------------------
  76.  
  77. s.close()
Tags: print lpd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement