Turn net-setup.sh into a function library

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
s243a
Posts: 2580
Joined: Tue 02 Sep 2014, 04:48
Contact:

Turn net-setup.sh into a function library

#1 Post by s243a »

net-setup.sh was a script originated by Barry Kauler in 2004 and later enhanced by others. It is useful for finding and loading network interfaces. It is called in network configuration through the following actions.

In the menu select
Setup -> "Internet Connection Wizard"

which loads the script:
/usr/sbin/connectwizard

If you then select the button which says
"Wired or Wireless Land"
a new wizard is loaded for wired & wireless LANs. This new wizard is defined in the file:
/usr/sbin/connectwizard_2nd

This wizard gives three choices of wizards for configuring wired or wireless LAN. They are:

"Simple Network Setup"

"Frisbee"

"Network WIzard"

it is the last option which loads:

/usr/sbin/net-setup.sh

The description for the wizard is
This is the most sophisticated tool for network setup in PUppy. It is based on the 'Classic' Network Wizard originaly written by BarryK, but greatly enhanced by Doubal, Rarsa, Shinobar and others. It is for both wired (Ethernet) and wireless connectivity, with more configuration features than Frisbee and SNS
The strength of this wizard from my experience is that it is able to load network drives which cannot be loaded by the "Simple Network Connection Wizard".

The line numbers that I will quote in the following text assume the user is using tahrpup but the script should be similar in most recent versions of puppylinux (fatdog64 being a notable exception).

The first problem area to fix is the script sets an app directory based on where the function is called from. This may make it easy to move the script but it is problematic if we try to load the script into the current evaluation context using the source operator.

To remady this I changed lines 75 to 78 of net-setup.sh from

Code: Select all

APPDIR="$(dirname $0)"
[ "$APPDIR" = "." ] && APPDIR="$(pwd)"
to

Code: Select all

if ! [ -n "$APPDIR" ]; then
  APPDIR="$(dirname $0)"
  [ "$APPDIR" = "." ] && APPDIR="$(pwd)"
fi
what this does is allows you to set the variable APPDIR outside of net-setup.sh. (I need to test to see whether an export statement is required).

I could add a second test that checks if the evaluation context is something other than net-setup.sh. We will see this trick below for a different application.

The next thing that you have to do is to ensure that the following functions

Code: Select all

 setDefaultMODULEBUTTONS
  refreshMainWindowInfo
  showMainWindow
will not be loaded by default when the script is loaded into the current evaluation context via the source operator.

The evaluation context can be obtained by the code

Code: Select all

CURRENT_CONTEXT=$(expr "$0" : '.*/\(.*\)$' )
and then if CURRENT_CONTEXT does not equal net-setup.sh we know the script was loaded via the source operator rather than called as a separate subshell. In the prior case we which to avoid calling the functions mentioned above.

To accomplish this lines 1809 to 1842 of my net-setup.sh look as follows

Code: Select all

#=============================================================================
#=============== START OF SCRIPT BODY ====================
#=============================================================================


# Cleanup older temp files (in case didn't exit nicely last time)
cleanUpTmp
# Do we have pcmcia hardware?...
if elspci -l | grep -E -q '60700|60500' ; then
  MPCMCIA="yes"
fi


BGCOLOR="#ffe0e0" #light red.
TOPMSG="$L_TOPMSG_Initial"

CURRENT_CONTEXT=$(expr "$0" : '.*/\(.*\)$' )
if [ "${CURRENT_CONTEXT}" = "net-setup.sh" ] ; then
  setDefaultMODULEBUTTONS

  refreshMainWindowInfo


  showMainWindow

  # Dougal: clean up /tmp
  cleanUpTmp

  #v411 BK hack to remove old network wizard configs so rc.sysinit won't use them if old wizard installed...
  [ "`ls -1 /etc/network-wizard/network/interfaces 2>/dev/null`" != "" ] && rm -f /etc/*[0-9]mode
fi
#=============================================================================
#================ END OF SCRIPT BODY =====================
#=============================================================================
Now that we have done this we can test out some of the functions in the script. From the following

Code: Select all

root# getInterfaceList
root# echo "$INTERFACES"
eth0 ra0
we see that the function getInterfaceList simply returns the names of the interfaces. While on the surface this doesn't seem very usefully it let's me know what other functions are looking for. I know that ra0 is the interface for my wireless network dongle. And I expect that this information is needed as inputs to other functions.

So based on this let's try

Code: Select all

root# findInterfaceInfo "ra0"
root# echo "$INTTYPE $FI_DRIVER $TYPE $INFO"
Wireless usb usb Ralink 11n Adapter
This might be useful and if not being able to call the function individually from the command line will make it easier for someone to understand how the network wizards work.

As a final note the function configureWirless of net-setup.sh assigns it's first argument to a variable called interface. Let's assume it is looking for something along the lines of "ra0". In the second line of the function configureWirless there is the following statement

Code: Select all

showProfilesWindow "$INTERFACE"
the function showProfilesWindow is from the file:
/usr/sbin/wag-profiles.sh

wag-profiles.sh when called as a sub-shell loads a wizard for connecting your wireless interface to a network device via wirelss Ethernet. However, it does not assign an ip adress. The ip adress is assigned in the wizard which called net-setup.sh (or equivlanetly the showProfilesWindow() function)

Alternatively, If wag-profiles.sh is called via the source operatoer then it acts as a function library.

My next goal is to use the functions in both:
net-setup.sh and wag-profiles.sh to configure my network without using the network wizards. I will probably explain this in a separate thread.

In this thread I will explore more of the functions in net-setup.sh via the command line.

***As a final disclaimer, net-setup.sh is called from many files within puppylinux. We can see which files by using a grep command. I cannot promise that my modifications won't break anything. I will let people know whether I have any issues with my mods.

s243a
Posts: 2580
Joined: Tue 02 Sep 2014, 04:48
Contact:

#2 Post by s243a »

As planned, I buit a connect script, using the functions which were part of the network wizard.

My connect script is as follows:

Code: Select all

#!/bin/bash
PROFILE_TITLE="TELUS7201" #See Line 1039 of /usr/sbin/wag-profiles.sh
APPDIR=/usr/sbin/
INTERFACE=ra0

. /usr/sbin/net-setup.sh
# This also indirectly loads:
# /usr/sbin/ndiswrapperGUI.sh
# /usr/sbin/wag-profiles.sh
# See lines 117 and 118 pf wag-profile.sh of  

#The next three lines essentially do the same thing as the loadProfileData() 
# function (line 1036 of /usr/sbin/wag-profiles.sh) 
PROFILE_FILE=$( grep -l "TITLE=\"${PROFILE_TITLE}\"" ${PROFILES_DIR}/*.conf | head -n1 )
. "$PROFILE_FILE"
assignProfileData

#the function "useProfile calls the appropriate wpa method to connect to the wireless gateway.
useProfile

#This function is from line 198 of /usr/sbin/wag-profiles.sh
setupDHCP
# It is called from line 1103 of /usr/sbin/net-setup.sh within the function 
# showConfigureInterfaceWindow(). A screen shot of the GUI rendered by
# showConfigureINterfaceWindow can be found at:
# http://www.pearltrees.com/s243a/net-setup-usr-sbin-puppylinux/id16402846/item186280428

route del default gw 192.168.0.1 eth0
The last line seems out of place but for some reason a default gateway gets created for my ethernet interface. I don't want this because I'm using the usb interface for a wifi connection to the internet and hard wired Ethernet just for a local network.

My internet deosn't seem to work when there are two default gateways. See my other (thread), so I must delete the default gatway for the hard wired interface by the method shown above.

Regarding my original post, modifying net-setup.sh probably wasn't necessary for my above script to run. However, the modifications noted in the original post did help me better understand how the network wizards worked and therefore indirectly helped me write this script.

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#3 Post by Karl Godt »

Code: Select all

if ! [ -n "$APPDIR" ]; then
  APPDIR="$(dirname $0)"
  [ "$APPDIR" = "." ] && APPDIR="$(pwd)"
fi 
above code could be replaced by
some bashism:

Code: Select all

MY_SELF=`realpath "$0"` 
APPDIR=${APPDIR:-`dirname "$MY_SELF"`}
«Give me GUI or Death» -- I give you [[Xx]term[inal]] [[Cc]on[s][ole]] .
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal :P

Post Reply