Here's a function prototyping script to show you what was talked about today would look like...
Code:
#!/bin/bash
## MAFoElffen, <mafoelffen@ubuntu.com>, 2021.08.31
## Paster()
# Function. Possible Version 4, 2021.09.08
report=$HOME/system-info.txt
startt="$(date '+%F %T %Z (%z)')"
function GetPingStatus()
{
# Takes arg as IP or URL. Returns psuedo boolean $return_status.
ping -c 1 $1 > /dev/null
pingStatus=$?
if [ $pingStatus == 0 ]
then
return_status="true"
else
return_status="false"
fi
}
function nl()
{
echo -e ""
}
function ProgressActive()
{
# Provides a spinner to show progress. Called once by Paster.
declare -a spinner=("\b|" "\b/" "\b-" "\b\\" "\b-")
echo -ne ' '
while test -f "$1"
do
for spin in "${spinner[@]}"
do
stdbuf -oL echo -ne $spin
sleep 0.2
done
done
stdbuf -oL echo -en '\b '
}
function PasteCurl()
{
return_url=$(curl -fsSL -X POST \
--url $target \
--output /dev/null \
--write-out "%{url_effective}\n" \
--data-urlencode "content@${PASTEBIN_CONTENT:-$report}" \
--data "poster=${PASTEBIN_POSTER:-`whoami`@`hostname`}" \
--data "syntax=${PASTEBIN_SYNTAX:-text}" \
--data "expiration=${PASTEBIN_EXPIRATION:-week}")
check_paste=$?
}
function PastePastebinit()
{
return_url=$(pastebinit \
-a "$USER" \
-i "$report" \
-b "$target" \
-t "$sname.txt")
check_paste=$?
}
function PasteWget()
{
return_url=$(wget --quiet -O- --post-data=$report 'termbin.com:9999' )
check_paste=$?
}
function PasteNc()
{
echo -e "This uses a program which sometimes conflicts with "
echo -e "Anti-Virus Programs. You should turn that off if you "
echo -e "have installed."
read -p "Would you like to proceed or skip? (y/N) " ans
if [[ ${ans,} =~ ^[Yy]$ ]]
then
return_url=$(cat $report | nc termbin.com 9999 )
check_paste=$?
else
echo -e "You skipped uplloading to Pastebin."
check_paste=1
fi
}
function CheckCurl()
{
[ -e /usr/bin/curl ];
check_curl=$?
}
function CheckPasterProgs()
{
if [ -e /usr/bin/curl ]
then
paste_prog=1;
elif [ -e /usr/bin/pastebinit ]
paste_prog=2;
elif [ -e /usr/bin/wget ]
paste_prog=3;
elif [ -e /usr/bin/nc ]
paste_prog=4;
else
paste_prog=0;
echo -e "Many utilites were not found. Something is wrong.";
fi
}
function DoPaste()
{
prog_here=$1
case $prog_here in
1)
PasteCurl
;;
2)
PastePastebinit
;;
3)
PasteWget
;;
4)
PasteNc
;;
*)
echo -e "Something went wrong. Error out of range."
exit 1
esac
}
function Paster()
{
# Provides an upload of report with a progress bar. Called once by Main().
target="https://paste.ubuntu.com"
linklog="$HOME/system-info-link.log"
echo -e "$blueback Sensitive data is [REMOVED] from the report file $resetvid"
CheckPasterProgs
## Eval $paste_prog
if [ $paste_prog -ne 0 ]
then
read -p "Do you want to upload the report file fo '$target'? (y/N) " ans
if [[ ${ans,} =~ ^[Yy]$ ]]
then
GetPingStatus "www.ubuntu.com"
if [ "$return_status" == "true" ]
then
echo "Uploading '$report' to '$target'"
echo -e "This may take 1-2 minutes..."
nl
tput civis
keep_spinning=$(mktemp)
ProgressActive "$keep_spinning" & pid_progressactive=$!
DoPaste $paste_prog
if [ $check_paste -eq 0 ]
then
paste_done=true
else
paste_done=false
fi
rm "$keep_spinning"
sleep 0.5
tput cnorm
if [ $paste_done == 0 ]
then
echo -e "Uploaded Report: ${startt}:" >> "$linklog"
echo -e "$blueback Upload successful $resetvid"
nl
echo -e "The link to the pastebin is saved in: '$linklog'"
nl
echo -e "View at: $return_url" | tee -a "$linklog"
nl
else
echo -e "$redback Upload failed $resetvid"
echo -e "Upload the file manually: 'copy & paste' to"
echo -e "https://paste.ubuntu.com/"
nl
fi
else
echo -e "No connection to pastebin.ubuntu.com " | tee -a "$linklog"
echo -e "Please copy the file $report to a USB and post "
echo -e "from a computer that does have a working connection."
echo -e "Upload the file manually: 'copy & paste' to"
nl
echo -e "https://paste.ubuntu.com/"
fi
fi
else
echo -e "$redback The required package was not installed. $resetvid"
nl
echo -e "Either rerun the script after installing curl, or"
echo -e "Upload the file manually: 'copy & paste' to"
nl
echo -e "https://paste.ubuntu.com/"
nl
fi
}
## MAIN ##
Paster
Explained:
First checks the programs needed to do a Paste to a Pastebin, in order of preference. The first two, are not default installed to all flavors, but will paste to paste.ubuntu.com. The next two are default to all flavor's and will paste to termbin.com.
On the paste, it uses the method related to the preferred program found.
Bookmarks