# How to write Script

**URL:** <https://forum.gl-inet.com/t/how-to-write-script/4701>\
**Category:** Router\
**Created:** [October 7, 2018, 7:56am UTC](https://forum.gl-inet.com/t/how-to-write-script/4701 "2018-10-07T07:56:21Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azndfc](https://forum-assets.gl-inet.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@Azndfc](https://forum.gl-inet.com/u/Azndfc)\
**Post date:** [October 7, 2018, 7:56am UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/1 "2018-10-07T07:56:22Z")

</div>

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

> [@Openvpn reconnect script](https://forum.gl-inet.com/t/openvpn-reconnect-script/3179):
>
> Openvpn vpn should auto reconnect if it is broken. But in some situations, it will exit because of the server send an exit command. For example, if you IP is changed and the server doesn’t like it. Here is a VPN auto re-connection script. If someone has this problem please test. Name this file /usr/bin/vpn\_reconnect, then enable execution chmod +x /usr/bin/vpn\_reconnect Add it it at the end of /etc/rc.local, before exit /usr/bin/vpn\_reconnect & Here is the file content #!/bin/sh #wait f…

Is there a sticky thread for basic things like these?

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

Thank you very much for the response

---

<div class="post-metadata">

**Author:** ![Johnex](https://forum-assets.gl-inet.com/user_avatar/forum.gl-inet.com/johnex/32/300_2.png) [@Johnex](https://forum.gl-inet.com/u/Johnex)\
**Post date:** [October 7, 2018, 1:44pm UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/2 "2018-10-07T13:44:38Z")

</div>

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

> **[SSH to the Router - GL.iNet Docs](https://docs.gl-inet.com/en/3/tutorials/ssh/)**
>
> Documentation for GL.iNet Productions

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.

---

<div class="post-metadata">

**Author:** ![Azndfc](https://forum-assets.gl-inet.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@Azndfc](https://forum.gl-inet.com/u/Azndfc)\
**Post date:** [October 8, 2018, 4:49am UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/3 "2018-10-08T04:49:42Z")

</div>

> [@Johnex](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![alzhao](https://forum-assets.gl-inet.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@alzhao](https://forum.gl-inet.com/u/alzhao)\
**Post date:** [October 8, 2018, 5:34am UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/4 "2018-10-08T05:34:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Johnex](https://forum-assets.gl-inet.com/user_avatar/forum.gl-inet.com/johnex/32/300_2.png) [@Johnex](https://forum.gl-inet.com/u/Johnex)\
**Post date:** [October 8, 2018, 8:47am UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/5 "2018-10-08T08:47:42Z")

</div>

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:

```bash
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.

---

<div class="post-metadata">

**Author:** ![Azndfc](https://forum-assets.gl-inet.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@Azndfc](https://forum.gl-inet.com/u/Azndfc)\
**Post date:** [October 8, 2018, 10:23am UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/6 "2018-10-08T10:23:43Z")

</div>

> [@Johnex](#):
>
> 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.

Very cool explanation. 😀

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?

---

<div class="post-metadata">

**Author:** ![Johnex](https://forum-assets.gl-inet.com/user_avatar/forum.gl-inet.com/johnex/32/300_2.png) [@Johnex](https://forum.gl-inet.com/u/Johnex)\
**Post date:** [October 8, 2018, 10:32am UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/7 "2018-10-08T10:32:46Z")

</div>

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:

```bash
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.

---

<div class="post-metadata">

**Author:** ![Azndfc](https://forum-assets.gl-inet.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@Azndfc](https://forum.gl-inet.com/u/Azndfc)\
**Post date:** [October 9, 2018, 11:36am UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/8 "2018-10-09T11:36:25Z")

</div>

> [@Azndfc](#):
>
> Here is a VPN auto re-connection script.

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

---

<div class="post-metadata">

**Author:** ![Johnex](https://forum-assets.gl-inet.com/user_avatar/forum.gl-inet.com/johnex/32/300_2.png) [@Johnex](https://forum.gl-inet.com/u/Johnex)\
**Post date:** [October 9, 2018, 3:33pm UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/9 "2018-10-09T15:33:41Z")

</div>

You will have to do this:

```bash
nano /usr/bin/vpn_reconnect

```

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

```bash
chmod +x /usr/bin/vpn_reconnect

```

Then open the rc.local file:

```bash
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 🙂

---

<div class="post-metadata">

**Author:** ![Azndfc](https://forum-assets.gl-inet.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@Azndfc](https://forum.gl-inet.com/u/Azndfc)\
**Post date:** [October 9, 2018, 4:06pm UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/10 "2018-10-09T16:06:56Z")

</div>

> [@Johnex](#):
>
> nano /usr/bin/vpn\_reconnect

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

> [@Johnex](#):
>
> chmod +x /usr/bin/vpn\_reconnect

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

---

<div class="post-metadata">

**Author:** ![Johnex](https://forum-assets.gl-inet.com/user_avatar/forum.gl-inet.com/johnex/32/300_2.png) [@Johnex](https://forum.gl-inet.com/u/Johnex)\
**Post date:** [October 9, 2018, 4:21pm UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/11 "2018-10-09T16:21:35Z")

</div>

> [@Johnex](#):
>
> opkg update && opkg install nano

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.

---

<div class="post-metadata">

**Author:** ![Azndfc](https://forum-assets.gl-inet.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@Azndfc](https://forum.gl-inet.com/u/Azndfc)\
**Post date:** [October 9, 2018, 4:30pm UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/12 "2018-10-09T16:30:27Z")

</div>

> [@Johnex](#):
>
> opkg update && opkg install nano

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. 😃

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

---

<div class="post-metadata">

**Author:** ![Johnex](https://forum-assets.gl-inet.com/user_avatar/forum.gl-inet.com/johnex/32/300_2.png) [@Johnex](https://forum.gl-inet.com/u/Johnex)\
**Post date:** [October 9, 2018, 6:26pm UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/13 "2018-10-09T18:26:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Azndfc](https://forum-assets.gl-inet.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@Azndfc](https://forum.gl-inet.com/u/Azndfc)\
**Post date:** [October 9, 2018, 7:56pm UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/14 "2018-10-09T19:56:06Z")

</div>

> [@Johnex](#):
>
> If you go into Luci, go to Processes, you should see the script running there, in one of the rows.

I cannot find process named “vpn” in preocesses

---

<div class="post-metadata">

**Author:** ![Johnex](https://forum-assets.gl-inet.com/user_avatar/forum.gl-inet.com/johnex/32/300_2.png) [@Johnex](https://forum.gl-inet.com/u/Johnex)\
**Post date:** [October 9, 2018, 8:24pm UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/15 "2018-10-09T20:24:21Z")

</div>

![53](https://forum-static.gl-inet.com/original/1X/c08119288925f250e4699a682785b70fd34522b1.png)

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

---

<div class="post-metadata">

**Author:** ![Azndfc](https://forum-assets.gl-inet.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@Azndfc](https://forum.gl-inet.com/u/Azndfc)\
**Post date:** [October 9, 2018, 11:07pm UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/16 "2018-10-09T23:07:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![admon](https://forum-assets.gl-inet.com/user_avatar/forum.gl-inet.com/admon/32/32880_2.png) [@admon](https://forum.gl-inet.com/u/admon)\
**Post date:** [June 11, 2024, 9:40am UTC](https://forum.gl-inet.com/t/how-to-write-script/4701/18 "2024-06-11T09:40:51Z")

</div>


