From 332b5171b886dbc00d432070331f266dbabedc58 Mon Sep 17 00:00:00 2001 From: pvincent Date: Sun, 12 Jun 2022 20:15:21 +0400 Subject: [PATCH] tools --- tools/extract_pdf_image | 35 +++++++++++++++ tools/internet_check | 22 +++++++++ tools/ip-lan-detect | 7 +++ tools/pgrep2 | 12 +++++ tools/shrinkpdf | 99 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 175 insertions(+) create mode 100755 tools/extract_pdf_image create mode 100755 tools/internet_check create mode 100755 tools/ip-lan-detect create mode 100755 tools/pgrep2 create mode 100755 tools/shrinkpdf diff --git a/tools/extract_pdf_image b/tools/extract_pdf_image new file mode 100755 index 0000000..e1624b8 --- /dev/null +++ b/tools/extract_pdf_image @@ -0,0 +1,35 @@ +#!/bin/bash + + +usage(){ + echo "Usage: $(basename "$0") [FOLDER|PREFIX]" +} + +[[ $# -lt 2 ]] && usage && exit 2 + +PAGE=$1 +PDF=$2 +DEST=${3:-.} + +[[ ! -f "$PDF" ]] && echo "FAILURE: File <$PDF> not found!" && exit 3 +file -bi "$PDF" | grep -q "application/pdf" +[[ $? -ne 0 ]] && echo "FAILURE: File <$PDF> not detected as a valid application/pdf document!" && exit 3 + +if [[ -d $DEST ]]; then + DEST="$DEST/extract" +fi + +if [[ "$PAGE" == *-* ]]; then + START=$(echo "$PAGE" | cut -d '-' -f1) + START=${START:-1} + PAGINATION="-f $START" + + END=$(echo "$PAGE" | cut -d '-' -f2) + if [[ ! -z $END ]]; then + PAGINATION="$PAGINATION -l $END" + fi +else + PAGINATION="-f $PAGE -l $PAGE" +fi + +pdftoppm -jpeg $PAGINATION "$PDF" "$DEST" diff --git a/tools/internet_check b/tools/internet_check new file mode 100755 index 0000000..a7a80d5 --- /dev/null +++ b/tools/internet_check @@ -0,0 +1,22 @@ +#!/bin/bash + +if ping -c1 1.1.1.1 2>&1 >/dev/null; then + connectivity=true +else + connectivity=false +fi +echo "connectivity=$connectivity" + +if ping -c1 google.fr 2>&1 >/dev/null; then + dns=true +else + dns=false +fi +echo "dns=$dns" + +if nmcli device status | grep -E "^wl" | grep -q connected; then + wifi=true +else + wifi=false +fi +echo "wifi=$wifi" diff --git a/tools/ip-lan-detect b/tools/ip-lan-detect new file mode 100755 index 0000000..43d522a --- /dev/null +++ b/tools/ip-lan-detect @@ -0,0 +1,7 @@ +#!/bin/bash + +nmap -sn `ip route | tail -1 | cut -d ' ' -f1` | sed -n 'n;p' | cut -c 22- | head -n -1 | awk '{tmp=$1; $1=$2; $2=tmp; gsub("\\(","",$1); gsub("\\)","",$1); printf "%-15s %s\n", $1, $2}' | sed 's/^ *//' + +# sed -n 'n;p' => FILTER odd lines +# awk '{tmp=$1; $1=$2; $2=tmp; gsub("\\(","",$1); gsub("\\)","",$1); printf "%-15s %s\n", $1, $2}' => SWAP words and remove parenthesis +# sed 's/^ *//' => REMOVE starting spaced diff --git a/tools/pgrep2 b/tools/pgrep2 new file mode 100755 index 0000000..1ba2e2b --- /dev/null +++ b/tools/pgrep2 @@ -0,0 +1,12 @@ +#!/bin/bash +# Colorized pgrep + +arg1="$1" + + +if [[ $arg1 = *"grep"* ]];then + # remote the grep command itself from process list! + ps -eo pid,ppid,uname:20,cmd | grep -v $$ | grep --color=auto "$arg1" +else + ps -eo pid,ppid,uname:20,cmd | grep -v 'grep' | grep --color=auto "$arg1" +fi diff --git a/tools/shrinkpdf b/tools/shrinkpdf new file mode 100755 index 0000000..f5c7539 --- /dev/null +++ b/tools/shrinkpdf @@ -0,0 +1,99 @@ +#!/bin/sh + +# http://www.alfredklomp.com/programming/shrinkpdf +# Licensed under the 3-clause BSD license: +# +# Copyright (c) 2014, Alfred Klomp +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# 3. Neither the name of the copyright holder nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + + +shrink () +{ + gs \ + -q -dNOPAUSE -dBATCH -dSAFER \ + -sDEVICE=pdfwrite \ + -dCompatibilityLevel=1.3 \ + -dPDFSETTINGS=/screen \ + -dEmbedAllFonts=true \ + -dSubsetFonts=true \ + -dAutoRotatePages=/None \ + -dColorImageDownsampleType=/Bicubic \ + -dColorImageResolution=$3 \ + -dGrayImageDownsampleType=/Bicubic \ + -dGrayImageResolution=$3 \ + -dMonoImageDownsampleType=/Bicubic \ + -dMonoImageResolution=$3 \ + -sOutputFile="$2" \ + "$1" +} + +check_smaller () +{ + # If $1 and $2 are regular files, we can compare file sizes to + # see if we succeeded in shrinking. If not, we copy $1 over $2: + if [ ! -f "$1" -o ! -f "$2" ]; then + return 0; + fi + ISIZE="$(echo $(wc -c "$1") | cut -f1 -d\ )" + OSIZE="$(echo $(wc -c "$2") | cut -f1 -d\ )" + if [ "$ISIZE" -lt "$OSIZE" ]; then + echo "Input smaller than output, doing straight copy" >&2 + cp "$1" "$2" + fi +} + +usage () +{ + echo "Reduces PDF filesize by lossy recompressing with Ghostscript." + echo "Not guaranteed to succeed, but usually works." + echo " Usage: $1 infile [outfile] [resolution_in_dpi]" +} + +IFILE="$1" + +# Need an input file: +if [ -z "$IFILE" ]; then + usage "$0" + exit 1 +fi + +# Output filename defaults to "-" (stdout) unless given: +if [ ! -z "$2" ]; then + OFILE="$2" +else + OFILE="-" +fi + +# Output resolution defaults to 72 unless given: +if [ ! -z "$3" ]; then + res="$3" +else + res="72" +fi + +shrink "$IFILE" "$OFILE" "$res" || exit $? + +check_smaller "$IFILE" "$OFILE"