Linux, Pytorch, Python

Remote Workspace

Peter Yoon 2021. 11. 5. 22:48

Remote Port Tunneling

1. ssh connection

1-1 Remote ssh

외부 클라이언트 C ----> 중계서버 B <-------- 내부 서버 A

ssh -fNT -g -o ServerAliveInterval=60 -R :{PORT_B2}:localhost:22 {USER_B}@{HOST_B} -p {PORT_B1}
  • 1 중계서버 B에 접속 {USER_B}@{HOST_B} -p {PORT_B1}
  • 2 `{PORT_B2}' listening
  • 3 클라이언트 C에서 {PORT_B2}'로 접속시localhost:22`로 forwarding.

1-2 login without passwd

Follow below instruction
link

1-2-1 ssh keygen

Client에서 key를 만든다.

ssh-keygen -t ras

결과값으로 id_ras 그리고 id_ras.pub 나옴.

1-2-2 key copy

Server에 .pub 파일으 보내고 Server에서 authorized_keys에 복사해준다.

sftp -oPort PORT USER@HOST
put id_rsa.pub
cat .pub >> HOME/.ssh/authorized_keys

1-2-3 login

Private key는 소유주만 사용해야함. 권한변경해서 사용할 것

chmod 400 id_rsa
ssh -i ~/.ssh/id_rsa USER@HOST -p PORT

2. Check ssh running

Count running specific process

emma_check=`ps -ef | grep -v "grep" | grep "ssh -fNT -g" | wc -l`

if emma_check ==0: connect ssh
else: do nothing

3. logging

echo "text" > /PATH/TO/LOGFILE 2>&1  # overwrite
echo "text" >> /PATH/TO/LOGFILE 2>&1 # appending

4. cron

4.1 cron job

crontab을 열어 ssh 명령어를 등록하자.

4.2 cheatsheet

cron에 sheel script 등록

crontab -e  # open crontab
crontab -l  # print cron jobs

service cron status 
service cron start
service cron restart

-주의사항-
crontab 변경후에는 꼭 서비스 재시작하자

Example

#! /bin/bash
n_process=`ps -ef | grep -v "grep" | grep "ssh -fNT -g" | wc -l`
date=$(date "+%Y-%m-%d_%H:%M:%S")

if [ "$n_process" == "0"  ]; then
echo "${date} | Connecting" >> /home/peter/workspace/scripts/check.log 2>&1
ssh -fNT -g -o ServerAliveInterval=60 -R :32773:localhost:22 nas_peter@xiah.duckdns.org -p 12255

else
echo "${date} | rssh still running now" >> /home/peter/workspace/scripts/check.log 2>&1
fi

라즈베리파이 서버에서 커넥션이 잘 되고 있는지 확인.
who -a
또는

sudo netstat -tnpa | grep 'ESTABLISHED.*sshd

이게 더 자세히, 더 많이 나오긴 한다.

'Linux, Pytorch, Python' 카테고리의 다른 글

Cudatoolkit  (0) 2021.11.11