Thermal printer ESC/POS status command result to BASH variable - bash

I am trying to create a script that will check if the serial connection is functional on the thin client running strip down version of ubuntu.
I am able to print to the printer, however, I need to find a way to read settings from the printer connected with RS232 (ttyS0).
here is the BASH script I have so far:
#!/bin/bash
# Gather information
ip4=$(/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1)
locdate=$(date)
drstatus="To be implemented yet"
#[Set serial port speed]
stty -F /dev/ttyS0 38400;
#listen to the port report
# cat /dev/ttyS0&
#[Send print test to the printer]
#initialize printer
echo -e '\x1b\x40'>/dev/ttyS0
# send printer data
echo -e '\x1b\x45 TM88 Hardware Test:\x1b\x46\xa'>/dev/ttyS0
echo -e $locdate'' >/dev/ttyS0
echo -e 'Hostname: '$HOSTNAME'' >/dev/ttyS0
echo -e 'Endpoint IP address: '$ip4''>/dev/ttyS0
echo -e '\x1d\x6b\x04000\x00'>/dev/ttyS0
echo -e 'Printer connected to COM1 / ttyS0'>/dev/ttyS0
echo -e 'Comm. speed 38400 bps'>/dev/ttyS0
echo -e 'Cash drawer status:'$drstatus'\xa'>/dev/ttyS0
echo -e '\xa\xa\xa\xa\xa\xa\x1bm'>/dev/ttyS0
According to the reference site the code should look like this for the request should look like this:
echo -e '\x1d\x49\x01'>/dev/ttyS0
and the response should be visible with
cat < /dev/ttyS0
However cat does not display anything.
How can I get the response from the ESC/POS command into a variable?

Related

Bash using cut to separate an IP and Port

I am trying to pass an ip and port to my bash script from a list of devices but the script is reading it as multiple devices instead of port. So in the example from below it's trying to telnet to 4 devices as it's reading the ports as a device.
for device in `cat device-list.txt`;
do
hostname=$(echo $device | cut -d : -f 1)
port=$(echo $port | cut -d : -f 2)
./script.exp $device $username $password $port ;
done
I am trying to use cut to take the port and pass it through as a variable so my telnet should be e.g. abc.abc.com 30040 as one device and so on.
# Telnet
spawn telnet $hostname $port
This is my list of devices
abc.abc.com 30040
abc.abc.com 30041
I have tried searching this site already for answers.
I see two errors (lines 4 & 5). It should be
for device in `cat device-list.txt`;
do
hostname=$(echo $device | cut -d : -f 1)
port=$(echo $device | cut -d : -f 2)
./script.exp $hostname $username $password $port ;
done
You can use the Bash built-in read function to extract hostname and port from the lines in a loop:
while read -r hostname port || [[ -n $hostname ]] ; do
./script.exp "$hostname" "$username" "$password" "$port"
done <device-list.txt
See Read a file line by line assigning the value to a variable for information about reading files line by line in Bash.
I've added quotes to stop Shellcheck warnings, and make the code safer.
See How to loop over the lines of a file? for an explanation of why the code in the question doesn't work.
#pjh has the correct answer.
But here's some notes on your script:
you iterate over all the words of the file, rather than its lines.
using cut -d :, you specify the delimiter between fields as :.
However, in your file you don't use : as the delimiter, but space ()
you calculate the $hostname variable by parsing $device, but then you use $device when calling the script
you calculate the $port variable by parsing the $port variable, which doesn't make any sense.
Here's an example on how to parse each line with cut:
cat device-list.txt | while read device; do
hostname=$(echo $device | cut -d" " -f 1)
port=$(echo $device | cut -d" " -f 2)
./script.exp $hostname $username $password $port
done

bash read timeout from serial port

I'm trying something like Linux serial port listener and interpreter?
However, I want a timeout
#!/bin/bash
stty -F /dev/ttyUSB1 300 cs7 parenb -parodd
echo -n -e 'Sending '
echo -n -e "\x2F\x3F\x21\x0D\x0A">/dev/ttyUSB1
read LINE -r -t1 </dev/ttyUSB1
echo -n "Read "
echo $LINE
I'd like to continue if I do not get input; it just hangs.
(It is part of an input routine for reading a powermeter https://electronics.stackexchange.com/questions/305745/ir-data-from-landisgyr-e350)
Based on this answer, I think you can use the timeout function to get what you want.
You'll probably have to do a bit of formatting and redirect the file to stdout, like in this answer, so it would look like this :
LINE="$(timeout 1 cat /dev/ttyUSB1)"
echo -n "Read "
echo $LINE
You can also just use a combination of sleep and kill, like in here.

Reading from pasted input with line breaks in a Bash Script

I've been trying for a couple nights to get this Script to run with no luck. I'm trying to write a script using Bash that allows a user to paste a block of text, and the script will grep out the valid IP addresses from the text, and automatically ping them in order.
So far, after much modification, I'm stuck at this point:
#!/bin/sh
echo Paste Text with IP Addresses
read inputtext
echo "$inputtext">inputtext.txt
grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" inputtext.txt > address.txt
awk '{print $1}' < address.txt | while read ip; do
if ping -c1 $ip >/dev/null 2>&1; then
echo $ip IS UP
else
echo $ip IS DOWN
fi
done
rm inputtext.txt
rm address.txt
After running this script, the user is prompted as desired, and if an IP address was included in the first line of text, the ping check will succeed, but then all the text after that line will be spat out onto the following command prompt. So it seems that my issue lies in when I read from user input. The only part that is being read is the first line, and once a break is encountered, the script does not considered any lines past the first in its work.
As written, you just need an outer loop to actually read each line of user input.
#!/bin/sh
echo Paste Text with IP Addresses
while read -r inputtext
do
echo "$inputtext">inputtext.txt
grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" inputtext.txt > address.txt
awk '{print $1}' < address.txt | while read ip; do
if ping -c1 $ip >/dev/null 2>&1; then
echo $ip IS UP
else
echo $ip IS DOWN
fi
done
rm inputtext.txt
rm address.txt
done
However, you can actually simplify this much further and eliminate the temporary files.
#!/bin/sh
echo Paste Text with IP Addresses
while read -r inputtext
do
ip=$(echo "$inputtext" | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | awk '{print $1}')
if ping -c1 $ip >/dev/null 2>&1; then
echo $ip IS UP
else
echo $ip IS DOWN
fi
done

bash script to read/ping a list of ip addresses from a text file and then write the results to another file?

I have a text file with a lists of IP addresses called address.txt which contains the following
172.26.26.1 wlan01
172.26.27.65 wlan02
172.26.28.180 wlan03
I need to write a bash script that reads the only IP addresses, ping them and output to a another text file to something like this:
172.26.26.1 IS UP
172.26.27.65 IS DOWN
172.26.28.180 IS DOWN
I am fairly new to bash scripting so I am not sure where to start with this. Any help would be much appreciated.
In Linux this would work:
awk '{print $1}' < address.txt | while read ip; do ping -c1 $ip >/dev/null 2>&1 && echo $ip IS UP || echo $ip IS DOWN; done
I don't have a cygwin now to test, but it should work there too.
Explanation:
With awk we get the first column from the input file and pipe it into a loop
We send a single ping to $ip, and redirect standard output and standard error to /dev/null so it doesn't pollute our output
If ping is successful, the command after && is executed: echo $ip IS UP
If ping fails, the command after || is executed: echo $ip IS DOWN
Somewhat more readable, expanded format, to put in a script:
#!/bin/sh
awk '{print $1}' < address.txt | while read ip; do
if ping -c1 $ip >/dev/null 2>&1; then
echo $ip IS UP
else
echo $ip IS DOWN
fi
done

netcat script to send a message to a marquee device

I wrote a script that uses netcat to update the marquee in my office without knowing the ip address of the device. I used fping to calculate candidateIPs.
The script works. But, I still don't know the IP address of the device. Can someone help me understand how to update the script to narrow down the IP address that updated the text on the device?
#!/bin/bash
while read p; do
echo "try $p"
echo "\x00\x00\x00\x00\x00\x01\x5A\x30\x30\x02\x41\x41\x1B\x22\x61 Test message!\x04" | nc $p 3001 &
done < candidateIPs
wait
You can log your outputs an add verbosity e.g.
#!/bin/bash
while read p; do
echo "try $p"
echo "\x00\x00\x00\x00\x00\x01\x5A\x30\x30\x02\x41\x41\x1B\x22\x61 Test message!\x04" | nc -v "$p" 3001 2>&1 | tee "$p.log" &
done < candidateIPs
wait
You can examine either the ip-specific log files after that.

Resources