⬆️ ⬇️

Own DynDNS on the knee

So, let's say you are a web developer. Suppose you have a Linux home computer running 24x7 and a dynamic external ip. Suppose you need to connect to it via ssh or show web projects to a customer that for some reason cannot be spread to the hosting for a long time or at all (because of the need to show something to third parties, VPN is not an option). Suppose you do not like DynDNS services. Let's get started



I agree, the conditions are specific - but something in life does not happen.



The scheme is approximately the same - home PC is knocking on your site (you are a web developer, after all, you must have a site), he fixes an ip address, and gives it to you.



First, we configure the output (actually input) of the home PC (hereinafter - the server, for simplicity) to the external network. If your PC is directly connected to the Internet, feel free to skip this item. This is approximately how setting up a web interface of my ASUS WL-520GU router with alternative firmware tomatoUSB - I open port 666 for ssh forwarding and port 667 on the router for redirecting web requests to the server (internal address is 192.168.1.100, the ip address is fixed to the server by rules DHCP of the same router).

')





Some routers need to be rebooted, but with a high degree of probability they will report this themselves.



It remains to configure the server to request a cron'u page on your hosting (http://my.site/testippage.php, for example).



In the console -

crontab -e */5 * * * * wget http://my.site/testippage.php?key=habrahabr -O /dev/null 


(every 5 minutes download the page my.site/testippage.php to nowhere)



The key transmitted by the get request serves as a small protection against accidental or deliberate sabotage.



Now we configure server part. I used to use the MySQL database to store the current ip address - but this is completely unnecessary, and in the process of editing the article I got this code:

 <?php if($_GET['key']=='habrahabr'){ file_put_contents ( 'ip.txt' , getenv("REMOTE_ADDR")); } else { $ip = file_get_contents('ip.txt'); if (isset($_GET['page'])){ header('Location: http://'.$ip.':667/'.$_GET['page']); } else { echo $ip; } } ?> 




As is clear from the code, if the get parameter is passed to the script, it records the knocked ip, if the page parameter redirects to your home computer, and if it does not receive anything, it simply displays the ip address.



To show the customer the project lying in the project folder of the server - you need to give him the link my.site/testippage.php?page=project .



Now the last part is setting up a connection to the server with a single command. Let's make a script, let's say, homeslackconnect.sh:

 content=$(wget http://my.site/testippage.php -q -O -) ssh $content -p 666 -l niph 


it reads the ip address into the content variable and tries to connect to the received address via port 666 as the niph user. As required initially.



I do not argue with the fact that DynDNS is a much simpler solution, but not all routers can use free DynDNS servers, besides, they tend to become not so free, and their solution is always warmer. Ask questions in the comments, I hope someone this decision will help simplify your life.

Source: https://habr.com/ru/post/160275/



All Articles