From e608f2cbce1603a6ca374136c95e4804cf916e6e Mon Sep 17 00:00:00 2001 From: Rich Brown Date: Sun, 8 Oct 2017 08:28:47 -0400 Subject: [PATCH] Initial commit of opkgscript from OpenWrt forum --- README.md | 45 +++++++++--- opkgscript.sh | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 235 insertions(+), 10 deletions(-) create mode 100755 opkgscript.sh diff --git a/README.md b/README.md index 94a46e0..6fa21b8 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,24 @@ OpenWrtScripts ============== -This is a set of scripts (sometimes also called "Openscripts") that report, configure and measure (and improve) latency in home routers (and everywhere else!) These scripts include: +This is a set of scripts (sometimes also called "Openscripts") that report, configure and measure (and improve) latency in home routers (and everywhere else!) +These scripts work equally well for both [LEDE](https://lede-project.org) and [OpenWrt](https://openwrt.org) and include: * [getstats.sh](#getstatssh) - a script to collect troubleshooting information that helps us diagnose problems in the OpenWrt distribution. +* [opkgscript.sh](#opkgscriptsh) - a script to save the list of +currently-installed packages (say, before a sysupgrade), +and then restore the full set of packages after the upgrade. + * [config-openwrt.sh](#config-openwrtsh) - a script to configure the OpenWrt router consistently after flashing factory firmware. * [betterspeedtest.sh](#betterspeedtestsh) & [netperfrunner.sh](#netperfrunnersh) & [networkhammer.sh](#networkhammersh) - scripts that measure the performance of your router or offer load to the network for testing. -* [tunnelbroker.sh](#tunnelbrokersh) - a script to set up a IPv6 6-in-4 tunnel to TunnelBroker.net. *This script has not been converted for OpenWrt* +* [tunnelbroker.sh](#tunnelbrokersh) - a script to set up a IPv6 6-in-4 tunnel to TunnelBroker.net. +*This script was originally created for CeroWrt. It has not been converted for OpenWrt/LEDE.* -These scripts can be saved in the `/usr/lib/OpenWrtScripts` directory. The easiest way to do this is to use ssh into the router and enter these commands: +These scripts can be saved in the `/usr/lib/OpenWrtScripts` directory. +The easiest way to do this is to use ssh into the router and enter these commands: ``` opkg update @@ -37,7 +44,23 @@ In the example below, the output would contain results from the standard set of **Sample output file:** See a sample output file - [openwrtstats.txt](./sample_output/openwrtstats.txt) --- +## opkgscript.sh +The `opkgscript.sh` script helps to restore the current set of packages after a sysupgrade +or even a clean install of either LEDE or OpenWrt. +By default, the `write` command saves the list of installed packages in +`/etc/config/opkg.installed` (where it will be preserved across sysupgrades), and the +`install` command reads the file, to restore that set of packages. + +**Example:** + +`sh opkgscript.sh write` _use before sysupgrade to save the current set of packages_ + +`sh opkgscript.sh install` _use after successful sysupgrade, to restore those packages_ + +`sh opkgscript.sh` _display full help information for the script_ + +--- ## config-openwrt.sh The `config-openwrt.sh` script updates the factory settings of OpenWrt to a known-good configuration. @@ -58,7 +81,8 @@ All the sections are commented out. There are sections for: - Enable mDNS/ZeroConf on the WAN interface - Set the SQM (Smart Queue Management) parameters -_[Note: the remaining items have not been converted to work on OpenWrt yet +_[ Note: the remaining items have not been converted to work on OpenWrt yet ]_ + - Enable NetFlow export for traffic analysis - Change default IP addresses and subnets for interfaces - Change default DNS names @@ -68,11 +92,11 @@ _[Note: the remaining items have not been converted to work on OpenWrt yet **To run this script** -Flash the router with factory firmware. Then telnet in and execute these statements. +Flash the router with factory firmware. Then telnet/ssh in and execute these statements. You should do this over a wired connection because some of these changes may reset the wireless network. - telnet 192.168.1.1 + ssh root@192.168.1.1 cd /tmp cat > config.sh [paste in the contents of this file, then hit ^D] @@ -185,16 +209,17 @@ The `networkhammer.sh` script continually invokes the netperfrunner script to pr --- ## tunnelbroker.sh -_[This script has not been converted yet]_ +_[This script was originally created for CeroWrt. It has not been converted for OpenWrt/LEDE.]_ -The `tunnelbroker.sh` script configures OpenWrt to create an IPv6 tunnel via Hurricane Electric. -It's an easy way to become familiar with IPv6 if your ISP doesn't offer native IPv6 capabilities. There are three steps: +The `tunnelbroker.sh` script configures CeroWrt to create an IPv6 tunnel via Hurricane Electric. +It's an easy way to become familiar with IPv6 if your ISP doesn't offer native IPv6 capabilities. +There are three steps: 1. Go to the Hurricane Electric [TunnelBroker.net](http://www.tunnelbroker.net/) site to set up your free account. There are detailed instructions for setting up an account and an IPv6 tunnel at the [IPv6 Tunnel page.](http://www.bufferbloat.net/projects/cerowrt/wiki/IPv6_Tunnel) 2. Edit the tunnelbroker.sh script, using the parameters supplied by Tunnelbroker.net. They're on the site's "Tunnel Details" page. Click on the "Example Configurations" tab and select "OpenWRT Backfire 10.03.1". Use the info to fill in the corresponding lines of the script. -3. ssh into the OpenWrt router and execute this script with these steps. +3. ssh into the router and execute this script with these steps. ssh root@172.30.42.1 cd /tmp diff --git a/opkgscript.sh b/opkgscript.sh new file mode 100755 index 0000000..55f6c16 --- /dev/null +++ b/opkgscript.sh @@ -0,0 +1,200 @@ +#! /bin/sh + +# Write a list of packages currently installed or read that list, +# presumably after a firmware upgrade, in order to reinstall all packages +# on that list not currently installed +# +# (c) 2013 Malte Forkel +# +# Version history +# 0.2.1 - fixed typo in awk script for dependency detection +# 0.2.0 - command interface +# 0.1.0 - Initial release + +PCKGLIST=/etc/config/opkg.installed # default package list +SCRIPTNAME=$(basename $0) # name of this script +COMMAND="" # command to execute + +INSTLIST=$(mktemp) # list of packages to install +PREQLIST=$(mktemp) # list of prerequisite packages + +UPDATE=false # update the package database +OPKGOPT="" # options for opkg calls +VERBOSE=false # be verbose + +cleanup () { + rm -f $INSTLIST $PREQLIST +} + +echo_usage () { + echo \ +"Usage: $(basename $0) [options...] command [packagelist] + +Available commands: + help print this help text + write write a list of currently installed packages + install install packages on list not currently installed + script output a script to install missing packages + +Options: + -u update the package database + -t test only, execute opkg commands with --noaction + -v be verbose + +$SCRIPTNAME can be used to re-install those packages that were installed +before a firmware upgrade but are not part of the new firmware image. + +Before the firmware upgrade, execute + + $SCRIPTNAME [options...] write [packagelist] + +to save the list of currently installed packages. The default package list +is '$PCKGLIST'. Save the package list in a place that will +not be wiped out by the firmware upgrade or copy it to another computer +before the upgrade. + +After the firmware upgrade, execute + + $SCRIPTNAME [options...] install [packagelist] + +to re-install all packages that were not part of the firmware image. +Alternatively, you can execute + + $SCRIPTNAME [options...] script [packagelist] + +to output a shell script that will contain calls to opkg to install those +missing packages. This might be useful if you want to check which packages +would be installed of if you want to edit that list. + +In order for this script to work after a firmware upgrade or reboot, the +opkg database must have been updated. You can use the option -u to do this. + +You can specify the option -t to test what $SCRIPTNAME would do. All calls +to opkg will be made with the option --noaction. This does not influence +the call to opkg to write the list of installed packages, though. +" +} + +trap cleanup SIGHUP SIGINT SIGTERM EXIT + +# parse command line options +while getopts "htuvw" OPTS; do + case $OPTS in + t ) + OPKGOPT="$OPKGOPT --noaction";; + u ) + UPDATE=true;; + v ) + VERBOSE=true;; + [h\?*] ) + echo_usage + exit 0;; + esac +done +shift $(($OPTIND - 1)) + +# Set the command +COMMAND=$1 + +# Set name of the package list +if [ "x$2" != "x" ]; then + PCKGLIST="$2" +fi + +# +# Help +# + +if [ "x$COMMAND" == "x" ]; then + echo "No command specified." + echo "" + COMMAND="help" +fi + +if [ $COMMAND == "help" ]; then + echo_usage + exit 0 +fi + +# +# Write +# + +if [ $COMMAND = "write" ] ; then + if $VERBOSE; then + echo "Saving package list to $PCKGLIST" + fi + # NOTE: option --noaction not valid for list-installed + opkg list-installed > "$PCKGLIST" + exit 0 +fi + +# +# Update +# + +if $UPDATE; then + opkg $OPKGOPT update +fi + +# +# Check +# + +if [ $COMMAND == "install" ] || [ $COMMAND == "script" ]; then + # detect uninstalled packages + if $VERBOSE && [ $COMMAND != "script" ]; then + echo "Checking packages... " + fi + cat "$PCKGLIST" | while read PACKAGE SEP VERSION; do + # opkg status is much faster than opkg info + # it only returns status of installed packages + #if ! opkg status $PACKAGE | grep -q "^Status:.* installed"; then + if [ "x$(opkg status $PACKAGE)" == "x" ]; then + # collect uninstalled packages + echo $PACKAGE >> $INSTLIST + # collect prerequisites + opkg info "$PACKAGE" | + awk "/^Depends: / { + sub(\"Depends: \", \"\"); \ + gsub(\", \", \"\\n\"); \ + print >> \"$PREQLIST\"; \ + }" + fi + done +fi + +# +# Install or script +# + +if [ $COMMAND == "install" ]; then + # install packages + cat "$INSTLIST" | while read PACKAGE; do + if grep -q "^$PACKAGE\$" "$PREQLIST"; then + # prerequisite package, will be installed automatically + if $VERBOSE; then + echo "$PACKAGE installed automatically" + fi + else + # install package + opkg $OPKGOPT install $PACKAGE + fi + done +elif [ $COMMAND == "script" ]; then + # output install script + echo "#! /bin/sh" + cat "$INSTLIST" | while read PACKAGE; do + if ! grep -q "^$PACKAGE\$" "$PREQLIST"; then + echo "opkg install $PACKAGE" + fi + done +else + echo "Unknown command '$COMMAND'." + echo "" + echo_usage + exit 1 +fi + +# clean up and exit +exit 0