Automatic Configuration Script

I use the script bellow to configure my routers every time I do an upgrade of the firmware. I don’t like having a lot of residual files taking up space, and when going from one version to another it is not recommended to save settings when doing an upgrade.

The script is for the S1300, and you will need to read it and go through changing some of the values for your router, but its a good starting point. You can copy individual lines of the script and run them to see what is going on and you can expand the script with other configs you would like. Each config block starts with printl and a comment, so that gives you information what is going on in each block.

The script uses echo and cat heredoc for adding lines to config files, and uses sed to modify lines in config files.

The script formatting is important, don’t change it, especially the EOF lines, they should not have any whitespace before them.

The wireless and network configs contain unique names for the network and wireless only for your router, so you will need to change those as well. Those are the only things that are not “universal” across the routers. All other configs are the same. As config files are stored in /etc/config/, you can open those files and see what your current config is, then copy it to the corresponding place in the script.

After you have made your modifications, run the script by just copying it and pasting it into your SSH window. After the router reboots, the new config will be active. If something goes wrong while you experiment, you can always follow the debrick guide.

# Gl-iNet Configurator v1.0 - © Johnex 2021
# =========================================

cat<<'SEOF' | ash
#!/bin/sh

    #=====================#
    #=|-----------------|=#
    #=|                 |=#
    #=|    FUNCTIONS    |=#
    #=|                 |=#
    #=|-----------------|=#
    #=====================#

    printh()
    {
        line='~~~~~~~~~~~~~~~~~~~~~~~~~'
        echo -e "\n \033[0;96m$1\033[0m\n ${line::${#1}}\n"
    }
    
    printl()
    {
        echo -e "\n \033[0;96m$1\033[0m"
    }

    #================#
    #=|------------|=#
    #=|            |=#
    #=|    MAIN    |=#
    #=|            |=#
    #=|------------|=#
    #================#

    echo -e "\n \033[0;97mGl-iNet Configurator v1.0 - © Johnex 2021\n =========================================033[0m"

    printh 'Install apps'
    opkg update
    opkg install htop nano openssh-sftp-server sudo
    
    printl 'Configuring Sudo'
    sed -i 's/# %sudo.ALL=(ALL) ALL/%sudo ALL=(ALL) ALL/g' /etc/sudoers
    sed -i 's/# Defaults targetpw/Defaults targetpw/g' /etc/sudoers
    sed -i 's/# ALL ALL=(ALL) ALL/ALL ALL=(ALL) ALL/g' /etc/sudoers

    printh 'Creating Users'
    echo 'USERNAME:p:16666:0:99999:7:::' >> /etc/shadow
    echo 'USERNAME:x:1000:1000:USERNAME:/home/USERNAME:/bin/ash' >> /etc/passwd
    echo -e "PASSWORD\nPASSWORD\n" | passwd USERNAME

    printl 'Created Groups'
    echo 'sudo:x:27:USERNAME' >> /etc/group
    echo 'USERNAME:x:1000:' >> /etc/group

    printl 'Created Home Directories'
    mkdir -p /home/USERNAME
    chown USERNAME /home/USERNAME

    printl 'Disabled Root SSH'
    sed -i "s/option RootPasswordAuth 'on'/option RootPasswordAuth 'off'/g" /etc/config/dropbear

    printl 'Configured Network'
    sed -i "s/'192.168.8.1'/'10.5.0.1'/g" /etc/config/network

    printl 'Static IPs Set'
    cat<<'EOF' >> /etc/config/dhcp
config host 'aa22aa22aa22'
        option mac 'AA:22:AA:22:AA:22'
        option ip '10.5.0.2'
        option name 'DEVICE1'

config host 'bb33bb33bb33'
        option mac 'BB:33:BB:33:BB:33'
        option ip '10.5.0.2'
        option name 'DEVICE2'

EOF

    printl 'Configured Virtual Server'
    cat<<'EOF' >> /etc/config/firewall
config redirect
        option target 'DNAT'
        option src 'wan'
        option dest 'lan'
        option proto 'tcp'
        option src_dport '22'
        option dest_ip '10.5.0.1'
        option dest_port '22'
        option name 'SSH'
        option reflection '1'

config redirect
        option target 'DNAT'
        option src 'wan'
        option dest 'lan'
        option proto 'tcp'
        option src_dport '8080'
        option dest_ip '10.5.0.1'
        option dest_port '8080'
        option name 'WebServer'
        option reflection '1'

EOF

    printl 'Configured Wireguard'
    cat<<'EOF' > /etc/config/wireguard_server
config servers
        option local_ip '10.0.0.1'
        option private_key 'ouhdafhoug80tr7asdbj74ggahdfbjhhquafbt8abg='
        option public_key 'ojibjahbfkmnhnjuuasdvFnkyojhasvhdbhnkhihadg='
        option local_port 'SERVER_PORT'
        option enable '0'

config peers 'wg_peer_5296'
        option name 'CLIENT1'
        option client_key 'hurhhbsdfkHBSbkhgroufb4ruhd834hsdfugg4wtsdbj='
        option private_key 'nkleasjforegabnasyigfwohrynhklja794w86jlnbj='
        option client_ip '10.0.0.2/32'

config peers 'wg_peer_8352'
        option name 'CLIENT2'
        option client_key 'knhbvawuvthiptbhyuv68q8304t6nipjbasyifygafuh='
        option private_key '7912gy43bhkwefviytfauopjybjoauvdutfqpiynhpi='
        option client_ip '10.0.0.3/32'

EOF

    printl 'Configured Wireless'
    sed -i "s/'DE'/'US'/g" /etc/config/wireless
    sed -i "s/'18'/'25'/g" /etc/config/wireless
    sed -i "s/'19'/'25'/g" /etc/config/wireless
    sed -i "s/'GL-S1300-418'/'AP_NAME'/g" /etc/config/wireless
    sed -i "s/'GL-S1300-418-5G'/'AP_NAME'/g" /etc/config/wireless
    sed -i "s/'GL-S1300-418-Guest'/'AP_NAME-Guest'/g" /etc/config/wireless
    sed -i "s/'GL-S1300-418-Guest-5G'/'AP_NAME-Guest'/g" /etc/config/wireless
    sed -i "s/'goodlife'/'AP_PASSWORD'/g" /etc/config/wireless

    printl 'Configured Hostname'
    sed -i "s/'console.gl-inet.com'/'NETWORKNAME'/g" /etc/config/dhcp

    printl 'Replaced S1300 Name'
    sed -i "s/'GL-S1300'/'NETWORKNAME'/g" /etc/config/system
    sed -i "s/'GL-S1300-418'/'NETWORKNAME'/g" /etc/config/network
    sed -i "s/'GL-S1300'/'NETWORKNAME'/g" /etc/config/samba
    sed -i "s/'GL-S1300-418'/'NETWORKNAME'/g" /etc/config/samba

    printl 'Replaced Banner'
    cat<<'EOF' > /etc/banner
  _____  __        __  __  _         __
 |   __||  |    _ |__||  \| |.-----.|  |__
 |  |  ||  |__ |_||  ||     ||  -__||   __|
 |_____||_____|   |__||_|\__||_____||_____|
                      www.gl-inet.com
 ------------------------------------------
 GL-S1300            v3.1 | OpenWrt 15.05.1
 ------------------------------------------


EOF

    echo -e '\n \033[0;92mDone, Rebooting\033[0m\n'
    reboot

SEOF
10 Likes

out of curiosity any specific reason for not using uci and/or uci defaults provided by openwrt for network, wifi, dropbear and other normal services??

fantastic!!
I wanted a reference example like this.

Even I know that It can change the default settings according to the UCI system, It can run an Ash shell script immediately after booting, It can adjust the firmware in Image Builder, and … etc.
That said, I’m a beginner in routers or OpenWRT, so I need to get expert reviews or take time to scrutinize, before using great power. Both are difficult to do alone.

For example, I didn’t know the criteria for managing users in a complex way or focusing on the root user.
It is difficult for beginners because there is no definite answer and some judgments are based on the feeling of experience.

If I can refer to the scripts created by an expert, I can compare them with my script to find out why I made a different decision. This works well as an alternative to reviews, clarifies what beginners need to know! so did it!

1 Like

just setup working configuration first on working device. create backup of this settings. (save this .zip file for future)
now you have options:
every time you upgrading firmware - after upgrade just upload this file and restart device. its all.
another option: unzip file and add this files to your openwrt sdk “files” folder. its will create new firmwares already with pre-setup configs.
another option: wrote your own script for build new firmwares, its can grab old settings and put in to new firmware automatically, before or after assembling firmware. many options, you choose.

I made the script a long time ago, i just didn’t see the point in upgrading it. GL also doesn’t use UCI for all the settings, so i didn’t want to mix. If you want you can improve the script and post an update :smiley:

id rather use imagebuilder with files and package option so when i play around i have a “reset to my personal default” option… thanks for sharing your script anyways :wink:

Well the script is more for going from one version to another, so having to generate a new image would take longer for some people than just pasting the script into SSH, where you can already have the GL Wifi default connection in windows ready, and the default connection for SSH ready as well. One click to connect, paste; when the router restarts it’s done.

There are many solutions for different things :smiley: For doing testing and resetting the same version many times, sure, imagebuilder is the way to go.

this summarized my entire journey and feelings through university and 10+ years as a professional web dev perfectly

Is that good or bad? :joy:

i think knuckles cracking it’s good? i quit my programming job to do other tech work because i just love writing scripts and code so much that it felt unclean to bring money into such a pure act?..? that was sarcasm but i do love using and digging into my gl router(s)! the younger user that had one of the initial replies to this thread sounded exactly like i did in CS101 with only the most naive and green ideas about what computer science and networking were, and i got a little verklempt for a second.