Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

2014-09-29

Using rsync over ssh

rsync-ssh-download.bash
-----------------------------------------------------------------------
#! /bin/bash

# SOURCE
SSH_PORT=22
SSH_USER="userA"
SSH_HOST="192.168.0.5"
SSH_PATH="/home/userA/"
# DESTINATION
DST="/home/userB/backup/"
DST_CHOWN="userB"

SRC="${SSH_USER}@${SSH_HOST}:${SSH_PATH}"

CHECK_FAIL=0

    if [ ! -f "/usr/bin/ssh" ]; then
    echo "ssh not found!"
    CHECK_FAIL=1   
    fi

    if [ ! -f "/usr/bin/rsync" ]; then
    echo "rsync not found!"
    CHECK_FAIL=1
    fi
   
    if [ ! -f "/usr/bin/sshpass" ]; then
    echo "sshpass not found!"
    CHECK_FAIL=1
    fi
   
    if [ "$(id -u)" != "0" ]; then
    echo "not root"
    CHECK_FAIL=1
    fi
   
    if [ $CHECK_FAIL -eq 1 ];then
    exit 0
    fi

   
read -s -p "Enter SSH Password: " pswd
echo ""
echo "Connecting..."

/usr/bin/rsync --verbose --recursive --append --links --copy-dirlinks \
--hard-links --perms --executability --times --delete --chown=${DST_USER}:${DST_USER} \
 --rsh="/usr/bin/sshpass -p '${pswd}' ssh -p ${SSH_PORT} -o StrictHostKeyChecking=no -l ${SSH_USER}" $SRC  $DST

du ${DST}  --max-depth=0 --block-size=MB
echo "Complete"

-----------------------------------------------------------------------
rsync-ssh-upload.bash
-----------------------------------------------------------------------
#! /bin/bash

# SOURCE
SRC="/home/userB/backup/"
# DESTINATION
SSH_PORT=22
SSH_USER="userA"
SSH_HOST="192.168.0.5"
SSH_PATH="/home/userA/"
DST_CHOWN="userA"

DST="${SSH_USER}@${SSH_HOST}:${SSH_PATH}"

CHECK_FAIL=0

    if [ ! -f "/usr/bin/ssh" ]; then
    echo "ssh not found!"
    CHECK_FAIL=1   
    fi

    if [ ! -f "/usr/bin/rsync" ]; then
    echo "rsync not found!"
    CHECK_FAIL=1
    fi
   
    if [ ! -f "/usr/bin/sshpass" ]; then
    echo "sshpass not found!"
    CHECK_FAIL=1
    fi
   
    if [ "$(id -u)" != "0" ]; then
    echo "not root"
    CHECK_FAIL=1
    fi
   
    if [ $CHECK_FAIL -eq 1 ];then
    exit 0
    fi

du ${SRC}  --max-depth=0 --block-size=MB
   
read -s -p "Enter SSH Password: " pswd
echo ""
echo "Connecting..."

/usr/bin/rsync --verbose --recursive --append --links --copy-dirlinks \
--hard-links --perms --executability --times --delete --chown=${DST_USER}:${DST_USER} \
 --rsh="/usr/bin/sshpass -p '${pswd}' ssh -p ${SSH_PORT} -o StrictHostKeyChecking=no -l ${SSH_USER}" $SRC  $DST

echo "Complete"


 -----------------------------------------------------------------------




 

2014-01-25

Bash script for Livestreamer to record twitch stream by schedule

 Install livesreamer from:

http://livestreamer.tanuki.se/en/latest/

Very very thankful for this tool, I search for something like this sense I hooked on StarCraft II!

To use script, just edit configuration part...  It's pretty self explanatory.
In my time zone, all GSL battles going when I'm at work.
This script allows me to record all and watch it when I come back home after work! :) 

I use livesreamer, not only for recording. Sorry, but Twitch page is lagging and I don't need other stuff, only video. And the best experience, in sense of avoiding video lagging or some sort of interrupts, is to stream to file and then watch it!
Also you can start streaming to file and watch from the file right away. Perhaps you should wait 10 or 15 seconds, this way player not going to catch the end of the file!


^^^^^^^^^^^^^^^^^^^^^^^^^^
#! /bin/bash
STREAM_URL="http://www.twitch.tv/gsl"
STREAM_QUALITY="best"
STREAMS_DUMP_DIR="/media/xbmc/Streams/"
STREAM_FILE_NAME="gsl_code_s_"

#                        month day year hour minutes
START_TIMESTAMP=$(date -d '10/04/2014 11:45' +%s)
END_TIMESTAMP=$(date -d '10/04/2014 19:00' +%s)
KILL_TIMESTAMP=$(date -d '10/04/2014 19:01' +%s)

^^^^^^^^^^^^^^^^^^^^^^^^^^

Bash version1




Bash version 2


It runs as a daemon. Just drop a simple text file to "shed" directory.
Text file should look like this:

^^^^^^^^^^^^^^^^^^^^^^^^^^
# twitch link
URL=http://www.twitch.tv/nathanias
#    Year Month Day
DATE=2014.12.23
#    Hours minutes
TIME=07.45
^^^^^^^^^^^^^^^^^^^^^^^^^^


Also set global configurations before start ("bash.conf"):


^^^^^^^^^^^^^^^^^^^^^^^^^^
# stream quality
QUALITY=best
# directory where steams will be saved
DIR=/media/8b58Volume830GiB/Streams/
# stream offline timeout to consider that stream is ended.
# at least one stream file should be created 
# in minutes
OFFLINE_TIMEOUT=20
^^^^^^^^^^^^^^^^^^^^^^^^^^




Extract archive and open terminal in "livestreamer-deamon" directory

$ ./livestreamer-deamon start


I run this on Raspberry-Pi. I can leave it online 24for7 it consumes ~5W of power and make no noise! It's environment friendly and wife approved! :)))

I added this to /etc/rc.local

^^^^^^^^^^^^^^^^^^^^^^^^^^
# livestreamer-deamon
cd /home/pi/wcs/
su pi /home/pi/wcs/livestreamer-deamon.bash start >> /dev/null 2>> /dev/null &
^^^^^^^^^^^^^^^^^^^^^^^^^^



 

Bash version 3


^^^^^^^^^^^^^^^^^^^^^^^^^^
# twitch link
URL=http://www.twitch.tv/nathanias
#    Year Month Day
DATE=2015.04.04
#    Hours minutes
TIME=19.34

#    Continue for ? days and record everything...
DAYS=3
#    log file
LOG=
^^^^^^^^^^^^^^^^^^^^^^^^^^
If days set to zero it will work as before.


Download :

livestreamer-bash


Enjoy!



2013-11-20

Bash script to manage sshfs


If you wanna use sshfs, this software should be installed:


$apt-get install openssh-client openssh-server sshfs 

If server not always online and you want to mount automatically sshfs when server online, I use this bash.

Bud before running bash, you have to be able connect to server without a password.

You have to create pabulic key on client

$ssh-keygen -t rsa

And then copy key from client ~/.id_rsa.pub (single long line in the text file)
to server ~/.ssh/authorized_keys 

I added this script to "Startup Applications".


Download sshfs.bash

#!/bin/bash

echo $0

# =======================================================
# CONFIGURATION
REMOTE_IP="192.168.1.2"
REMOTE_USER="user"
REMOTE_PATH="/home/"
MOUNT_POINT="/media/sshfs/"

# =======================================================
# VARIABLES
BOOL_ONLINE=0
BOOL_MOUNTED=0
BOOL_PID_RUN=0
# =======================================================
# FUNCTIONS


FUNC_remote_net_stat()
{
NET_STATUS=`ping ${REMOTE_IP} -c 1`

    if grep -q "64 bytes from ${REMOTE_IP}: icmp_req=1 ttl=64 time=" <<< "$NET_STATUS" ; then
        echo "ONLINE"
        BOOL_ONLINE=1
        else
        echo "OFFLINE"
        BOOL_ONLINE=0
    fi
}


FUNC_pid_status()
{
PID_STAT=`ps axu`

    if grep -q "sshfs ${REMOTE_USER}@${REMOTE_IP}:${REMOTE_PATH}" <<< "$PID_STAT" ; then
        BOOL_PID_RUN=1
        echo "PID RUNNING"
        else
        BOOL_PID_RUN=0
        echo "PID DEAD"
    fi
}

# =======================================================
# SCIPT BODY


    while true
    do
        FUNC_remote_net_stat
        FUNC_pid_status
       
        if [ $BOOL_ONLINE -eq "1" ]; then
       
       
            if [ $BOOL_PID_RUN -eq "1" ]; then
            BOOL_MOUNTED=1
            fi
       
            if [ $BOOL_MOUNTED -eq "0" ]; then
            echo "MOUNTIN SSHFS ..."
   
            SSH_MOUNT=`sshfs ${REMOTE_USER}@${REMOTE_IP}:${REMOTE_PATH} ${MOUNT_POINT}`

                if [ ! -z "$SSH_MOUNT" ]; then
                echo "ERROR ${SSH_MOUNT}"
                else
                BOOL_MOUNTED=1
                fi
            echo "DONE"    
            fi   
        fi
       
       
        if [ $BOOL_ONLINE -eq "0" ]; then
       
            if [ $BOOL_MOUNTED -eq "1" ]; then
            echo "KILLING SSHFS ..."
            kill `pidof ssh`
            BOOL_MOUNTED=0
            echo "DONE"
            fi
        fi
       
        if [ $BOOL_MOUNTED -eq "1" ]; then
       
            if [ $BOOL_PID_RUN -eq "0" ]; then
            BOOL_MOUNTED=0
            fi
        fi       
       
    sleep 5
   
   
    done


echo "EXIT"

# THE END
# =======================================================

2013-02-21

Linux Wine: Setting Windows/DOS environment variables & running bat files

Example how to add environment variable "PATH"




Example of running *.bat:

Content of bash for starting *.bat file:
--------------------------------------
#!/bin/bash
wine start ./dir.bat
--------------------------------------

Content of "dir.bat":
--------------------------------------
DIR
PAUSE
--------------------------------------