postscript printing with netcat - printing

How can I use the netcat program for bidirectional communication with a networked postscript printer? Postscript programs frequently send information to the STDOUT and that is difficult to capture over the network. I need bidirectional communication with the printer 9100 port. My answer is below. Does anybody know of a better way?

The netcat program can be used for bidirectional communication with a postscript printer.
I have been using some simple programs to send print jobs directly to a network postscript printer and haven't gotten any return info from the printer until now. I like to communicate directly with the printer instead of sending jobs to a spooler.
There is a little information a few places about printing with netcat just very sparse so I am making this report.
My printer is a Xerox 6500n connected to a router by ethernet cable. I can send postscript programs or pdf files directly to the printer in a variety of ways. Sometimes I want to get answers from the printer and so have a test program to use the printer to convert the input using 'pathforall' and send back the answer. The netcat does this nicely:
nc -n 192.168.1.111 9100 < pathforall.ps
I then get the desired response. This has been checked and is similar to ghostscript output only slightly off for some reason:
$ nc -v -w 5 -n 192.168.1.111 9100 < pathforall.ps
192.168.1.111 9100 (hp-pdl-datastr) open
28.6998 12.2999 moveto
28.6998 9.29996 28.3998 8.09995 25.2998 6.59998 curveto
24.3998 6.09998 21.2999 4.79999 19.0999 4.79999 curveto
15.3999 4.79999 12.4999 7.89996 12.4999 12.5999 curveto
12.4999 12.7999 lineto
12.4999 16.1999 13.5999 21.7999 28.6998 26.7999 curveto
closepath
$ gs pathforall.ps
GPL Ghostscript 9.50 (2019-10-15)
Copyright (C) 2019 Artifex Software, Inc. All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Loading Times-Roman font from /usr/share/ghostscript/fonts/Times-Roman.pfa... 4824712 3114962 3833824 2546359 1 done.
28.6184 12.2615 moveto
28.6184 9.27632 28.3224 8.07977 25.2262 6.57484 curveto
24.3257 6.08141 21.2418 4.78618 19.0461 4.78618 curveto
15.3577 4.78618 12.4589 7.8824 12.4589 12.5576 curveto
12.4589 12.7673 lineto
12.4589 16.1472 13.5567 21.7352 28.6184 26.7188 curveto
28.6184 12.2615 lineto
closepath
The netcat allows me to communicate with my printer similar to ghostscript except I don't have the executive mode working yet. Also, be sure to include a 'flush' command in the postscript to get the entire answer sent back.
This can be used for many purposes to get information from the printer.
EDIT: Here is a simple test to see if the printer STDOUT is getting back to the computer:
%!
(Hello World\n) print flush
EDIT3: Postscript executive mode works using telnet for the Xerox 6500n as suggested by luser droog. See comments below.

Here is a bash /dev/tcp alternative for printing without netcat.
This sends to a network PostScript printer using bash if built with --enable-net-redirections. This works with my Void bash and with my Xerox Phaser 6500n and switches between PJL and PostScript. Testing results with other printers would be helpful.
#!/bin/bash
#
# ++++ driverless postscript printing ++++
# bash needs --enable-net-redirections build option
#
# for postscript programs with postscript printer only
#
# bash escape is \033 or \e or \E or \x1B
exec 5<>/dev/tcp/192.168.1.111/9100 || exit 1 # change ip as needed
{
echo -e "\E%-12345X#PJL" # PJL entrance
echo #PJL ECHO "$(date)"
echo #PJL ECHO "setting up printer ..."
echo #PJL COMMENT Change Printer Settings
echo #PJL SET COPIES = 1 # modify environment settings
echo #PJL SET MANUALFEED = off
echo #PJL ECHO "Starting PostScript Program ..."
echo #PJL ENTER LANGUAGE = POSTSCRIPT
echo "%!" # optional
cat "$1" || echo -E "(Hello World\n) print flush" # if input.ps missing
echo -e "\004" # ctrl-D for end of file
echo -e "\e%-12345X#PJL" # back to PJL
echo #PJL ECHO "Finished PostScript Program."
echo #PJL RESET # unset modified environment settings
echo #PJL ECHO BYE.
echo -e "\033%-12345X" # PJL universal exit
} >&5 # send to printer
while read -t 122 -r LINE # timeout after 122 seconds of silence
do
echo "$LINE"
if [[ "$LINE" =~ #PJL\ ECHO\ BYE. ]]; then break; fi
done <&5
echo "Finished reading printer"
exec 5>&- # close
exec 5<&- # close both
exit 0
Here is the result in my terminal. Be sure to send a file like "print.sh input.ps" or this will happen:
$ ./print.sh
cat: '': No such file or directory
#PJL ECHO Mon 20 Apr 2020 07:10:03 AM PDT
#PJL ECHO setting up printer ...
#PJL ECHO Starting PostScript Program ...
Hello World
#PJL ECHO Finished PostScript Program.
#PJL ECHO BYE.
Finished reading printer
UPDATE: I have added this here: github

Related

MediaInfo output to stdout

I have used MediaInfo before to extract information in a shell script in CygWin.
#!/bin/bash
IFS=$'\n'; for file in $(ls *.mp3 /.mp3 ); do count="C:/Program Files/MediaInfo/MediaInfo.exe" $file | grep "Bit rate mode" | grep "Variable" | wc -l; if [ $count -gt 0 ]; then echo $file VBR; fi done
For some reason, it no longer outputs to stdout. It displays the data in a window. Is there some command line flag tp force stdout?
You use the Graphical Interface (GUI) version of MediaInfo, you need to use the Command Line Interface (CLI) version of MediaInfo.
See the different download options in the MediaInfo Windows download page.

Formatting nmap output

I have an nmap output looking like this
Nmap scan report for 10.90.108.82
Host is up (0.16s latency).
PORT STATE SERVICE
80/tcp open http
|_http-title: Did not follow redirect to https://10.90.108.82/view/login.html
I would like the output to be like
10.90.108.82 http-title: Did not follow redirect to https://10.90.108.82/view/login.html
How can it be done using grep or any other means?
You can use the following nmap.sh script like that:
<nmap_command> | ./nmap.sh
nmap.sh:
#!/usr/bin/env sh
var="$(cat /dev/stdin)"
file=$(mktemp)
echo "$var" > "$file"
ip_address=$(head -1 "$file" | rev | cut -d ' ' -f1 | rev)
last_line=$(tail -1 "$file" | sed -E "s,^\|_, ,")
printf "%s%s\n" "$ip_address" "$last_line"
rm "$file"
If you do not mind using a programming language, check out this code snippet with Python:
import nmapthon as nm
scanner = nm.NmapScanner('10.90.108.82', ports=[80], arguments='-sS -sV --script http-title')
scanner.run()
if '10.90.108.82' in scanner.scanned_hosts(): # Check if host responded
serv = scanner.service('10.90.108.82', 'tcp', 80)
if serv is not None: # Check if service was identified
print(serv['http-title'])
Do not forget to execute pip3 install nmapthon.
I am the author of the library, feel free to have a look here
Looks like you want an [nmap scan] output to be edited and displayed as you wish. Try bash scripting, code a bash script and run it.
Here's an link to a video where you might find an answer to your problem:
https://youtu.be/lZAoFs75_cs
Watch the video from the Time Stamp 1:27:17 where the creator briefly describes how to cut-short an output and display it as we wish.
If you require, I could code an bash script to execute an cut-shorted version of the output given by an nmap scan.

how to remove zero packets (empty streams) records in wireshark

I am very new to wireshark. in my day to day job i need to remove the packet bytes zero records from captured PCAP file. please help me in this process. attached image is for reference
wireshark packets zero.png
Since you have 47 TCP Streams and 28 that you want to remove, it might be a bit faster to filter for all the TCP streams that you do want to keep since there are only 19 of those.
For the 19 streams you want:
Right-click on the first TCP conversation and choose "Prepare a Filter -> Selected -> A<-->B".
For the next 17 TCP conversations, right-click on each one and choose "Prepare a Filter -> ... And Selected -> A<-->B".
Finally, for the last TCP stream, right-click on the TCP conversation and choose "Apply as Filter -> ... And Selected -> A<-->B".
You may wish to export the resulting filtered packets to a new file via "File -> Export Specified Packets... -> All packets:Displayed" so you won't have to keep filtering for those streams anymore.
If you have a large number of streams to filter, then you are better off scripting something. Here's a script you can use that seems to work well in my testing on my Linux machine. If you're using Windows, you will need to write an equivalent batch file, or you may be able to use it as is if you have Cygwin installed.
#!/bin/sh
# Check usage
if [ ${#} -lt 2 ] ; then
echo "Usage: $0 <infile> <outfile>"
exit 0
fi
infile=${1}
outfile=${2}
# TODO: Could also pass the filter on the command-line too.
filter="ip.dst eq 192.168.10.44 and tcp.len > 0"
stream_filter=
for stream in $(tshark -r ${infile} -Y "${filter}" -T fields -e tcp.stream | sort -u | tr -d '\r')
do
if [[ -z ${stream_filter} ]] ; then
stream_filter="tcp.stream eq ${stream}"
else
stream_filter+=" or tcp.stream eq ${stream}"
fi
done
tshark -r ${infile} -Y "${stream_filter}" -w ${outfile}
echo "Wrote ${outfile}"

Replacement string not working in GNU parallel

I have the script run_md.py which produces the file test.dcd from the input file named test.pdb.
I want to execute the same command on multiple input files (test*.pdb) on a remote server using GNU parallel and transfer the result back to the local computer. Therefore, I'm using the following command:
parallel --trc {.}.dcd -j 2 -S $SERVER1 './run_md.py {} 1000' ::: test*.pdb
The command is running as expected on the server using 2 slots. However, the files are not transferred back and I get the following error:
rsync: link_stat "/home/bougui/{.}.dcd" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1655) [Receiver=3.1.1]
It looks like the replacement string is not working. How can I make it works?
Below is the output of parallel --version:
GNU parallel 20130922
Copyright (C) 2007,2008,2009,2010,2011,2012,2013 Ole Tange and Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
GNU parallel comes with no warranty.
Web site: http://www.gnu.org/software/parallel
When using GNU Parallel for a publication please cite:
O. Tange (2011): GNU Parallel - The Command-Line Power Tool,
;login: The USENIX Magazine, February 2011:42-47.
What you are doing is 100% correct. So something on your system is breaking this. Please try this on another system and if possible follow REPORTING BUGS from man parallel.
The bug reported in that thread has been fixed and this feature works well with the latest version of GNU parallel (20160622). The GNU parallel version 20130922 packaged with Debian 8.5 is buggy for the usage of {.} string replacement, as described below:
With more test I found that the output file must be specified with a replacement string in the command run in parallel.
For testing purpose, you can find below a complete example that others can run:
echo This is input_file > input_file && parallel --trc {}.out -S $SERVER1 cat {} ">"{}.out ::: input_file
The example above works well. When I use the substitution string {.} as below:
echo This is input_file > input_file.in && parallel --trc {.}.out -S $SERVER1 cat {} ">"{.}.out ::: input_file
It works, as well. However, if I didn't specify {.}.out in the command run in parallel as below:
echo This is input_file > input_file.in && parallel --trc {.}.out -S $SERVER1 cat {} ">"input_file.out ::: input_file
... I reproduce the error:
rsync: link_stat "/home/bouvier/{.}.out" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1655) [Receiver=3.1.1]
rsync: [Receiver] write error: Broken pipe (32)
Therefore the output file must be specified in the command run in parallel.

How can I remove duplicates (deduplicate) a mbox format email mailbox?

I've got a mbox mailbox containing duplicate copies of messages, which differ only in their "X-Evolution:" header.
I want to remove the duplicate ones, in as quick and simple a way as possible. It seems like this would have been written already, but I haven't found it, although I've looked at the Python mailbox module, the various perl mbox parsers, formail, and so forth.
Does anyone have any suggestions?
This a small script, which I used for it:
#!/bin/bash
IDCACHE=$(mktemp -p /tmp)
formail -D $((1024*1024*10)) ${IDCACHE} -s
rm ${IDCACHE}
The mailbox needs to be piped through it, and in the meantime it will be deduplicated.
-D $((1024*1024*10)) sets a 10 Mebibyte cache, which is more than 10x the amount needed to deduplicate an entire year of my mail. YMMV, so adjust it accordingly. Setting it too high will cause some performance loss, setting it to low will let it slip duplicates.
formail is part of the procmail utility bundle, mktemp is part of coreutils.
I didn't look at formail (part of procmail) in enough detail. It does have such such an option, as mentioned in places like: http://hints.macworld.com/comment.php?mode=view&cid=115683 and http://us.generation-nt.com/answer/deleting-duplicate-mail-messages-help-172481881.html
'formail -D' and 'reformail -D' can only process one email per execution. Each mail needs to be separated from mbox first before being processed. I use reformail from maildrop instead since it's still in active development.
remove old idcache, tmpmail, nmbox
run dedup.sh .
nmbox is the output with duplicate messages removed.
dedup.sh
#! /bin/sh
# $1 = mbox, thunderbird mailbox
# wmbox.sh is called for each mail.
cat $1 | reformail -s ./wmbox.sh
wmbox.sh
#! /bin/sh
# stdin: a email
# called by dedup.sh
TM=tmpmail
if [ -f $TM ] ; then
echo error!
exit 1
fi
cat > $TM
# mbox format, each mail end with a blank line
echo "" >> $TM
cat $TM | reformail -D 99999999 idcache
# if this mail isn't a dup (reformail return 1 if message-id is not found)
if [ $? != 0 ]; then
# each mail shall have a message-id
if grep -q -i '^message-id:' $TM; then
cat tmpmail >> nmbox
fi
fi
rm $TM

Resources