Você está aqui: Página Inicial > Contents > Documentos > sshpc > sshpc_server_pair.sh
conteúdo

sshpc_server_pair.sh

por EDSON - LAMBDA última modificação 13/06/2023 11h32

text/x-sh sshpc_server_pair.sh — 2 KB

Conteúdo do arquivo

#!/bin/bash

#########################################################################################
## SSHPC - Cluster de processamento distribuído baseado em SSH                         ##
##                                                                                     ##
## Local:   Laboratório Multiusuário de Bioinformática e Análise de Dados (LAMBDA)     ##
##          Centro de Biotecnologia (CBiotec), Universidade Federal da Paraíba (UFPB)  ##
## Data:   30/05/2023                                                                  ##
## Coordenador: Edson Luiz Folador                                                     ##
## IC: Arthur Araújo de Lacerda                                                        ##
#########################################################################################

readonly PROGNAME=$(basename "$0")
readonly PROGDIR=$(readlink -m $(dirname "$0"))
readonly SSHPC_DIRECTORY="/home/sshpc"

debugging() { ${debug} && echo -e "$1"; }

help() {
    echo "Usage: ./sshpc_server_pair.sh [OPTION] args"
    echo "Pair with computing node"
    echo "Example ./.sshpc_server_pair.sh -i 192.123.453.54 -l MYLAB"
    echo
    echo "Options"
    echo -e "-i, --ip\tIp from node you are trying to connect"
    echo -e "-l, --lab\tLab from node you are trying to connect"
    echo -e "-q, --quiet\tDisables verbose"
    echo -e "-h, --help\tHelp function"
}

test_connection() {
  # Sends ssh-key to client
  local ip="${1}" && debugging "connecting to ip ${ip}"
  local port="9123"
  local key="${SSHPC_DIRECTORY}/.ssh/id_rsa.pub"
  debugging "scp -P ${port} ${key} sshpc@${ip}:~"
  scp -P ${port} ${key} sshpc@"${ip}":~
}

repair() {
  server_ip=$(ip ad show enp3s0 | grep -Po "([0-9]{0,3}\.){3}[0-9]*" | head -1)
  client_ip=$1
  lab=$2
  ssh-keygen -R "${client_ip}"
  ssh -p 9123 sshpc@$client_ip /home/sshpc/.bin/sshpc_client_pair.sh -i $server_ip -l $lab &
}

scan_pairing_attempt() {
  while true; do
    # Select only the login attempts that are Invalid User attempts
    # /var/log/auth.log is wherer attemps are stored
    local invalid_attempt=$(grep 'Invalid user sc_pair_sshpc' < "${SSHPC_DIRECTORY}/sshpc.log" | sed -E 's/ +|_/\t/g' | cut -f 14 | sed 's/[_ ]/;/g' | sort -k 4 | uniq) 
    echo "" > "${SSHPC_DIRECTORY}/sshpc.log" 

    if [[ -z "${invalid_attempt}" ]]; then 
      debugging "looking for pairing"
      sleep 10
    else 
      debugging "pair connection found, sending key"
      test_connection "${invalid_attempt}"
      return 0
    fi
    sleep 3
  done
}

main() {
  # Debug 
  local debug=true
  while true; do
    case "$1" in
      -q | --quiet) debug=false; shift;;
      -i | --ip)  client_ip=$2; shift; shift;;
      -l | --lab)  lab=$2; shift; shift;;
      -h | --help) help; exit 0;;
      --) break;;
      *) break;;
    esac
  done

  repair $client_ip $lab
  scan_pairing_attempt
}

main "$@"