📜 ⬆️ ⬇️

ssh and dynamic ip

I have at home an adsl-modem along with a router. And there is a desire to have constant access to the home machine via ssh, but bad luck - the ip modem is not static, but even quite the opposite - dynamic and find out what address it currently has. being outside the house is impossible. But only if you do not prepare for it in advance =)

Armed with a plan to create for myself all the possibilities for obtaining an ip-person, I sketched a small script that every time the system boots, it recognizes the modem’s ip and uploads it to a file on any ftp server, and I can only see what ip was assigned to the modem at the last boot and connect through the thrown port to the home machine =)

For convenience, I divided the functionality into two parts (the script and the config itself) and put it all into a small deb-package.

Script code:
')
#! / bin / bash

### BEGIN INIT INFO
# Provides: get-ip
# Required-Start: $ local_fs $ remote_fs
# Required-Stop: $ local_fs $ remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Setting up daemon for detect IP on your system
### END INIT INFO

. /etc/get-ip.conf

if [$ {ftp_host} = ""]; then
echo "Program is not configured! Host: $ ftp_host »
exit 1;
fi

wget -qO / tmp / ip_tmp myip.ru && cat / tmp / ip_tmp | grep -e " . * "| sed" s / // g »| sed "s / <\ / TD> <\ / TR> // g" | sed "s / // g"> $ upload_file_name && rm / tmp / ip_tmp

#echo $ upload_file_name

ftp -n $ ftp_host << EOF
user $ ftp_name $ ftp_pass
delete $ upload_file_name
send $ upload_file_name
bye
EOF

rm $ upload_file_name

echo "IP changed"

Actually, there is absolutely nothing complicated in the script, when installing the package, this script will be added to the scripts called when the system is booted, although the solution may not seem entirely rational, because the machine can work for a week or two, and the modem will have time to change ip more than once , but this variant quite suits me on the home machine. and if someone doesn’t like it, you can easily fix the source code, since it’s available via this link along with the package already assembled: get-ip.tar.gz

PS: After installation, you need to edit the /etc/get-ip.conf file to work with your FTP. If someone tells a more effective and less katylepodobny way of getting your IP, I will be grateful =)

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


All Articles