Showing posts with label ssh. Show all posts
Showing posts with label ssh. 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"


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




 

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-01-22

SSH linux terminal



$apt-get install openssh-client
$apt-get install openssh-server
 
http://wiki.debian.org/SSH
 

---------------------------------------------------------------------
on client:
$ ssh-keygen -t rsa
$ ls ~/.ssh/
$ cat ~/.ssh/id_rsa.pub
     # copy conten to server "~/.ssh/authorized_keys"
$ ssh-copy-id -i ~/.ssh/id_rsa.pub $remote_user@$remote_host


on server:
$ cat ~/.ssh/authorized_keys

-----------------------------------------------------------------------
create pabulic key on client
 ssh-keygen -t rsa
 copy key from client ~/.id_rsa.pub (line) to server ~/.ssh/authorized_keys (line)
---------------------------------------------------------------------
on client:

    destination (server)         localhost (client)

$ ssh rolas@192.168.1.11 -p 22 -R 22222:192.168.1.15:22


on server:
$ ssh 127.0.0.1 -p 22222
---------------------------------------------------------------------

scp $source_file $remote_user@$remote_host:$destination_file
 
scp $remote_user@$remote_host:$source_file $destination_file 

ssh $remote_user@$remote_host 'ls *.txt'
 



http://www.debianadmin.com/howto-use-ssh-local-and-remote-port-forwarding.html

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


GET ETH ACTIVE
$ /sbin/route -n | grep "0.0.0.0" | grep "UG"
0.0.0.0         192.168.1.15     0.0.0.0         UG    0      0        0 eth0

GET IP
$ /sbin/ifconfig eth0 | grep "inet addr:"
    inet addr:192.168.1.15  Bcast:192.168.1.255  Mask:255.255.255.0