another technical blog...technically

Monday, April 23, 2018

Another HOWTO about media center on Raspberry pi3 (Part 2/2)

I promised this article would be just a little bit more interesting, indeed i will share the bash scripts i did to manage some activities.

sendip.sh

I've created a simple script which send me an email when the IP changes, or it send me the IP by default at midnight. At first i subscribed to a service called smtp2go, installed ssmtp and configured ssmtp like this:
sudo apt-get install ssmtp mailutils 
sudo nano /etc/ssmtp/ssmtp.conf 

rewriteDomain=smtp2go_ChosenDomain
AuthUser=smtp2go_AccountUsername
AuthPass=smtp2go_AccountPassword
mailhub=mail.smtp2go.com:2525 
UseSTARTTLS=YES 
FromLineOverride=YES 

After that i wrote down those lines:
#!/bin/bash
# Just a script to send me an email with my IP
# Use "sendip" to execute the command and "sendip force" to force email send

# Const
readonly LAST_IP_FILEPATH="/home/pi/scripts/lastIp"
readonly MAIL_RECIPIENT="myemail@email.com"

# Main
CURRENT_IP=$( curl ipinfo.io/ip )
LAST_IP=""

# If 'force' delete IP file content
if [ "$1" = "force" ] || [ ! -e $LAST_IP_FILEPATH ]
then
    echo "[INFO] Creating new file containing IP"
    echo "" > $LAST_IP_FILEPATH
    echo $CURRENT_IP > $LAST_IP_FILEPATH
    echo "[INFO] Sending email containing IP"
    echo "$CURRENT_IP" | mail -s "IP" $MAIL_RECIPIENT
else
    echo "[INFO] File found, getting last ip from file"
    LAST_IP=$( cat $LAST_IP_FILEPATH )
    if [ "$LAST_IP" = "$CURRENT_IP" ]
    then
        echo "[INFO] IP not changed since the last poll, no need to send an email"
    else
        echo "[INFO] Whoah! ip changed, i need to send you the new one"
        echo $CURRENT_IP > $LAST_IP_FILEPATH
        echo "$CURRENT_IP" | mail -s "IP Changed" $MAIL_RECIPIENT
    fi
fi

After i make the scipt executable as a bash command
path="/home/pi/scripts/sendip.sh"
sudo ln -sfT "$path" /usr/local/bin/sendip
chmod +x "$path"

Then, finally, i register the command in crontab, paying attention to change the first row like this. The sendip command will try to understand if the IP changed last time, if yes, it will send you an email with the new public IP.

convert.sh

The other script I created helps you converting media files, so if you have something the media player can't play, you can use the script to launch a media conversion

#!/bin/bash
# Just a wrapper to avconv with my preferred settings


# Const
readonly INPUT_DEFAULT_DIR="/media/Vault/Download/2Convert/"
readonly OUTPUT_DEFAULT_DIR="/media/Vault/Download/"
readonly MAIL_RECIPIENT="youremailaddress@email.com"
readonly MAIL_SUBJECT="LittleBox: File converted"


# Function
sendMail(){
 endEpoch="$(date +%s)"
 
 # Compute the difference in dates in seconds
 tDiff="$(($endEpoch-$startEpoch))"
 # Compute the approximate minute difference
 mDiff="$(($tDiff/60))"
 # Compute the approximate hour difference
 hDiff="$(($tDiff/3600))"
  
 message=""
 if [ $mDiff -gt 59 ]
 then
  message="File $inputFile processed in approx $hDiff hours"
 else
  message="File $inputFile processed in approx $mDiff minutes"
 fi
 
    echo $message | mail -s "$MAIL_SUBJECT" $MAIL_RECIPIENT
}

executeFileConversion() {
 inputFile=$1
 outputDirectory=$2
 startEpoch="$(date +%s)"

 # Get filename and create output file
 filename=$(basename "$inputFile")
 extension="${filename##*.}"
 filename="${filename%.*}"
 outputFile="$outputDirectory$filename.mkv"
 echo "[INFO] Output file will be: $outputFile"
 
 cmd="avconv -i '$1' -c:v libx264 -preset medium -tune film -c:a copy '$outputFile' -y"
 echo "[INFO] Conversion command will be: $cmd"
 eval $cmd
 sendMail $inputFile $startEpoch
}

executeFileConversionDefault() {
 IFS=$'\n'
 files=( $(find $INPUT_DEFAULT_DIR -type f) )
 for i in "${!files[@]}"; do 
  echo "[INFO] Executing conversion of '${files[$i]}'"
  executeFileConversion "${files[$i]}" "$OUTPUT_DEFAULT_DIR"
 done
}


# Main
if [[ $# -eq 0 ]] ; then
    echo "[INFO] No parameter specified, all file in default dir will be processed"
 executeFileConversionDefault
elif [[ $# -eq 2 ]] ; then
 executeFileConversion "$1" "$2"
fi

p2p.sh

I used the last script to shutdown the p2p application when i saw the were decreasing the pi2 performances. The pi3 does not suffer anymore multithreading because it has more firepower, but maybe it could be useful to some of you
#!/bin/bash
# Just a script to start/stop p2p services
# Use "p2p start" to start all registered services and "p2p" stop to shutdown

# Const
startCmd=( )
# Amule
startCmd[0]="sudo /etc/init.d/amule-daemon start"
# Transmission
startCmd[1]="sudo service transmission-daemon start"

stopCmd=( ) 
# Amule
stopCmd[0]="sudo /etc/init.d/amule-daemon stop"
# Transmission
stopCmd[1]="sudo service transmission-daemon stop"


# Functions
execCmd(){
 declare -a argArray=("${!1}")
 for i in "${!argArray[@]}"; do 
  echo "[INFO] Executing command ${argArray[$i]}"
  eval ${argArray[$i]}
 done
 
}


# Main
case $1 in
 "start" )
  echo "[INFO] Starting all registered services"
  execCmd startCmd[@]
  ;;
 "stop" )
  echo "[INFO] Stopping all registered services"
  execCmd stopCmd[@]
  ;;
esac

I think all the script are quite self-explanating and i hope you find it useful. That's all
Share:
written in: Milano MI, Italia

0 commenti:

Post a Comment

Because of a lot of SPAM about courses, I need to moderate all comments here.
I ensure you that I will answer whenever possible (if you are not a spammer).

Me, myself and I

My Photo
I'm just another IT guy sharing his knowledge with all of you out there.
Wanna know more?