Skip to content

Instantly share code, notes, and snippets.

@unique1984
Last active March 15, 2020 13:59
Show Gist options
  • Save unique1984/4676d817ba71dc39a72c4bc9f9d4ef60 to your computer and use it in GitHub Desktop.
Save unique1984/4676d817ba71dc39a72c4bc9f9d4ef60 to your computer and use it in GitHub Desktop.
ovpn_autostart
#!/bin/sh
OVPNCONF="/root/ovpn/client.ovpn"
RUNNING=$(ps aux | grep -o "openvpn \-\-config" | wc -l)
case "$1" in
start)
if [ $RUNNING -eq 0 ]; then
echo -n > /var/log/ovpn_autostart.log
nohup openvpn --config "$OVPNCONF" > /var/log/ovpn_autostart.log 2>&1 &
else
$0 restart
fi
;;
stop)
if [ $RUNNING -eq 1 ]; then
pid=$(ps aux | grep "openvpn \-\-config" | awk '{print $2}')
kill -9 $pid
unset pid
echo -n > /var/log/ovpn_autostart.log
fi
;;
restart)
echo "$0"
$0 stop
sleep 1
$0 start
;;
reload|force-reload)
$0 restart
;;
status)
if [ $RUNNING -eq 0 ]; then
echo -e "Kapalı\n"
else
echo -e "Aktif\n"
fi
;;
*)
exit 1
esac
@unique1984
Copy link
Author

unique1984 commented Sep 4, 2017

apt-get install openvpn
apt-get install ca-certificates

Burada openvpn client config dosyası ile ilgili ufak bir not:

client.ovpn config dosyası içerisinde ca.crt, client.crt, private.key dosya yollarını /root/ovpn/* şeklinde ayarlaman gerekecek veya aşağıdaki shell script ve client.ovpn içerisindeki yolları doğru ayarlamaya bak.

mkdir /root/ovpn

Oluşturduğun bu klasöre client.ovpn | ca.crt | client.crt | private.key dosyalarını kopyala.

ardından
chmod 600 /root/ovpn/*
komutunu çalıştır yoksa openvpn kızıyor.

aşağıdaki kodu
/etc/init.d/ovpn_autostart
isimli bir dosyaya kaydet dosya içinde OVPNCONF değişkeninin senin client.ovpn dosyanı gösterdiğinden emin ol.

ovpn_autostart

chmod +x /etc/init.d/ovpn_autostart

komutu ile dosyaya çalıştırma özelliği ekle.

root@lamp:~# ls -l /etc/rc.local
-rwxr-xr-x 1 root root 338 Eyl 4 05:56 /etc/rc.local

/etc/rc.local dosyasının özelliklerine baktığında bu şekilde birşey görmelisin.
-rwxr-xr-x

nano /etc/rc.local

komutu ile dosyayı aç ve exit 0 dan önceki satıra.

/etc/init.d/ovpn_autostart start

komutunu ekle.

#####################
Debian 9 için

cat <<EOF >/etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0
EOF

chmod +x /etc/rc.local
systemctl start rc-local
systemctl status rc-local

● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
  Drop-In: /lib/systemd/system/rc-local.service.d
           └─debian.conf
   Active: active (running) since Tue 2017-09-05 02:41:56 +03; 9s ago
  Process: 4439 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/rc-local.service
           └─4445 openvpn --config /root/ovpn/ysn_fuji.ovpn

Eyl 05 02:41:56 uxn9 systemd[1]: Starting /etc/rc.local Compatibility...
Eyl 05 02:41:56 uxn9 systemd[1]: Started /etc/rc.local Compatibility.

#####################

Artık test edebiliriz. dosyaları ve değişkenleri doğru yapılandırdıysan /etc/init.d/ovpn_autostart start komutunu verdiğinde ifconfig ile tun (.ovpn dosyanda dev kısmında ne tanımlıysa) adaptörün vpn ip aldığını göreceksin. /var/log/ovpn_autostart.log ismindeki dosyayada bakabilirsin bir hata oluşursa diye.

wget -qO- https://gist.githubusercontent.com/unique1984/0f987a06ce76b27402cd631c7441b369/raw/77644a592298283c19c3195c2dc6a9e2162944d8/ip.sh | bash

komutunu çalıştırdığında ise vpn adresini ve geolocation bilgilerini görüyor olmalısın.

sistemi yeniden başlatıp tekrar dene umarım bir sıkıntı çıkmaz bende çalışıyor ancak bir sıkıntı olursa /etc/init.d/ovpn_autostart dosyasında start) hemen sonrasına sleep 30 şeklinde bir satır eklersen komut çalıştıktan 30 saniye sonra vpn bağlantısı kurmaya çalışacaktır.

Benden buraya kadar kolay gelsin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment