Skip to content

Instantly share code, notes, and snippets.

@JonTheNiceGuy

JonTheNiceGuy/ink_level.sh

Last active May 12, 2020
Embed
What would you like to do?
Canon MG3050 Printer Ink Level reporting tool
#!/bin/bash
rc=0
function matchIP() {
# These regex based on https://stackoverflow.com/a/17871737
if echo "$1" | egrep '^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])$' >/dev/null 2>/dev/null
then
echo 4
return 4
elif echo "$1" | egrep '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,
4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:
[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3
}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))' >/dev/null 2>/dev
/null
then
echo 6
return 6
fi
echo 0
return 0
}
function getMessage() {
echo "$1" | cut -d\' -f2
}
if [ -n "$1" ]
then
if [ $(matchIP "$1") -eq 4 ]
then
printer="$1"
elif [ $(matchIP "$1") -eq 6 ]
then
printer="\[$1\]"
elif echo "$1" | egrep '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$' >/dev/null 2>/dev/null
then
if [ -n "$2" ]
then
interface="$2"
else
# Based on https://unix.stackexchange.com/a/302613
interface="$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)')"
fi
# Based on https://stackoverflow.com/a/17429136
printer=$(gssdp-discover -i $interface -n 3 -t uuid:$1 -m available | grep "Location" | cut -d\/ -f3 | cut -d: -f1)
else
printer="$1"
fi
fi
if [ -n "$printer" ]
then
while read -r line
do
if echo $line | egrep "var g_err_msg_id = 'MES.+';" >/dev/null 2>&1
then
ref="$(getMessage "$line")"
mes="$(curl -s http://$printer/LANG/messID.js | grep "$ref")"
echo "!! $(echo "$mes" | cut -d\" -f2) !!"
rc=255
# Following block based on working back the dispInkInfo function
# which is in http://$printer/rui/JS/top.js
elif echo $line | egrep '^inktank\[' >/dev/null 2>&1
then
values=$(echo $line | cut -d\[ -f3 | cut -d\] -f1)
case $(echo $values | cut -d, -f1) in
[07])
colour=Black
;;
1)
colour=PigmentBlack
;;
2)
colour=Cyan
;;
3)
colour=Magenta
;;
4)
colour=Yellow
;;
5)
colour=Grey
;;
6)
colour=Colour
;;
esac
case $(echo $values | cut -d, -f2) in
0)
scale=100
;;
1)
scale=90
;;
2)
scale=80
;;
3)
scale=70
;;
4)
scale=60
;;
5)
scale=50
;;
6)
scale=40
;;
7)
scale=30
;;
8)
scale=20
;;
9)
scale=10
;;
10)
scale=5
;;
11)
scale=0
;;
esac
# 0 = Fine
# 1 = Low
# 2 = Empty
# 3 = Unknown
case $(echo $values | cut -d, -f3) in
0)
echo "$colour cartridge is fine - quantity $scale% remaining"
;;
1)
echo "$colour cartridge is low - quantity $scale% remaining"
if [ $rc -eq 0 ]
then
rc=1
fi
;;
2)
echo "$colour cartridge is empty - quantity $scale% remaining"
if [ $rc -lt 2 ]
then
rc=2
fi
;;
3)
echo "$colour cartridge is in an unknown state - quantity $scale% remaining"
if [ $rc -lt 2 ]
then
rc=2
fi
;;
esac
fi
done <<< "$(curl -s http://$printer/JS_MDL/model.js)"
exit $rc
else
echo "ink_level.sh"
echo "Options are: ink_level.sh [ip_address | uuid [interface]]"
echo "For example: ink_level.sh 192.0.2.1"
echo " ink_level.sh 2001:db8::1"
echo " ink_level.sh 00000000-0000-0000-0000-DECAFBAD1234"
echo " ink_level.sh 00000000-0000-0000-0000-DECAFBAD1234 wlan0"
exit 255
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment