From e146caaa63ac85edca18c78da53663748b43dc9b Mon Sep 17 00:00:00 2001 From: maxime Date: Mon, 5 Feb 2024 10:02:13 +0100 Subject: [PATCH] initial commit --- check_ssl | 5 ++ cloud_batch-check.sh | 1 + cloud_batch.sh | 1 + degtodecimal | 19 +++++++ ffmpeg_extract_audio.sh | 19 +++++++ find_missing_files.sh | 8 +++ flixbus_curl.sh | 1 + mailbox_watcher.pl | 107 ++++++++++++++++++++++++++++++++++++++++ mhssh | 72 +++++++++++++++++++++++++++ mhssh_lxc | 68 +++++++++++++++++++++++++ mount_hfs.sh | 1 + mp3_reduce_quality.sh | 2 + mutt_.sh | 2 + passmenu | 25 ++++++++++ remove-youtube-id.sh | 6 +++ rsync_batch.sh | 1 + sms_sender.sh | 42 ++++++++++++++++ tplinkM7350.sh | 36 ++++++++++++++ upgrade_mpsyt_ytdl.sh | 1 + yt | 3 ++ 20 files changed, 420 insertions(+) create mode 100755 check_ssl create mode 100755 cloud_batch-check.sh create mode 100755 cloud_batch.sh create mode 100755 degtodecimal create mode 100755 ffmpeg_extract_audio.sh create mode 100755 find_missing_files.sh create mode 100755 flixbus_curl.sh create mode 100755 mailbox_watcher.pl create mode 100755 mhssh create mode 100755 mhssh_lxc create mode 100644 mount_hfs.sh create mode 100755 mp3_reduce_quality.sh create mode 100755 mutt_.sh create mode 100755 passmenu create mode 100755 remove-youtube-id.sh create mode 100755 rsync_batch.sh create mode 100755 sms_sender.sh create mode 100755 tplinkM7350.sh create mode 100755 upgrade_mpsyt_ytdl.sh create mode 100755 yt diff --git a/check_ssl b/check_ssl new file mode 100755 index 0000000..80d6bbb --- /dev/null +++ b/check_ssl @@ -0,0 +1,5 @@ +#!/usr/bin/bash +while read -r URL; +do + openssl s_client -brief -connect $URL:443 < /dev/null; +done < ./https_urls_freepoteries diff --git a/cloud_batch-check.sh b/cloud_batch-check.sh new file mode 100755 index 0000000..69c462d --- /dev/null +++ b/cloud_batch-check.sh @@ -0,0 +1 @@ +rsync -vai --delete-before --read-batch=/home/emixam/cloud_batch-check ${1:-/mnt/cloud/} diff --git a/cloud_batch.sh b/cloud_batch.sh new file mode 100755 index 0000000..e96c0db --- /dev/null +++ b/cloud_batch.sh @@ -0,0 +1 @@ +rsync -vai --delete-before --read-batch=/home/emixam/cloud_batch ${1:-/mnt/cloud/} diff --git a/degtodecimal b/degtodecimal new file mode 100755 index 0000000..ffd194e --- /dev/null +++ b/degtodecimal @@ -0,0 +1,19 @@ +#!/bin/bash +LON="8° 02' 41.9\"W" +LAT="39° 57' 00.0\"N" + +deg_to_dec() +{ + deg=$(echo "$@"|awk -F\° '{ print $1 }') + ARG=$(echo "$@"|awk -F\° '{ print $2 }') + min=$(echo "$ARG"|awk -F\' '{ print $1 }') + ARG=$(echo "$ARG"|awk -F\' '{ print $2 }') + sec=$(echo "$ARG"|awk -F\" '{ print $1 }') + + operation="scale=10; ( $deg + $min / 60 + $sec / 3600 )" + + echo $ARG|grep -ioE "[ws]$" >/dev/null && operation="$operation * -1" + bc <<< "$operation" +} + +echo "` deg_to_dec $LAT `:` deg_to_dec $LON `" diff --git a/ffmpeg_extract_audio.sh b/ffmpeg_extract_audio.sh new file mode 100755 index 0000000..0c3d3bb --- /dev/null +++ b/ffmpeg_extract_audio.sh @@ -0,0 +1,19 @@ +#!/bin/sh +REC=0 +if [[ "$1" == "-r" ]]; +then + shift; + [[ ! -d "$1" ]] \ + && echo "Recursive mode argument must be a directory" \ + && exit -1 + + find "$1" -type f -exec $0 {} \; +else + IN="$1"; + [[ ! -f "$IN" ]] && echo "$IN is not a file. Skipping." && exit -1; + OUT=${2:-$(echo "$1"|sed 's/\.[^\.]*$/\.mp3/')} + [[ -e "$OUT" ]] && echo "$OUT already exists. Skipping." && exit -1; + echo "$IN -> $OUT"; + ffmpeg -i "$IN" -vn -ar 44100 -ac 2 -ab 192k -f mp3 "$OUT" > /dev/null 2>&1; +fi +exit 0 diff --git a/find_missing_files.sh b/find_missing_files.sh new file mode 100755 index 0000000..d43d2e4 --- /dev/null +++ b/find_missing_files.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# first argument : where files may be missing +# second argument : where files may be + +while read f; +do + test -z "$(find "$2" -not -iregex ".*$1.*" -name $(basename "$f"))" && echo "Missing $f in $2"; +done < <(find "$1" -type f); diff --git a/flixbus_curl.sh b/flixbus_curl.sh new file mode 100755 index 0000000..69daa8d --- /dev/null +++ b/flixbus_curl.sh @@ -0,0 +1 @@ +curl 'https://www.ombord.info/hotspot/hotspot.cgi?method=login&url=https://flixbusmedia-emea.on.icomera.com/en/landing_page.html&onerror=https://flixbusmedia-emea.on.icomera.com/en/' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'DNT: 1' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' diff --git a/mailbox_watcher.pl b/mailbox_watcher.pl new file mode 100755 index 0000000..6ada0eb --- /dev/null +++ b/mailbox_watcher.pl @@ -0,0 +1,107 @@ +#!/usr/bin/env perl +use Mail::IMAPClient; +use IO::File; +use Data::Dumper; + +my $cfg = do ($ENV{'HOME'}."/.config/mailbox_watcher.pl") or die "Can't read configuration file"; +my $cache_dir = $cfg->{cache_dir}; +my $log_file = IO::File->new(">>".$cache_dir."/mailbox_watcher.log") or die "Can't open log file $!"; +my $date = `date "+%Y-%m-%d %H:%M:%S"`; +chomp $date; + +select $log_file; +printf "[%s] Mailbox watcher started\n", $date; + +if ($cfg->{debug} == 1) { + $cfg->{server}{Debug} = 1; + $cfg->{server}{Debug_fh} = IO::File->new( + ">".$cache_dir."/imap.log") + or die "Can't open imap log: $!\n"; +} + +# chomp the pass as it's given from an external script -> should do it directly +my $pass = $cfg->{server}->{Password}; +chomp $pass; +$cfg->{server}->{Password} = $pass; + +my $server = $cfg->{server}; + +if ($cfg->{debug} == 1) { + print Data::Dumper->Dump([$server]); +} + +my $imap = Mail::IMAPClient->new(%$server) + or die ("Can't create imap object"); + +$imap->connect or die "No connection!"; +$date = `date "+%Y-%m-%d %H:%M:%S"`; +chomp $date; +printf "[%s] Connected to IMAP server\n", $date; + +my $fh = IO::File->new(">".$cache_dir."/imap_count"); +select $fh; +foreach $box (@{$cfg->{boxes}}) { + my $r_count = 0; + defined($r_count = $imap->recent_count($box)) + or die "Could not count ".$box." recent: $@\n"; + + if ($r_count > 0) { + $imap->select($box); + #$imap->unset_flag("\Recent", @{$imap->recent}); + } + + my $u_count = 0; + defined($u_count = $imap->unseen_count($box)) + or die "Could not count ".$box." unseen: $@\n"; + + printf "%s\t%d/%d\n", ($box, $r_count, $u_count); +} +undef $fh; + +select $log_file; +print "OK"; +if ($cfg->{sms_send} == 1) { + my $fh = IO::File->new("<".$cache_dir."/imap_count") + or die "Can't open imap count file"; + my $sms = ""; + + my $date = `date "+%Y-%m-%d %H:%M:%S"`; + chomp $date; + printf "[%s] Building the SMS to send\n", $date; + + while (readline($fh)) { + if (not /\t0\//) { + s/\t/ /g; + s/ /:/g; + s/\n/\\n/g; + $sms .= $_; + print $sms; + } + else { + } + } + my $date = `date "+%Y-%m-%d %H:%M:%S"`; + chomp $date; + chomp $sms; + if (length $sms > 0) { + #printf "[%s] SMS to send : %s\n", ($date, $sms); + printf "[%s] Sending SMS to number : %s\n", ($date, $cfg->{sms_number}); + my $tmp = `mktemp`; + my $tmp_fh = IO::File->new(">".$tmp) or die "Can't open temporary file"; + + # write sms into tmp file + select $tmp_fh; + print $sms; + + select $log_file; + my $sys_call = $cfg->{sms_sender}.' '.$cfg->{sms_number}.' '.$tmp; + !system $sys_call; + !system "/usr/bin/rm $tmp"; + } + else { + printf "[%s] No new message\n", ($date, $sms); + printf "%s, %d", ($sms, length $sms); + } +} +exit; + diff --git a/mhssh b/mhssh new file mode 100755 index 0000000..f3d6f3c --- /dev/null +++ b/mhssh @@ -0,0 +1,72 @@ +#!/bin/bash +#MHSSH (Multi Host SSH) : send a command on multiple hosts +#Copyright (C) 2016 Weber Yann +# +#This program is free software; you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation; either version 3 of the License, or +#any later version. +# +#This program is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + + +# Host list path (file should contains hosts seperated by $IFS ) +hosts="$HOME/.config/mhssh.hosts" + +red=$(tput setaf 1) +grn=$(tput setaf 2) +raz=$(tput sgr0) + +usage() { + echo "Run a single command on multiple hosts" + echo "Usage : $0 [-h|--help] [-n|--no-color] CMD ..." + echo -e "\n\t-h --help\tprint this help\n\t-n --no-color\tdisable colors\n\n" + echo "Host list configured to be found in '$hosts'" +} + +nocmd() { + echo -e "at least one command expected...\n" + usage + exit 3 +} + +# arg "parsing" +[ $# -eq 0 ] && nocmd +case "$1" in + -h|--help) + usage + exit 1 + ;; + -n|--no-color) + [ $# -eq 1 ] && nocmd + shift + red='[ERR] ' + grn='' + raz='' + ;; +esac + +if [ ! -f "$hosts" ] +then + echo -e "File '$hosts' not found" >&2 + echo "" + usage + exit 2 +fi + + +for host in $(cat $hosts) +do + echo -e "\n$grn=====================\n$grn$host\n$grn=====================$raz\n" + stdout=$(echo -ne "$grn$(printf "%20s" "$host") : $raz") + stderr=$(echo -ne "$red$(printf "%20s" "$host") : $raz") + { + ssh $host "$@;exit" 2>&3 | sed -e "s/^/$stdout/" 1>&2 + } 3>&1 | sed -e "s/^/$stderr/" +done diff --git a/mhssh_lxc b/mhssh_lxc new file mode 100755 index 0000000..0d93b51 --- /dev/null +++ b/mhssh_lxc @@ -0,0 +1,68 @@ +#!/bin/bash +red=$(tput setaf 1) +grn=$(tput setaf 2) +raz=$(tput sgr0) + +usage() { + echo "Run a single command on multiple lxc containers" + echo "Usage : $0 [-h|--help] [-n|--no-color] [-o|--os OS] CMD ..." + echo -e "\n\t-h --help\tprint this help\n\t-n --no-color\tdisable colors\n\n" + echo "Host list configured to be found in '$hosts'" +} + +nocmd() { + echo -e "at least one command expected...\n" + usage + exit 3 +} + +# arg "parsing" +[ $# -eq 0 ] && nocmd +case "$1" in + -h|--help) + usage + exit 1 + ;; + -n|--no-color) + [ $# -eq 1 ] && nocmd + shift + red='[ERR] ' + grn='' + raz='' + ;; + -o|--os) + if [[ "$#" -gt 0 ]]; + then + shift; + OS=$1; + hosts="$HOME/.config/mhssh_lxc.$OS.hosts"; + shift; + fi; + ;; +esac + +if [ -z $hosts ]; +then + hosts="$HOME/.config/mhssh_lxc.hosts"; +fi + + +if [ ! -f "$hosts" ] +then + echo -e "File '$hosts' not found" >&2 + echo "" + usage + exit 2 +fi + +while read -r host; +do + echo -e "\n$grn=====================\n$grn$host\n$grn=====================$raz\n" + stdout=$(echo -ne "$grn$(printf "%20s" "$host") : $raz") + stderr=$(echo -ne "$red$(printf "%20s" "$host") : $raz") + { + lxc-attach -n "$host" -- "$@" 2>&3 | sed -e "s/^/$stdout/" 1>&2 + } 3>&1 | sed -e "s/^/$stderr/" + +done <<< $(cat $hosts) + diff --git a/mount_hfs.sh b/mount_hfs.sh new file mode 100644 index 0000000..ffdf94e --- /dev/null +++ b/mount_hfs.sh @@ -0,0 +1 @@ +mount -t hfsplus -o force,rw /dev/sdb4 /mnt/usb/ diff --git a/mp3_reduce_quality.sh b/mp3_reduce_quality.sh new file mode 100755 index 0000000..cf53fdf --- /dev/null +++ b/mp3_reduce_quality.sh @@ -0,0 +1,2 @@ +#!/bin/bash +lame -V9 -h "$1" "$(echo "$1"|sed 's/\.mp3$/_0&/')" diff --git a/mutt_.sh b/mutt_.sh new file mode 100755 index 0000000..f2882cb --- /dev/null +++ b/mutt_.sh @@ -0,0 +1,2 @@ +#!/bin/sh +/usr/bin/urxvt -e /usr/bin/mutt $@ diff --git a/passmenu b/passmenu new file mode 100755 index 0000000..83268bc --- /dev/null +++ b/passmenu @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +shopt -s nullglob globstar + +typeit=0 +if [[ $1 == "--type" ]]; then + typeit=1 + shift +fi + +prefix=${PASSWORD_STORE_DIR-~/.password-store} +password_files=( "$prefix"/**/*.gpg ) +password_files=( "${password_files[@]#"$prefix"/}" ) +password_files=( "${password_files[@]%.gpg}" ) + +password=$(printf '%s\n' "${password_files[@]}" | dmenu "$@") + +[[ -n $password ]] || exit + +if [[ $typeit -eq 0 ]]; then + pass show -c "$password" 2>/dev/null +else + pass show "$password" | { IFS= read -r pass; printf %s "$pass"; } | + xdotool type --clearmodifiers --file - +fi diff --git a/remove-youtube-id.sh b/remove-youtube-id.sh new file mode 100755 index 0000000..8e42a7a --- /dev/null +++ b/remove-youtube-id.sh @@ -0,0 +1,6 @@ +#!/bin/sh +find $1 -type f | while read FILE; +do + newfile="$(echo ${FILE} |sed -E 's/-[^\.]{11}\././')" ; + mv "${FILE}" "${newfile}" ; +done; diff --git a/rsync_batch.sh b/rsync_batch.sh new file mode 100755 index 0000000..df82ccd --- /dev/null +++ b/rsync_batch.sh @@ -0,0 +1 @@ +rsync -a --delete-before --read-batch=rsync_batch ${1:-test2/} diff --git a/sms_sender.sh b/sms_sender.sh new file mode 100755 index 0000000..62e1140 --- /dev/null +++ b/sms_sender.sh @@ -0,0 +1,42 @@ +#!/bin/sh +num=${1:-"+33614495846"} +msg="${2}" +tim=$(date "+%Y,%m,%d,%H,%M,%S") +nonce=$(curl -s 'http://192.168.0.1/cgi-bin/qcmap_auth' \ + --data '{"module":"authenticator","action":0}'|jq '.nonce'|tr -d '\"') +digest=$(printf 'admin:admin:%s' $nonce|md5sum|cut -d' ' -f1) +json=$(printf '{"module":"authenticator","action":1,"digest":"%s"}' $digest); +token=$(curl -s 'http://192.168.0.1/cgi-bin/qcmap_auth' \ + --data "${json}"|jq '.token'|tr -d '"') + +num=${1:-"+33614495846"} +msg="${2:-"Hello, World!"}" +tim=$(date "+%Y,%m,%d,%H,%M,%S") +tmp=$(mktemp) +cat << EOF > $tmp +{"token":"${token}", \ +"module":"message", \ +"action":3, \ +"sendMessage":{"to":"${num}", \ +"textContent":"$(cat ${msg})", \ +"sendTime":"${tim}"}} +EOF + +curl -qs 'http://192.168.0.1/cgi-bin/qcmap_web_cgi' \ +-H "Cookie: tpweb_token=${token}" \ +--data @$tmp && echo "Trying to send message" +RES=-1 +read json << EOF +{"token":"${token}","module":"message","action":7} +EOF + +while [ "$RES" -lt 0 ]; +do +RES=$(curl -s 'http://192.168.0.1/cgi-bin/qcmap_web_cgi' \ + -H "Cookie: tpweb_token=${token}" \ + --data "$json" \ + | jq '.result') + sleep 2; +done; +rm $tmp; +exit 0; diff --git a/tplinkM7350.sh b/tplinkM7350.sh new file mode 100755 index 0000000..31ff38b --- /dev/null +++ b/tplinkM7350.sh @@ -0,0 +1,36 @@ +#!/bin/bash +get_stats() +{ + echo -n $(curl -s 'http://192.168.0.1/cgi-bin/qcmap_web_cgi' --data '{"module":"status","action":0}') +} + +in_mb() +{ + echo $(echo "scale=2; $1/1024/1024" | bc); +} + +stats=$(get_stats) +total=$(echo "$stats"|jq '.wan.totalStatistics'|sed 's/"//g') +daily=$(echo "$stats"|jq '.wan.dailyStatistics'|sed 's/"//g') + +#echo "Total : $total"; +total=$(in_mb "$total") +#echo "Total (MB) : $total"; +#echo "Daily : $daily"; +daily=$(in_mb "$daily") +echo "Daily (MB) : $daily"; + +days_left=$(echo 21 - `date +%d`|bc) +if [[ ${days_left} -eq 0 ]]; +then + days_left=21 +elif [[ ${days_left} -lt 0 ]]; +then + mdays=$(date --date="1 day ago 1 $(date --date="next month" "+%b")" "+%d") + days_left=$(echo 21 + \("$mdays" - $(date +%d)\)|bc) +fi +#echo "Days left : $days_left"; +max_daily=$(echo "scale=2; (6144 - $total) / $days_left"|bc) +echo "Max daily : $max_daily"; +today_rest=$(echo "$max_daily - $daily"|bc) +echo $today_rest; diff --git a/upgrade_mpsyt_ytdl.sh b/upgrade_mpsyt_ytdl.sh new file mode 100755 index 0000000..f266870 --- /dev/null +++ b/upgrade_mpsyt_ytdl.sh @@ -0,0 +1 @@ +pip install --user --upgrade youtube-dl mps-youtube diff --git a/yt b/yt new file mode 100755 index 0000000..e653196 --- /dev/null +++ b/yt @@ -0,0 +1,3 @@ +#!/bin/sh +youtube-dl -f18 $1 +