From 473494565f3fa7ba9da10df5a9ff4ebbb48cfe74 Mon Sep 17 00:00:00 2001 From: Mid Favila Date: Wed, 30 Jun 2021 00:22:57 -0300 Subject: [PATCH] Initial commit. --- LICENSE | 2 +- Links | 8 +++++++ README.md | 14 ++++++++++- fpm | 38 ++++++++++++++++++++++++++++++ fpv | 10 ++++++++ gasutc | 38 ++++++++++++++++++++++++++++++ gcl | 3 +++ iview | 22 ++++++++++++++++++ ls | 4 ++++ man | 4 ++++ modem-manager | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ netsh | 59 +++++++++++++++++++++++++++++++++++++++++++++++ plumber | 41 +++++++++++++++++++++++++++++++++ 13 files changed, 305 insertions(+), 2 deletions(-) create mode 100755 Links create mode 100755 fpm create mode 100755 fpv create mode 100755 gasutc create mode 100755 gcl create mode 100755 iview create mode 100755 ls create mode 100755 man create mode 100755 modem-manager create mode 100755 netsh create mode 100755 plumber diff --git a/LICENSE b/LICENSE index a4e9dc9..ab312c4 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT No Attribution -Copyright +Copyright 2021, Mid Favila Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software diff --git a/Links b/Links new file mode 100755 index 0000000..9e31c27 --- /dev/null +++ b/Links @@ -0,0 +1,8 @@ +#!/bin/sh + +export CONFIG_DIR=".config" + +if [ ! -e /tmp/Links.xid ] +then /usr/bin/tabbed -t '#000000' -T '#FFFFFF' -u '#C0C0C0' -U '#000000' -c /usr/bin/links "$@" -g -w > /tmp/Links.xid ; rm /tmp/Links.xid +else /usr/bin/links "$@" -g -w "$(cat /tmp/Links.xid)" +fi diff --git a/README.md b/README.md index ddc650b..588bd47 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,15 @@ # POSIX_Scripts -A set of POSIX* shell scripts designed for personal use. No guarantees when it comes to quality, as these are also used for various minor experiments of mine. \ No newline at end of file +A set of POSIX* shell scripts designed for personal use. No guarantees when it comes to quality, as these are also used for various minor experiments of mine. + +Links - wrapper script for managing links2 and tabbed +fpm - wrapper for using ffplay to manage music +fpv - the above, but video +gasutc - "Get and Set UTC" over HTTPS using ncat +gcl - one-liner to g(it)cl(one) +iview - simple wrapper for sxiv; auto-sizes based on image dimensions +ls - one-liner to pipe ls through cols +man - short script to auto-resize mandoc output +modem-manager - used to manage laptop PCIe WAN modems. rather dense +netsh - an old experiment that serves as a simple TUI for managing network interfaces... and an example of how not to use AWK +plumber - one of my more sophisticated scripts; plumber creates pipes between programs based on what sort of input it receives, and handles errors (somewhat) intelligently diff --git a/fpm b/fpm new file mode 100755 index 0000000..6f7e2c6 --- /dev/null +++ b/fpm @@ -0,0 +1,38 @@ +#!/bin/sh + +# Grab a number that is greater than zero but no greater than the number of +# files in our directory +rand_file() +{ + x=$(($(/usr/bin/dd bs=2 count=1 if=/dev/random of=/dev/stdout| /usr/bin/od -|\ + /usr/bin/sed 's/ */ /'|/usr/bin/cut -d ' ' -f2|/usr/bin/sed 1!d)%$((1+$(/usr/bin/ls|\ + /usr/bin/wc -l))))) + /usr/bin/echo "${x}" +} + +if [ $# = 0 ] + then /usr/bin/printf "FPM provides a convenient way to use ffplay for audio playback.\nIt accepts the following arguments:\n\n-s: Shuffle songs in the directory randomly.\n\n-l: Play on repeat.\n\n" + else +case "$@" in + +-s) + file=$(x=$(rand_file);if [ "${x}" = 0 ];then x=1;fi;/usr/bin/ls|sed "${x}"!d) + /usr/bin/ffplay -autoexit -nodisp "${file}" + sleep 1 # Prevent fpm from continuously respawning on kill + +;; + +-l) + while true + do /usr/bin/ffplay -autoexit -nodisp "$1" + done +;; + + +*) + /usr/bin/ffplay -autoexit -nodisp "$1" +;; + +esac +fi +exit diff --git a/fpv b/fpv new file mode 100755 index 0000000..d537b79 --- /dev/null +++ b/fpv @@ -0,0 +1,10 @@ +#!/bin/sh -e + +case "$1" in + +*) + /usr/bin/ffplay -autoexit "$1" +;; + +esac +exit diff --git a/gasutc b/gasutc new file mode 100755 index 0000000..c3e4347 --- /dev/null +++ b/gasutc @@ -0,0 +1,38 @@ +#!/bin/sh -e + +# Retrieve a header from somewhere. Don't care where. +echo "GET HTTP/1.1" | ncat --ssl 205.166.94.16 443 | grep Date > /tmp/date.jnk + +# Month lookup table. Horrible, but I don't know a better method... +# Please forgive me for this sin, Patriarch Bourne. +month=`cut -d ' ' -f 4 /tmp/date.jnk` +case "$month" in +Jan) month=01 ;; +Feb) month=02 ;; +Mar) month=03 ;; +Apr) month=04 ;; +May) month=05 ;; +Jun) month=06 ;; +Jul) month=07 ;; +Aug) month=08 ;; +Sep) month=09 ;; +Oct) month=10 ;; +Nov) month=11 ;; +Dec) month=12 ;; +esac + +x=1 +for each in day year time +do + export $each=`cat /tmp/date.jnk | tr ' ' '\n' | grep -E [0-9] | sed s/\://g | tr '\n' ' ' | cut -d ' ' -f $x` + x=$(($x+1)) +done + +printf "The current time is as follows.\nYear: $year\nMonth: $month\nDay: $day\nTime: $time\n" + +time=`echo $time | cut -b 1,2,3,4` +year=`echo $year | cut -b 3,4` +printf "Your date-compatible string for today is: $month$day$time$year\nHere it is again, on its own line:\n$month$day$time$year\n" +/bin/ssu -- date -u $month$day$time$year +rm /tmp/date.jnk +exit diff --git a/gcl b/gcl new file mode 100755 index 0000000..a9632ac --- /dev/null +++ b/gcl @@ -0,0 +1,3 @@ +#!/bin/sh +/usr/bin/git clone --recurse-submodules "$@" +exit diff --git a/iview b/iview new file mode 100755 index 0000000..0e0e845 --- /dev/null +++ b/iview @@ -0,0 +1,22 @@ +#!/bin/sh -e + +type=$(file --mime-type "${1}" | cut -f 2 -d ' ') + +case "${type}" in + +*image/png*) + x=$(/bin/file "${1}" | /bin/grep -o "[0-9]* x [0-9]*" | sed 's/ x /x/') +;; + +*image/jpeg*) + x=$(/bin/file "${1}" | /bin/grep -o "[0-9]*x[0-9]*" | sed "2!d") +;; + +*) + x=$(/bin/file "${1}" | /bin/grep -o "[0-9]* x [0-9]*" | sed 's/ x /x/') +;; + +esac + +/bin/sxiv -bg "${x}" "${1}" +exit diff --git a/ls b/ls new file mode 100755 index 0000000..6d58223 --- /dev/null +++ b/ls @@ -0,0 +1,4 @@ +#!/bin/sh + +/usr/bin/ls "$@" | /usr/bin/cols +exit diff --git a/man b/man new file mode 100755 index 0000000..4e98174 --- /dev/null +++ b/man @@ -0,0 +1,4 @@ +#!/bin/sh +x=$(/usr/bin/tput cols) +/usr/bin/man -O width=$((x-2)) "${@}" +exit diff --git a/modem-manager b/modem-manager new file mode 100755 index 0000000..adc02a9 --- /dev/null +++ b/modem-manager @@ -0,0 +1,64 @@ +#!/bin/sh + +# We need root to configure interface properties. +if [ "${USER}" != root ] + then printf "We need root!\n" + exit + else + +# Has the interface even been initialized? If not, do so. + while [ ! -e /dev/cdc-wdm0 ] + do printf "3\n" >> /sys/devices/pci0000:00/0000:00:14.0/usb2/2-3/bConfigurationValue + done + +# Get the name of our interface. + wif=$(ifconfig -l | tr ' ' '\n' | grep ww) + +# Establish a link, but only if one is not already available. + if [ -e /tmp/mbim-network-state-cdc-wdm0 ] + then printf "Connection already established.\nBringing down...\n" + printf "Closing netlink...\n" + mbim-network /dev/cdc-wdm0 stop >> /dev/null + printf "Bringing down interface...\n\n" + ifconfig "${wif}" down >> /dev/null + exit + else printf "Turning on radio...\n" + mbimcli --device=/dev/cdc-wdm0 --device-open-proxy --set-radio-state=on >> /dev/null + printf "Bringing up interface...\n" + ifconfig "${wif}" up >> /dev/null + printf "Opening netlink...\n\n" + mbim-network /dev/cdc-wdm0 start >> /dev/null + fi + +# Store the result of the query so that we don't have to call it constantly + printf "Querying for network info...\n" + mbimcli -d /dev/cdc-wdm0 -p --query-ip-configuration > /tmp/qic.tmp + +# Get our IPv4. + ip=$(sed -ne "s/IP \[0\]\: '\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\/[0-9][0-9]\)'/\1/p" /tmp/qic.tmp | sed 's/ //g' | cut -d '/' -f 1) + +# Get our CIDR netmask and convert to decimal. + nm=255.255.255.$(((0xFFFFFFFF >> $(sed -ne "s/IP \[0\]\: '\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\/[0-9][0-9]\)'/\1/p" /tmp/qic.tmp | sed 's/ //g' | cut -d '/' -f 2)) ^ 0xFF)) + +# Get our gateway. + gw=$(sed -ne "s/Gateway: '\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)'/\1/p" /tmp/qic.tmp|sed 's/ //g') + + +# Get our MTU. + mtu=$(sed 7!d /tmp/qic.tmp | sed -ne "s/MTU: '\([0-9]*\)'/\1/p" | sed -ne 's/ //gp') + + printf "IP is %s\nNM is %s\nGW is %s\nMTU is %s\n\n" "${ip}" "${nm}" "${gw}" "${mtu}" + +# Actually set up the connection. + printf "Assigning interface details...\n" + ifconfig "${wif}" "${ip}" netmask "${nm}" + printf "Routing through cellular gateway...\n" + route add default gw "${gw}" + +# Test connection. + printf "Testing interface.\n\n" + ping -c 4 sdf.org + printf "\n" + rm /tmp/qic.tmp +fi +exit diff --git a/netsh b/netsh new file mode 100755 index 0000000..5cf3c2c --- /dev/null +++ b/netsh @@ -0,0 +1,59 @@ +#!/usr/bin/env sh +#This is a poorly-written script and should be ignored. + + +printf "\nWhat do you want to do?\n1. Configure\n2. Scan\n3. View\n>: " + +read choice + +case $choice in + +1) + printf "Not ready yet.\n" +;; + +2) + printf "\nScan with which interface?\n" + ifconfig -l | tr ' ' '\n' | grep wl | nl | awk '{printf $1 ". " $2 "\n"}' + printf ">: " + read choice + dev=`ifconfig -l | tr ' ' '\n' | grep wl | sed $choice!d` + iw dev $dev scan | grep SSID | $PAGER +;; + +3) + + printf "\nView what?\n1. Interface information\n2. Routing table\n>: " + read choice + case $choice in + 1) + + x=1 + i=$((`ifconfig -l | tr ' ' '\n' | wc -l`+1)) + printf "\nWhich interface?\n" + while [ $x -lt $i ] + do ifconfig -l | tr ' ' '\n' | nl | sed $x!d | awk '{printf $1 ". " $2 "\n"}' + x=$(($x+1)) + done + printf ">: " + read choice + printf "\n" ; ifconfig -l | tr ' ' '\n' | sed $choice!d | awk '{printf "ifconfig " $1 "\n"}' | sh -e -- + + ;; + + 2) + printf "\n" ; route ; printf "\n" + ;; + + *) + printf "Illegal input. Aborting.\n" + ;; + esac +;; + +*) + printf "Illegal input. Aborting.\n" +;; + +esac +exit diff --git a/plumber b/plumber new file mode 100755 index 0000000..96a3a04 --- /dev/null +++ b/plumber @@ -0,0 +1,41 @@ +#!/bin/sh -e + +rand_inst() # Choose a random invidious instance to distribute load. + # Good digital citizenship is important, you know. +{ + case $(($(dd bs=2 count=1 if=/dev/random of=/dev/stdout | od - | \ + sed -ne 's/ */ /gp' | cut -d ' ' -f2) % 3)) in + 0) printf "vid.puffyan.us" ;; + 1) printf "ytprivate.com" ;; + 2) printf "yewtu.be" ;; + esac +} + +if [ "$(printf "%s" "${1}"|cut -b 1,2,3,4)" = "http" ] +then + case "${1}" in + *youtu*) exec Links "$(printf "%s" "${1}"|sed -ne \ + "s/\(http.*:\/\/\).*\(\/.*\)/\1$(rand_inst)\2/p")" & ;; + *reddit*) exec Links "$(printf "%s" "${1}"|sed -ne \ + "s/\(http.*:\/\/\).*\(\/.*\/\)/\1libredd.it\2/p")" & ;; + https://4chan.org*) exec Links "$(printf "%s" "${1}"|sed -ne \ + "s/4chan/4channel/p")" ;; + *) exec Links "${1}" & ;; + esac + +else + + mime=$(file --mime-type -b "${1}") + + case ${mime} in + *html*) exec Links "${1}" & ;; + text*) exec xless "${1}" & ;; + video*) exec fpv "${1}" & ;; + audio*) exec fpm "${1}" & ;; + image*) exec iview "${1}" & ;; + *pdf) exec gv "${1}" & ;; + *) printf "Plumber ran into an edge case with the following string/mime\n%s"\ + "${1}"|xmessage -file - & ;; + esac +fi +exit