%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /sbin/
Upload File :
Create Path :
Current File : //sbin/update-ieee-data

#!/bin/sh
# Copyright © 2013 Filippo Giunchedi <filippo@debian.org>
# Copyright © 2013-2016 Luciano Bello <luciano@debian.org>
# Copyright © 2022 Samuel Henrique <samueloph@debian.org>
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar.
# See the LICENSE file for more details.

BASEDIR=${BASEDIR:-/var/lib/ieee-data/}
RUN_PARSERS=${RUN_PARSERS:-1}
OLD="5"
FORCE=false
QUIET=false

set -e

files_to_get="https://standards-oui.ieee.org/oui/oui.txt|oui.txt
https://standards-oui.ieee.org/oui28/mam.txt|mam.txt
https://standards-oui.ieee.org/oui36/oui36.txt|oui36.txt
https://standards-oui.ieee.org/iab/iab.txt|iab.txt
https://standards-oui.ieee.org/oui/oui.csv|oui.csv
https://standards-oui.ieee.org/oui28/mam.csv|mam.csv
https://standards-oui.ieee.org/oui36/oui36.csv|oui36.csv
https://standards-oui.ieee.org/iab/iab.csv|iab.csv"
tmpf=$(mktemp -t "ieee-data_XXXXXX")
chmod 0644 "$tmpf"

Die () {
	$QUIET || printf "%s\n" "$1"
	exit 1
}

checkTXT () {
	TXTOUI='OUI.*\s*Organization\s*.*\s*Organization\s*Address'
	head -n 6 "$1" | tr -d '\n' | grep -qe "$TXTOUI"
	return $?
}


checkCSV () {
	CSVOUI='Registry,Assignment,Organization Name,Organization Address'
	head -n 1 "$1" | grep -qe "$CSVOUI"
	return $?
}

checkFileHead () {
	$QUIET || printf "\tChecking header\n"
	TXTOUI='OUI.*\s*Organization\s*.*\s*Organization\s*Address'
	checkTXT "$1" || checkCSV "$1" || ( rm "$1" && Die "The downloaded file looks corrupted." )
}

checkPerm() {
	$QUIET || printf "\tChecking permissions on %s\n" "$1"
	touch -a "$1" || { Die "Touch on $1 exit with $?";}
}

goAndGet () {
	$QUIET || printf "Updating %s/%s\n" "$BASEDIR" "$2"
	checkPerm "$BASEDIR/$2"
	$QUIET || printf "\tDownloading %s to %s/%s\n" "$1" "$BASEDIR" "$2"
	$dler "$1" > "$tmpf" || { Die "$dler $1 exit with $?";}
	checkFileHead "$tmpf"
	$QUIET || printf "\tTemporary location %s to be moved to %s/%s\n" "$tmpf" "$BASEDIR" "$2"
	mv -f "$tmpf" "$BASEDIR/$2"
	$QUIET || printf "\t%s/%s updated.\n" "$BASEDIR" "$2"
}

while getopts ":fq" opt; do
  case $opt in
    f)
      FORCE=true
      ;;
    q)
      QUIET=true
      ;;
    \?)
      printf "Invalid option: -%s\n" "$OPTARG" >&2
      ;;
  esac
done

dler=""
[ -x "$(command -v curl)" ] && dler="curl -sL"
[ -x "$(command -v lwp-request)" ] && dler="lwp-request -m GET"
[ -x "$(command -v wget)" ] && dler="wget -q -O-"

if [ -z "$dler" ]; then
	Die "Unable to find a suitable downloader, please install wget or curl or libwww-perl"
fi

cd "$BASEDIR" || { Die "can't cd to $BASEDIR"; }

LASTUPDATE=$(cat "$BASEDIR/.lastupdate")
OLD_SECONDS=$(( OLD * 86400 ))
CURRENT=$(date +%s)
AGE=$(( CURRENT - LASTUPDATE ))

if [ $FORCE = false ] && [ $AGE -le $OLD_SECONDS ]; then
	Die "The files are kinda new yet (less than $OLD days old)"
fi

for url_and_filename in $(printf "%s\n" "$files_to_get")
do
	url=$(echo "$url_and_filename" | cut -d "|" -f1)
	file_name=$(echo "$url_and_filename" | cut -d "|" -f2)
	goAndGet "$url" "$file_name"
done

rm "$BASEDIR/.lastupdate"
printf "%s\n" "$CURRENT" > "$BASEDIR/.lastupdate"

if [ -x "$(command -v run-parts)" ] && [ -d update.d ] && [ "$RUN_PARSERS" -ne 0 ]; then
	$QUIET || printf "\tRunning parsers from %s/update.d\n" "$BASEDIR"
	run-parts -a "$BASEDIR" -a oui.txt update.d/
	run-parts -a "$BASEDIR" -a iab.txt update.d/
	run-parts -a "$BASEDIR" -a oui.cvs update.d/
	run-parts -a "$BASEDIR" -a iab.cvs update.d/
fi

Zerion Mini Shell 1.0