How to write Script


#1

Hey All, i’m a newbie and i don’t know yet how to write and execute script like in this thread

Is there a sticky thread for basic things like these?

Once I copy paste script in firewall rules. Other than that. I’m clueless. :joy:

Thank you very much for the response


#2

Well first you need to connect to your router using SSH:

https://docs.gl-inet.com/en/3/app/ssh/

Using the WinSCP method there, you can make new files and edit them, pasting the script code into them, and then using Putty, run the commands.


#3

Nice. Do I really have to use Putty? Or I can choose only 1 program (WinSCP) to edit config files?


#4

Both are OK.
But if you want to learn, use putty.


#5

WinSCP can only upload and edit files (unless i have missed the commandline).

And Putty is the other way around, it can’t upload files, but it can do all commands.
That also means that its possible to edit and create files using commands and for example Nano, but you need to install Nano on your router by doing:

opkg update && opkg install nano

I personally use this method. Vi already comes installed on the device but i don’t really like it as an editor.


#6

Very cool explanation. :grinning:

About Nano & Vi, can we use editor on the router itself to edit files using smartphone?

About Putty, can you show one or two simple commands that is useful in GLinet? Why use Putty in the first place? Why @alzhao said it can be used for learning?


#7

Yeah Nano runs in the router. You can just download something like JuiceSSH if you are on Android to connect to your router, and then just type:

nano <filename>

(After you install nano)

You will then get something that resembles notepad but remotely, and you can type there, paste entire scripts, and then save the file using cntrl+o, and quit using cntrl+x.

Putty is just an SSH terminal program, so the learning part is because you will be able to access all the configuration files on the router and edit them. The GL UI is basically changing configuration files in the background, so you would be able to do the same, but manually and for more advanced configurations.


#8

So I’ll just have to make a new vpn_reconnect.txt file, insert in in the /usr/bin by using WinSCP, then let the script happen in the background?

The script is

#!/bin/sh

#wait for the openvpn to connect for the first time
sleep 120

while [ true ]; do

#check if openvpn is enabled, if not, go to next loop
vpn_enabled=$(uci get glconfig.openvpn.enable)
if [ “$vpn_enabled” != “1” ]; then
echo "VPN not enabled, check 20 seconds later"
sleep 20
continue
fi

vpn_pid=$(pidof openvpn)
tun0_ifname=$(ifconfig tun0)

if [ -z “$tun0_ifname” ] && [ -z “$vpn_pid” ]; then
echo “VPN enabled but not running, restarting it”
/etc/init.d/startvpn restart
else
echo "VPN is connected and connecting, check 20 seconds later"
fi

sleep 20

done


#9

You will have to do this:

nano /usr/bin/vpn_reconnect

Paste the script and then do cntrl + o, cntrl + x
Then:

chmod +x /usr/bin/vpn_reconnect

Then open the rc.local file:

nano /etc/rc.local

Add this to the end of the file just before exit:

/usr/bin/vpn_reconnect &

Do cntrl + o, cntrl + x to save and quit again.

The script should run on boot now :slight_smile:


#10

Sorry, how can i install Nano? I have tried searching Nano application on Repo App. But couldn’t find one.

I even couldn’t find Vi editor on installed apps.

So how to run this command? Sorry

I assume it is all done on desktop computer by installing apps said like Winscp and Putty. :frowning:


#11

To install nano.

You have to do this from any terminal app. In windows Putty, on Android JuiceSSH, iphone probably has some ssh free app too and so on.


#12

At last i have installed Nano using iTerminal app from iPhone.

Doing next steps. Will update

UPDATE:

  1. I have installed Nano
  2. I have executed chmod +x /usr/bin/vpn_reconnect
  3. I have modified /etc/rc.local

I am so happy. :smiley:

How to see if it is working? Is there some kind of log files I can see? Thank you so much @Johnex


#13

If you go into Luci, go to Processes, you should see the script running there, in one of the rows.

In the same place, you will also be able to check the ID for OpenVPN, if it changes, then the script has restarted it.


#14

I cannot find process named “vpn” in preocesses


#15

It should be there if it’s running and you are connected, check again.


#16

Ok. It is there now. But why the process does not restart? What has gone wrong?

FYI i have rebooted the router before

Update: The silly thing is, although the pid is there, the service cannot connect to the internet.

When I check again, there is no internet connection. So the problem is there is no internet connection

Sorry