❗ ssh public key 교환

❗ ssh public key 교환

❗ reverse SSH

❗ reverse SSH

▪︎ Command

pi@drone:~$ sudo ssh -f -N -T -R 2222:localhost:22 [email protected]*.*.* -p 5001

<aside> 📌 -f : Requests ssh to go to background just before command execution. ⇒ ssh 요청을 백그라운드로 실행하기 위한 옵션 (백그라운드로 실행될 수 있도록 프로세스를 fork)

-N : Do not execute a remote command. This is useful for just forwarding ports. ⇒ ssh로 연결을 설정한 후 원격 쉘(e.g. /bin/bash)에 대한 제공이 필요하지 않은 경우에 대한 옵션

-T : Disable pseudo-terminal allocation. ⇒ 터미널이 요청하는 것에 대해 무시하기 위한 옵션 ( 명령을 입력할 대화형 보안 쉘을 제공할 의도가 없는 경우 사용 )

-R remoteport:host:hostport : Reverse SSH option ⇒ 지정한 로컬 호스트와 로컬 호스트 포트로 전송될 원격 포트 설정 ( 지정한 원격 포트로 연결 요청이 들어오면 지정한 로컬 호스트와 호스트 포트로 바인딩 )

</aside>

<aside> 💡 1. **2222 포트**는 컴퓨터에서 원격으로 연결하는데 사용할 포트

  1. **22 포트**는 Raspberry Pi에서 SSH 트래픽을 기다리고 있는 포트
  2. **5001 포트**는 GCS 데스크탑에서의 SSH 포트 (/etc/ssh/sshd_config : 22 → 5001 변경)
  3. uhyeong GCS 데스크탑의 user-name
  4. 210.11*.*.* 은 라우터 IP 주소 (GCS 호스트의 주소) </aside>
uhyeong@DESKTOP-R39GAN6:~$ ssh pi@localhost -p 2222

▪︎ Shell script + Crontab

#!/bin/bash

mobileInterfaceName="usb0"
routerIp="210.115.229.207" # my router ip

# get dynamic mobile ip address from ifconfig
mobileIpAddress=$(ifconfig | grep -A2 $mobileInterfaceName | grep "inet " | awk -F' ' '{print $2}')

# run ssh command
# "11111111" is desktop GCS password
sshpass -p 11111111 ssh $mobileIpAddress -f -N -T -R 2222:localhost:22 uhyeong@$routerIp -p 5001
#!/bin/bash

routerIp="210.115.229.207"  # my router ip
interfaceName="usb0"

# get dynamic mobile gateway ip address from ifconfig
mobileGateway=$(ifconfig | grep -A2 $interfaceName | grep "inet " | awk -F' ' '{print $2}' | awk -F'.' '{print $1"."$2"."$3".1"}')

#sudo ip route add 210.115.229.207 via *.*.*.* dev usb0
setRoute=$(sudo ip route add $routerIp via $mobileGateway dev $interfaceName 2>&1)