WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: netcat (nc) acting like a printserver  (Read 4219 times)

Offline angryjack

  • Newbie
  • *
  • Posts: 22
netcat (nc) acting like a printserver
« on: August 19, 2015, 07:51:21 AM »
Hello,

i have PiCore running on a Raspberry 2 and want to use the box to cat like a printserver converting postscript to PCL and sends the output to the target printer. I know that it would be possible to take CUPS instead but i like to do it without.

My idea is that a driver on a PC is sending the postscript output to the RPi2 where a listener is running which picks the file and runs a bash script. The script converts the PS data with ghostscript to PCL and sends the PCL output to the target printer.

The main conversion line is like this:
Code: [Select]
gs -sDEVICE=ljet4 -sOutputFile="output.pcl" -dNOPAUSE "input.ps" -c quitThis is working properly since bmarkus added ghostscript to the PiCore repo...thank him again for this  ;)


I found that netcat (nc) should also be able to act as a "file server" like this:
Code: [Select]
sudo nc -l -p 9100 > input.psNC is then listening on port 9100 and should redirect the output to input.ps but it does not work for me. I configured a driver on a Windoze system to print to the RPi2 but the print file will stay there in the queue and is not send.

Do i misunderstand something the functionality of netcat or do i only make a mistake ? Probably somebody does have a helpful link for me ?
 

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11221
Re: netcat (nc) acting like a printserver
« Reply #1 on: August 19, 2015, 10:40:26 AM »
Hi angryjack
After you send the data, does  input.ps  have anything in it or is it empty? Are you running a firewall on your PI?

Quote
I configured a driver on a Windoze system to print to the RPi2 but the print file will stay there in the queue and is not send.
Did you set the printer mode to RAW and the port number to 9100. Could the Windows firewall be blocking it?
Quote
Do i misunderstand something the functionality of netcat or do i only make a mistake ?
That looks correct, though you shouldn't need sudo.
Quote
Probably somebody does have a helpful link for me ?
You could also do this using  inetd  found in  inetutils-servers.tcz  with the example shown here:
http://unix.stackexchange.com/a/203416

Offline angryjack

  • Newbie
  • *
  • Posts: 22
Re: netcat (nc) acting like a printserver
« Reply #2 on: August 19, 2015, 11:26:59 PM »
seems to be a communication problem between nc and the driver. I start nc and as i initiate a print job via the Windoze driver i get a "no response" message and the driver blocks. As soon as i terminate nc on the RPi2 the driver stops blocking and if i check it the spool file is created on the RPi2. I've checked the file and it is correct. I tried to run the nc process in background with appending a "&" at the end but still the Windoze driver is blocking since i terminate the process on RPi2 side.

I've installed the inetutils_servers but do not know how to configure it as i do only have SSH access and no graphical environment. Basically i would need printing support via JetDirect, port 9100. An example would also be helpful...  ;)
« Last Edit: August 19, 2015, 11:30:18 PM by angryjack »

Offline angryjack

  • Newbie
  • *
  • Posts: 22
Re: netcat (nc) acting like a printserver
« Reply #3 on: August 20, 2015, 04:40:50 AM »
I have now installed inetd with the following entries in inetd.conf:

Code: [Select]
9100   stream tcp nowait cat > /home/tc/printout.prn
ftp    stream tcp nowait root /usr/local/sbin/ftpd    ftpd

FTP deamon is running but i'm not able to print raw data from the WIndoze driver to printout.prn. Any idea ?

P.S: Sorry for double posting but i wasn't able to edit my previous post

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11221
Re: netcat (nc) acting like a printserver
« Reply #4 on: August 20, 2015, 07:47:12 AM »
Hi angryjack
Quote
P.S: Sorry for double posting but i wasn't able to edit my previous post
There's a 30 minute time limit for editing posts, and this is an example of why this is a good policy. Anyone checking for new
posts prior to 07:40:50 AM would have seen Reply #2 as originally entered, but be unaware that it was later edited to add
information since editing does not mark the post as being new or unread.

Apparently Windows insists on talking to the printer even though you requested RAW data be sent. Maybe it would make more
sense to tell Windows to print to a file and FTP it to your PI.

Something else you can try:
Code: [Select]
9100   stream tcp nowait nc -w 5 -l -p 9100 > input.psWhen a connection comes in on port 9100, inetd should launch nc. The  -w 5  will cause nc to terminate after 5 seconds of
inactivity. Of course if Windows happens to pause for 5 seconds to calculate more printer data or to handle another task, your
file will come up short.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11221
Re: netcat (nc) acting like a printserver
« Reply #5 on: August 20, 2015, 09:44:57 AM »
Hi angryjack
Just realized there's an error in my previous post, it should read:
Code: [Select]
9100   stream tcp nowait tc nc -w 5 -l -p 9100 > input.psThe user parameter was missing, as it is in your post, which should have been:
Code: [Select]
9100   stream tcp nowait tc cat > /home/tc/printout.prn

Offline angryjack

  • Newbie
  • *
  • Posts: 22
Re: netcat (nc) acting like a printserver
« Reply #6 on: August 21, 2015, 12:54:17 AM »
Hi Rich,

Quote
Apparently Windows insists on talking to the printer even though you requested RAW data be sent. Maybe it would make more
sense to tell Windows to print to a file and FTP it to your PI.

I had the same thoughts about the communication between host and printer and it seems that there is no way to bypass this stuff on the Windows platform. I know, printing to a file and transfer from there to the PI via FTP could be a solution but that would mean that the client who installs the driver has to mess around with customization and that is what i want to avoid.

I tried both of your examples, in both cases the windows driver blocks and no file is generated on PI side. Actually i'm not 100% sure if the deamon on port 9100 of the PI is really running, how can i test this ?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11221
Re: netcat (nc) acting like a printserver
« Reply #7 on: August 21, 2015, 01:22:08 AM »
Hi angryjack
Quote
Actually i'm not 100% sure if the deamon on port 9100 of the PI is really running, how can i test this ?
SSH into the PI and use nc to send a file to the PIs IP address port 9100 and see if it shows up.

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: netcat (nc) acting like a printserver
« Reply #8 on: August 21, 2015, 01:23:46 AM »
nmap localhost
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline angryjack

  • Newbie
  • *
  • Posts: 22
Re: netcat (nc) acting like a printserver
« Reply #9 on: August 21, 2015, 02:23:24 AM »
Hi,

deamon is running:
Code: [Select]
tc@box:~$ nmap localhost

Starting Nmap 6.47 ( http://nmap.org ) at 2015-08-21 09:14 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0019s latency).
rDNS record for 127.0.0.1: box
Not shown: 997 closed ports
PORT     STATE SERVICE
21/tcp   open  ftp
22/tcp   open  ssh
9100/tcp open  jetdirect

Nmap done: 1 IP address (1 host up) scanned in 0.59 seconds

I've created a file in the home/tc folder called "test.txt" and sent the file using nc like this...
Code: [Select]
cat test.txt | nc 127.0.0.1 9100
My inetd.conf includes:
Code: [Select]
9100    stream tcp nowait tc cat > /home/tc/printout.prn
...but no file printout.prn will be generated  :(


« Last Edit: August 21, 2015, 02:36:55 AM by angryjack »

Offline angryjack

  • Newbie
  • *
  • Posts: 22
Re: netcat (nc) acting like a printserver
« Reply #10 on: August 24, 2015, 02:53:04 AM »
Hello everybody,

any suggestion to my previous posting ? Do i make something wrong ?