another technical blog...technically

Showing posts with label Raspberry. Show all posts
Showing posts with label Raspberry. Show all posts

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:

Monday, April 16, 2018

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

Hi guys, it's a long time i don't touch my raspberry pi 2 media server (from now LittleBox). So with the raspberry pi3 release, i decided to do a little upgrade and creating the new LittleBox. which is the same as the old one, but with pi3, so more powerful.
Because of i have lost all the old scripts during SD formatting i decided to rewrite them and to share everything with you :-).
For the old version i decided to use just the command line version of Raspbian, so i controlled it using PuTTY sessions from my own pc (just like the old-fashioned way) this time i noticed the default version is the one with UI so ... why not? and this also has driven me to change some of the installed softwares.

Goals

My hardware configuration is about a Raspberry pi3, with a little fan, an attached HDD of 2TB formatted in NTFS and a ethernet connection. My goal is to create a small PC based on latest RaspBian installation that acts as a:
  • Media center 
  • Home Backup NAS 
  • Download station
So i've installed the following:
  • aMule: old but good... maybe 
  • avconv: useful for media conversion 
  • Plex: THE media server 
  • Transmission: just a torrent daemon 
  • VLC & AV codec: you never know 
  • Dos2Unix: sometimes used when i edit some files from Windows PC 
  • Fail2Ban: useful if you expose your little server on the internet 
  • MailUtils: utilities to send email, useful to send some mails directly to me 
  • Monit: useful to monitor some services 
  • NTFS-3G: drivers for NTFS filesystem
  • SMB Server: the best way to share files between a UNIX like system and a Windows one 

HDD Install

Let's create a folder to mount the HDD
sudo mkdir /media/Vault
sudo chmod 777 /media/Vault 
then install the NTFS drivers:
sudo apt-get install ntfs-3g
then edit the fstab file
sudo nano /etc/fstab
and add the following lines
# Custom
/dev/sda1    /media/Vault   ntfs-3g   rw,default   0   0 
Now if you reboot, the HDD will results mounted at /media/Vault

Setup SMB Sharing 

Let's now setup the SMB share, at first let's install the package
sudo apt-get install samba samba-common-bin
sudo apt-get install cifs-utils
Then let's edit the smb.conf adding the following lines
sudo nano /etc/samba/smb.conf
wins support = yes [pi] 
   comment= Pi Home 
   path=/home/pi 
   browseable=Yes 
   writeable=Yes 
   only guest=no 
   create mask=0777 
   directory mask=0777 
   public=no 

[Vault] 
   comment= Vault 
   path=/media/Vault 
   browseable=Yes 
   writeable=Yes 
   only guest=no 
   create mask=0777 
   directory mask=0777 
   public=no 
after that, you need to change the SMB password for pi user:
sudo smbpasswd -a pi 
Now you'll be able to access the pi home and the external HDD with a Windows PC.

InstAll

Be sure you have enabled SSH and VNC.
Now it's time to install aMule and Transmission and configure them to be accessible from the web
In this script i download the amule daemon and i get an encrypted version of the chosen password i will set up for the user who login to the web aMule server.
sudo apt-get install amule-daemon amule-utils 
amuled –f  
amuleweb -w 
echo -n YourPreferredPassword | md5sum | cut -d ' ' -f 1 
dc9dc28b924dc716069dc60fbdcbdc30 

nano /home/pi/.aMule/amule.conf  
Here the rows of the file i want to edit, note that i use the external HDD to store Temp files and incoming file cause i want to reduce as much as i can the write operations on the SD card:
[eMule] 
AddServerListFromServer=1 
AddServerListFromClient=1 
SafeServerConnect=1 
...
TempDir=/media/Vault/Download/Temp 
IncomingDir=/media/Vault/Download 
...

[ExternalConnect] 
AcceptExternalConnections=1 
ECAddress=127.0.0.1 
ECPort=4712 
ECPassword=dc9dc28b924dc716069dc60fbdcbdc30 

[WebServer] 
Enabled=1 
Password=dc9dc28b924dc716069dc60fbdcbdc30 
PasswordLow=dc9dc28b924dc716069dc60fbdcbdc30 
...
After that we just need to change the default amule user who is pi:
sudo nano /etc/default/amule-daemon 
AMULED_USER="pi" 
Now aMule will be available at port 4711 via browser, to make it available as soon as the server is reeboted, we can use crontab, so:
crontab -e 
#Amule 
@reboot amuled -f 
It's time to install Trasmission daemon and setup some settings so:
sudo apt-get install  transmission-daemon 
sudo nano /etc/transmission-daemon/settings.json 
Here the configuration i use, i think they are really self-descriptive:
"blocklist-enabled": true, 
"blocklist-url": "http://john.bitsurge.net/public/biglist.p2p.gz", 
"download-dir": "/media/Vault/Download" 
"incomplete-dir": "/media/Vault/Download/Temp" 
"incomplete-dir-enabled": true 
"peer-port-random-on-start": false, 
"port-forwarding-enabled": true, 
rpc-password: YourPreferredPassword, 
rpc-username: pi,  
rpc-whitelist: *.*.*.* 
sudo /etc/init.d/transmission-daemon reload 
sudo /etc/init.d/transmission-daemon restart 
Now let's install Plex Media Server, using a custom repository from dev2day
sudo apt-get update && sudo apt-get install apt-transport-https -y --force-yes 
wget -O - https://dev2day.de/pms/dev2day-pms.gpg.key | sudo apt-key add - 
echo "deb https://dev2day.de/pms/ jessie main" | sudo tee /etc/apt/sources.list.d/pms.list 
sudo apt-get update 
sudo apt-get install plexmediaserver -y 
sudo apt-get install libexpat1 -y 
sudo apt-get install mkvtoolnix -y 

sudo service plexmediaserver restart 

sudo nano /etc/default/plexmediaserver 
PLEX_MEDIA_SERVER_TMPDIR=/media/Vault/Download/Temp 
PLEX_MEDIA_SERVER_USER=pi 
sudo chown pi /var/lib/plexmediaserver/ 
and now we can install al the other software stated before
sudo apt-get install libav-tools libavcodec-extra vlc dos2unix ufw fail2ban
Now it's time for Monit which will help us to quickly understand what's going on on our little server
sudo apt-get install monit 
sudo nano /etc/monit/monitrc
set httpd port 2812 address 0.0.0.0 
   allow 0.0.0.0/0.0.0.0 
   allow pi:YourPreferredPassword 

check process aMule matching "amuled" 
   start program = "/etc/init.d/amule-daemon start" 
   stop program = "/etc/init.d/amule-daemon stop" 
   if failed host 127.0.0.1 port 4711 then restart 

check process Plex with pidfile "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/plexmediaserver.pid" 
    start program = "/etc/init.d/plexmediaserver start" 
    stop  program = "/etc/init.d/plexmediaserver stop" 
    if failed port 32400 type tcp then restart 
    if 3 restarts within 5 cycles then alert 

check process SSHd 
    with pidfile "/var/run/sshd.pid" 
    start program = "/etc/init.d/sshd start" 
    stop program = "/etc/init.d/sshd stop" 
    if 3 restarts within 3 cycles then alert 
    if failed port 22 protocol ssh then restart 

check process Transmission matching "transmission-daemon" 
    start program = "/etc/init.d/transmission-daemon start" 
    stop program  = "/etc/init.d/transmission-daemon stop" 
    if failed host 127.0.0.1 port 9091 type TCP for 2 cycles then restart 
    if 2 restarts within 3 cycles then unmonitor 
So now everything is lock and load but in the second part, that i promise, it will be more interesting, "i'll introduce some custom scripts that will help you to manage your personal littlebox, stay tuned

Saturday, June 20, 2015

Onion Pi: Set up as a Wifi-to-Wifi Tor middlebox

Some weeks ago i bought a Raspberry Pi 2 in order to replace my old Raspberry Pi as media center...
So i had some free time and unused spare wifi connectors and i decided to create a tor access point using this guides.
https://learn.adafruit.com/onion-pi/overview  
https://learn.adafruit.com/setting-up-a-raspberry-pi-as-a-wifi-access-point/overview
This access point is completely useless in my home, but i was curious about it, outside it was rainy and you know, cuorisity is the cure fore boredom.
Those guides are quite self-explaining, but what if you want to set up a Wi-Fi to Wi-Fi middlebox?
The guide does not provides so muchi infos about it.
Let's assume:
eth0: adapter you connect to the main router wlan0: adapter you want to connect to the main router wlan1: adapter you want to use as access point
what you have to do using iptables is:
Create a network translation between the adapter wlan1 and the adapter wlan0 (in my case also hostap uses as interface wlan1)
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
sudo iptables -A FORWARD -i wlan0 -o wlan1 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan1 -o wlan0 -j ACCEPT
Set up new iptables rules, redirecting also wlan0 traffic to torrc TransPort 9040 (adding also the well knows exceptions)
sudo iptables -t nat -D PREROUTING -i wlan0 -p tcp --dport 22 -j REDIRECT --to-ports 22
sudo iptables -t nat -D PREROUTING -i wlan0 -p udp --dport 53 -j REDIRECT --to-ports 53
sudo iptables -t nat -D PREROUTING -i wlan0 -p tcp --syn -j REDIRECT --to-ports 9040
That's all folks.
Share:

Me, myself and I

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