kyopages/scripts/getInfo

504 lines
16 KiB
Plaintext
Raw Normal View History

2017-07-30 17:08:05 +02:00
#!/bin/bash
2017-08-02 05:30:37 +02:00
version=0.5.0
2017-08-02 01:16:24 +02:00
date="08/2017"
projet="simpledeb"
2017-07-30 17:08:05 +02:00
contact="IRC freenode ##sdeb ou https://framagit.org/kyodev/kyopages/issues"
script="getInfo"
urlScript="https://framagit.org/kyodev/kyopages/raw/master/scripts/getInfo"
urlNotice=""
##### license LPRAB/WTFPL
# auteur: simpledebian
# contributeurs: kyodev
#####
2017-08-02 01:16:24 +02:00
PATH='/bin:/usr/bin:/sbin' ; IFS=' '
2017-07-30 17:08:05 +02:00
f__affichage(){
# définition couleurs
YELLOW="$(printf "\033[0;33m")" # ?
GREEN="$(printf "\033[0;32m")" # ok
BLUE="$(printf "\033[0;34m")" # info
RED="$(printf "\033[0;31m")" # alerte
COLOR="$(printf "\033[0m")" # standard
# clear
printf "$BLUE"
cat << 'EOF'
_ ___ __
__ _ ___| |_|_ _|_ __ / _| ___
/ _' |/ _ \ __|| || '_ \| |_ / _ \
| (_| | __/ |_ | || | | | _| (_) |
\__, |\___|\__|___|_| |_|_| \___/
|___/
EOF
echo -e "$YELLOW version $version - $date$COLOR\n"
}
# affichage $1 en rouge, $1++ optionnels en bleu, sortie script sur erreur
f__error() {
echo -e "\n$RED Erreur critique: $1 $COLOR"
for (( i=2 ; i<=$# ; i++ )); do echo -e " $BLUE${!i}"; done
echo "$COLOR"
exit 1
}
# affichage des paramètres en bleu, ligne vide à la fin
f__info() {
for (( i=1 ; i<=$# ; i++ )); do echo -e " $BLUE${!i}"; done
echo "$COLOR"
}
# detect system architecture, assign $architecture : i386 | x86_64, return 1 on unknown architecture
# remarque, debian: dpkg --print-architecture affiche i386
f__architecture(){
case "$(uname -m)" in
amd64 | x86_64 )
architecture="x86_64";;
i?86 | x86 )
architecture="i686";;
* )
case "$(getconf LONG_BIT)" in
64 )
architecture="x86_64";;
32 )
architecture="i686";;
*)
return 1
esac ;;
esac
}
# test dépendances (commandes disponibles), $1 liste commandes ou $1...$x, si manquant, avertissement des paquets manquants
f__requis(){
local dependsFail
for i in $@; do
if [ ! "$(which $i)" ]; then
dependsFail+="$i "
fi
done
[ -z "$dependsFail" ] || f__error "paquet(s) manquant(s)" \
"vous devez avoir le(s) paquet(s) $i pour exploiter ce script, installation,$RED en root$COLOR:" \
"apt install $dependsFail"
}
2017-08-02 05:30:37 +02:00
fi_start(){
os=$(uname -s)
hote=$(uname -n)
dateRapport=$(date +"%d %b %Y %H:%M %z")
echo -e "# getInfo $version sur $hote / $os \n" > "$fileOutput"
echo -e "Rapport au $dateRapport" >> "$fileOutput"
}
2017-07-30 17:08:05 +02:00
trim_quotes() { # thanks neofetch
trim_output="${1//\'}"
trim_output="${trim_output//\"}"
printf "%s" "$trim_output"
}
2017-08-02 01:16:24 +02:00
get_de() { # thanks neofetch, assigne $de
2017-07-30 17:08:05 +02:00
de="${XDG_CURRENT_DESKTOP/i3}"
de="${de/'X-'}"
de="${de/Budgie:GNOME/Budgie}"
# Fallback to using xprop.
[[ -n "$DISPLAY" && -z "$de" ]] && de="$(xprop -root | awk '/KDE_SESSION_VERSION|^_MUFFIN|xfce4|xfce5/')"
# Format strings
case "$de" in
"KDE_SESSION_VERSION"*) de="KDE${de/* = }" ;;
*"TDE_FULL_SESSION"*) de="Trinity" ;;
*"MUFFIN"* | "Cinnamon") de="$(cinnamon --version)"; de="${de:-Cinnamon}" ;;
*"xfce4"*) de="XFCE4" ;;
*"xfce5"*) de="XFCE5" ;;
esac
}
2017-08-02 01:16:24 +02:00
get_distro() { # thanks neofetch, assigne $distro
2017-07-30 17:08:05 +02:00
[[ "$distro" ]] && return
case "$os" in
"Linux" | "GNU")
if type -p lsb_release >/dev/null; then
case "$distro_shorthand" in
"on") lsb_flags="-sir" ;;
"tiny") lsb_flags="-si" ;;
*) lsb_flags="-sd" ;;
esac
distro="$(lsb_release $lsb_flags)"
elif type -p guix >/dev/null; then
distro="GuixSD"
elif type -p crux >/dev/null; then
distro="$(crux)"
case "$distro_shorthand" in
"on") distro="${distro//version}" ;;
"tiny") distro="${distro//version*}" ;;
esac
else
# Source the os-release file
for file in /etc/os-release /usr/lib/os-release /etc/*release /usr/lib/*release; do
source "$file" && break
done
# Format the distro name.
case "$distro_shorthand" in
"on") distro="${NAME:-${DISTRIB_ID}} ${VERSION_ID:-${DISTRIB_RELEASE}}" ;;
"tiny") distro="${NAME:-${DISTRIB_ID:-${TAILS_PRODUCT_NAME}}}" ;;
"off") distro="${PRETTY_NAME:-${DISTRIB_DESCRIPTION}} ${UBUNTU_CODENAME}" ;;
esac
# Workarounds for distros that go against the os-release standard.
[[ -z "${distro// }" ]] && distro="$(awk '/BLAG/ {print $1; exit}' /etc/*ease /usr/lib/*ease)"
[[ -z "${distro// }" ]] && distro="$(awk -F '=' '{print $2; exit}' /etc/*ease /usr/lib/*ease)"
fi
2017-08-01 01:48:22 +02:00
# for debian, add version
2017-08-02 05:30:37 +02:00
grep -qi 'Debian' /etc/issue && distro="$(sed 's/"//g' <<< $distro) $(cat /etc/debian_version)"
2017-07-30 17:08:05 +02:00
distro="$(trim_quotes "$distro")"
;;
esac
[[ -z "$distro" ]] && distro="$os (Unknown)"
}
2017-08-02 05:30:37 +02:00
get_wm() { # thanks neofetch, assigne $wm
if [ -n "$DISPLAY" ]; then
id="$(xprop -root -notype | awk '$1=="_NET_SUPPORTING_WM_CHECK:"{print $5}')"
wm="$(xprop -id "$id" -notype -f _NET_WM_NAME 8t)"
wm="${wm/*_NET_WM_NAME = }"
wm="${wm/\"}"
wm="${wm/\"*}"
# Fallback for Wayland wms
[[ "$wm" == "xwlc" ]] && \
wm="$(ps -e | grep -m 1 -o -F -e "sway" -e "orbment" -e "velox" -e "orbital")"
fi
}
get_cpu() { # thanks neofetch, assigne $cpu
cpu="$(awk -F ': | @' '/model name|Processor/ {printf $2; exit}' /proc/cpuinfo)"
local speed_dir="/sys/devices/system/cpu/cpu0/cpufreq"
local temp_dir="/sys/class/hwmon/hwmon0/temp1_input"
# Get cpu speed
if [[ -d "$speed_dir" ]]; then
# Fallback to cpuinfo_max_freq if $speed_type fails
local speed="$(< "${speed_dir}/${speed_type}")" || local speed="$(< "${speed_dir}/bios_limit")" || \
local speed="$(< "${speed_dir}/scaling_max_freq")"
speed="$((speed / 100000))"
else
local speed="$(awk -F ': |\\.' '/cpu MHz/ {printf $2; exit}' /proc/cpuinfo)"
local speed="$((speed / 100))"
fi
# Get cpu temp
if [ -f "$temp_dir" ]; then
temp="$(< "$temp_dir")"
temp="$((temp * 100 / 10000))"
temp="[${temp/${temp: -1}}.${temp: -1}°C]"
fi
# Show/hide hyperthreaded cores
cores="$(grep "^core id" /proc/cpuinfo | sort -u | wc -l)" # physique
coresT="$(grep -c "^processor" /proc/cpuinfo)" # logique
# Fix for speeds under 1ghz
if [[ -z "${speed:1}" ]]; then speed="0.${speed}"; else speed="${speed:0:1}.${speed:1}"; fi
cpu="$cpu @ ${speed}GHz $temp"
# Remove uneeded patterns from cpu output, this is faster than sed/gsub
cpu="${cpu//(tm)}"
cpu="${cpu//(TM)}"
cpu="${cpu//(r)}"
cpu="${cpu//(R)}"
cpu="${cpu//CPU}"
cpu="${cpu//Processor}"
cpu="${cpu//Dual-Core}"
cpu="${cpu//Quad-Core}"
cpu="${cpu//Six-Core}"
cpu="${cpu//Eight-Core}"
cpu="${cpu//Core}"
cpu="${cpu//with Radeon * Graphics}"
# Add cpu cores to output
cpu="${cpu/@/(${cores})(${coresT}) @}"
}
get_gpu() { # thanks neofetch, assigne $gpu
gpu="$(PATH="/sbin:$PATH" lspci -mm | awk -F '\\"|\\" \\"' '/"Display|"3D|"VGA/ {print $3 " " $4}')"
case "$gpu" in
*"advanced"*)
gpu="${gpu//Intel*$'\n'}"
gpu="${gpu/'[AMD/ATI]' }"
gpu="${gpu/'[AMD]' }"
gpu="${gpu/OEM }"
gpu="${gpu/ \/ *}"
gpu="${gpu/*\[}"
gpu="${gpu/\]*}"
gpu="AMD $gpu"
;;
*"nvidia"*)
gpu="${gpu//Intel*$'\n'}"
gpu="${gpu/*\[}"
gpu="${gpu/\]*}"
gpu="NVIDIA $gpu"
;;
*"virtualbox"*)
gpu="VirtualBox Graphics Adapter"
;;
esac
[[ "$gpu" =~ "intel" ]] && gpu="Intel Integrated Graphics"
}
get_memory() { # thanks neofetch, assigne $memory
# MemUsed = Memtotal + Shmem - MemFree - Buffers - Cached - SReclaimable
# Source: https://github.com/KittyKatt/screenFetch/issues/386#issuecomment-249312716
while IFS=":" read -r a b; do
case "$a" in
"MemTotal") mem_used="$((mem_used+=${b/kB}))"; mem_total="${b/kB}" ;;
"Shmem") mem_used="$((mem_used+=${b/kB}))" ;;
"MemFree" | "Buffers" | "Cached" | "SReclaimable") mem_used="$((mem_used-=${b/kB}))" ;;
esac
done < /proc/meminfo
local mem_used="$((mem_used / 1024))"
local mem_total="$((mem_total / 1024))"
memory="${mem_used}Mo / ${mem_total}Mo"
}
get_resolution() { # thanks neofetch, assigne $resolution
if type -p xrandr >/dev/null; then
resolution="$(xrandr --nograb --current | awk 'match($0,/[0-9]*\.[0-9]*\*/) {printf $1 " @ " substr($0,RSTART,RLENGTH) "Hz, "}')"
resolution="${resolution//\*}"
resolution="${resolution//\.[0-9][0-9]}"
elif type -p xdpyinfo >/dev/null; then
resolution="$(xdpyinfo | awk '/dimensions:/ {printf $2}')"
fi
resolution="${resolution%,*}"
}
get_disk() { # thanks neofetch, assigne $disk
# df flags
local df_flags="-h -l --total"
local df_dir="total"
# Get the disk info
disk="$(df $df_flags | awk -v dir="$df_dir" '$0 ~ dir {print $2 ":" $3 ":" $5}')"
# Format the output
local disk_used="${disk#*:}"
disk_used="${disk_used%%:*}"
local disk_total="${disk%%:*}"
disk_total_per="${disk#*:*:}"
# Put it all together
disk="${disk_used} / ${disk_total} (${disk_total_per})"
}
get_battery() { # thanks neofetch, assigne $battery
# We use 'prin' here and exit the function early so that we can do multi battery support with a single battery per line.
for bat in "/sys/class/power_supply/BAT"*; do
local capacity="$(< "${bat}/capacity")"
local status="$(< "${bat}/status")"
# Fix for bash on Windows 10 which includes /proc files for battery usage despite there not being a battery installed.
[[ -z "$capacity" ]] && return
battery+="${capacity}% [${status}]"
done
return
[[ "$battery_state" ]] && battery+=" En charge"
}
get_local_ip() { # thanks neofetch, assigne $local_ip
local_ip="$(ip route get 1 | awk '{print $NF;exit}')"
}
get_shell(){ # thanks neofetch, assigne $shell
shell="${SHELL##*/}"
case "${SHELL##*/}" in
"bash")
shell+=" ${BASH_VERSION/-*}" ;;
"zsh")
shell+="$(zsh --version)"
shell="${shell/ zsh}" ;;
"mksh" | "ksh")
shell+="$("$SHELL" -c 'printf "%s" "$KSH_VERSION"')"
shell="${shell/ * KSH}" ;;
"tcsh" | "csh")
shell+="$("$SHELL" --version)"
shell="${shell/tcsh}"
shell="${shell/\(*}" ;;
"fish")
shell+="$(fish -c 'printf "%s" "$FISH_VERSION"')" ;;
esac
}
2017-08-02 01:16:24 +02:00
fi_systeme(){
local uname=$(uname -rmo)
#dépend de apt install lsb-release, debian standard juste : lsb-base
[ -x "/usr/bin/lsb_release" ] && local description=$(lsb_release -ds)
local uptime=$(uptime -p)
2017-08-02 05:30:37 +02:00
uptime=$(sed 's/days/jours/; s/hours/heures/; s/minutes/mn/' <<< $uptime)
2017-08-02 01:16:24 +02:00
local lastboot=$(last -R -n 1 --time-format iso reboot | head -n 1 | grep -o '[0-9:T+-]*' | sed 's/T/ /')
local bootImage=$(sed 's/root=[^ ]*//;s/[ ]\+/, /g' /proc/cmdline)
2017-08-02 05:30:37 +02:00
local shells=$(grep -v "^#" /etc/shells | sed 's/\/bin\///' | tr '\n' ' ')
2017-08-02 01:16:24 +02:00
local text="\n## système\n\n"
2017-08-02 05:30:37 +02:00
get_distro; text+="$distro \n"
2017-08-02 01:16:24 +02:00
text+="$bootImage \n"
text+="dernier boot: $lastboot, uptime: $uptime \n"
[ $description ] && text+="description: $description \n"
text+="uname: $uname \n"
2017-08-02 05:30:37 +02:00
get_de; text+="desktop: $de \n"
get_wm; text+="wm: $wm \n"
get_shell; text+="shell: $shell \n"
text+="shells possibles: $shells \n"
get_cpu; text+="CPU: $cpu \n"
get_gpu; text+="GPU: $gpu \n"
get_memory; text+="mémoire utilisée/totale: $memory \n"
get_resolution; text+="résolution: $resolution \n"
get_disk; text+="Disque(s): $disk \n"
get_battery; text+="Batteries(s): $battery \n"
get_local_ip; text+="IP locale: $local_ip \n"
2017-08-02 01:16:24 +02:00
echo -e "$text" >> "$fileOutput"
}
fi_reseau(){
local netcards=$(lspci -nnk | grep -iEA 3 'network|ethernet')
local ifconfig=$(ip address show)
2017-08-02 05:30:37 +02:00
local iwconfig=$(iwconfig 2>&1 | grep -v 'no wireless extensions' | grep -v '^$')
2017-08-02 01:16:24 +02:00
local interfaces=$(sed '/^#/d;s/^wpa-psk [[:graph:]]\+/wpa-psk <WPA key removed>/;/^$/d;s/\(.*\)/\1 /' /etc/network/interfaces)
local route=$(ip route show)
local resolv=$(grep -v '^#' /etc/resolv.conf)
2017-08-02 05:30:37 +02:00
NETMGRPATHS=("/usr/sbin/NetworkManager" "/usr/sbin/wicd" "/usr/sbin/connmand") # thanks wireless-info
NETMGRNAMES=("NetworkManager" "Wicd" "ConnMan") # thanks wireless-info
2017-08-02 01:16:24 +02:00
for ii in "${!NETMGRPATHS[@]}"; do
if [ -f "${NETMGRPATHS[$ii]}" ]; then NETMGRINST+="${NETMGRNAMES[$ii]}"; fi
if [ "$(ps -ef | grep -c ${NETMGRPATHS[$ii]})" -ge 2 ]; then netmgrrun+=$(ps -ef | grep -o "${NETMGRPATHS[$ii]}.*$" | head -n 1); fi
done
local text="\n## réseau\n\n"
text+="**lspci -nnk | grep -iEA 3 'network|ethernet'** \n"
text+='```\n'
text+="$netcards \n"
text+='```\n'
text+="**ip address show** \n"
text+='```\n'
text+="$ifconfig \n"
text+='```\n'
text+="**iwconfig** \n"
text+='```\n'
text+="$iwconfig \n"
text+='```\n'
text+="**/etc/network/interfaces** \n"
text+='```\n'
text+="$interfaces \n"
text+='```\n'
text+="**ip route show** \n"
text+='```\n'
text+="$route \n"
text+='```\n'
2017-08-02 05:30:37 +02:00
text+="**/etc/resolv.conf** \n"
2017-08-02 01:16:24 +02:00
text+='```\n'
text+="$resolv \n"
text+='```\n'
text+="**network managers** \n"
2017-08-02 01:20:23 +02:00
[ "$NETMGRINST" ] && text+="installé(s): $NETMGRINST \n" || text+="non trouvé parmi: ${NETMGRNAMES[@]} \n"
2017-08-02 01:16:24 +02:00
text+="en fonctionnement: "
[ "$netmgrrun" ] && text+="$netmgrrun" || text+="non trouvé\n"
echo -e "$text" >> "$fileOutput"
}
2017-08-02 05:30:37 +02:00
fi_nm_wifi_autour(){
if [ -x /usr/bin/nmcli ]; then
echo nmcli -f SSID,BSSID,MODE,CHAN,FREQ,RATE,SIGNAL,BARS,SECURITY,ACTIVE,IN-USE device wifi list
else echo "NetworkManager n'est pas installé? (paquet network-manager)"; fi
}
fi_NetworkManager(){
# etat NM cat -s /var/lib/NetworkManager/NetworkManager.state
# conf NM grep -v '^#' /etc/NetworkManager/NetworkManager.conf
# root cat /etc/NetworkManager/system-connections/ssid
# liste canaux wifi [ -x /sbin/iwlist ] && /sbin/iwlist chan 2>&1| grep -v 'no frequency information' || echo "paquet wireless-tools à installer"
echo
}
2017-08-02 01:16:24 +02:00
fi_modules(){ # thanks wireless-info, $1 net|all
[ "$1" == "net" ] && local text="\n## modules réseau\n\nliste non garantie complète \n" || \
local text="\n\n## tous les modules\n\n**lsmod** \n"
if [ "$1" == "net" ]; then
local MODMATCHES="(air|ar5|at7|ath[^3]?|b43|bcma|brcm|carl|ipw|iwl|ndis|r(818|8192[eu]|871|92su)|8(188|189|192|723|812)[acde][esu]|rt[23567]|rtl|ssb|wl|(cfg|mac)80211)"
local LSMODMATCHES="(wmi|(dell|ideapad)[-_]laptop)"
local modules=$(lsmod | grep -E "(^|[[:punct:] ])($MODMATCHES|$LSMODMATCHES)[^[:punct:] ]*([[:punct:] ]|$)")
else local modules=$(lsmod); fi
text+='```\n'
text+="$modules \n"
text+='```\n'
echo -e "$text" >> "$fileOutput"
2017-08-02 05:30:37 +02:00
# grep -v '^#' /etc/modules
2017-08-02 01:16:24 +02:00
}
2017-08-01 01:48:22 +02:00
fi_curl_markdownshare(){
[ -e "$1" ] || f__error "fichier $1 inexistant"
curl -H "Accept: application/json" -X POST -F "text=<$1" https://markdownshare.com/create/
2017-07-30 17:08:05 +02:00
#-A, --user-agent and -e, --referer options
#If you wish to allow a post to expire then add an expire= parameter too:
#expire=Nh Expire in N hours.
#expire=Nd Expire in N days.
2017-08-01 01:48:22 +02:00
#-d expire ? ou --data expire
}
2017-08-02 05:30:37 +02:00
# $1 fichier à exporter, $2 durée de conservation en jour; $3 debug
2017-08-01 01:48:22 +02:00
fi_curl_pastery(){
[ -e "$1" ] || f__error "fichier $1 inexistant"
# curl -X POST "https://www.pastery.net/api/paste/?title=getInfo&language=markdown" -F file=@$1
2017-08-02 05:30:37 +02:00
local curl=$(curl --silent -X POST "https://www.pastery.net/api/paste/?title=getInfo_$version&language=markdown&duration=$(($2*1440))" --data-binary @$1)
local id=$(echo $curl | cut -d '"' -f 4)
2017-08-01 01:48:22 +02:00
f__info "\n\n votre paste:" "https://www.pastery.net/$id/"
2017-08-02 05:30:37 +02:00
f__info "$GREEN le rapport est aussi disponible en local, fichier: $fileOutput"
[ "$3" == "debug" ] && echo -e "\n$curl\n"
# ?api_key=<api_key>
# &duration=<duration> en mn, 1 jour par défaut
# &language=autodetect possible
# &max_views=<max_views>
# 100ko max
2017-07-30 17:08:05 +02:00
}
# initialisation
f__affichage
2017-08-02 01:16:24 +02:00
f__architecture || f__error "Architecture non supportée" \
"vous pouvez contacter contacter $projet $contact pour parfaire le script"
2017-08-01 01:48:22 +02:00
f__requis "curl"
2017-08-02 01:16:24 +02:00
fileOutput="getInfo_rapport.md"
2017-07-30 17:08:05 +02:00
2017-08-02 05:30:37 +02:00
fi_start
2017-08-02 01:16:24 +02:00
fi_systeme
fi_reseau
fi_modules "net"
2017-08-01 01:48:22 +02:00
2017-08-02 01:20:23 +02:00
#cat $fileOutput
2017-08-02 05:30:37 +02:00
fi_curl_pastery "$fileOutput" 1
2017-08-01 01:48:22 +02:00
2017-08-02 05:30:37 +02:00
# a tester fi_curl_markdownshare "$fileOutput"
2017-07-30 17:08:05 +02:00
echo
exit
2017-08-01 01:48:22 +02:00
{
"raw":"https://markdownshare.com/raw/b837c09d-d5ec-4d0b-afb8-a01efe31f350",
"delete":"https://markdownshare.com/delete/cb43e94336dd5cae2d5b2b8a077f700b",
"edit":"https://markdownshare.com/edit/cb43e94336dd5cae2d5b2b8a077f700b",
"link":"https://markdownshare.com/view/b837c09d-d5ec-4d0b-afb8-a01efe31f350",
"id":"b837c09d-d5ec-4d0b-afb8-a01efe31f350",
"auth":"cb43e94336dd5cae2d5b2b8a077f700b"
}
2017-08-02 05:30:37 +02:00
{"id": "pngznq", "title": "getInfo_0.5.0", "url": "https://www.pastery.net/pngznq/", "language": "markdown", "duration": 1438}
2017-07-30 17:08:05 +02:00
bug markdownshare:
2017-08-01 01:48:22 +02:00
'```' pas interprêté en <pre><code>
2017-07-30 17:08:05 +02:00
style.css ligne 167
pre {
2017-08-01 01:48:22 +02:00
padding:10px;
margin:0 0px 20px 0px;
font-size:0.8em
2017-07-30 17:08:05 +02:00
}
2017-08-01 01:48:22 +02:00
toc auto markdown:
http://ndossougbe.github.io/strapdown/
à voir:
https://www.pastery.net/api/
2017-08-02 05:30:37 +02:00
license non libre? pas de depot, mais ça marche
2017-08-02 01:16:24 +02:00
wget https://framagit.org/kyodev/kyopages/raw/master/scripts/getInfo
chmod +x getInfo && ./getInfo