Executing a command via shell script.

T-Daemon, Even with # BEFORE: netif it do not work.
I've tried also.
The output of the command
rcorder -p /etc/rc.d/* shows that hostname execute before netif.
 
I think I resolved the problem by adding ifconfig in the script
Bash:
#!/bin/sh

# PROVIDE: ucgetif
# REQUIRE: FILESYSTEMS
# BEFORE: netif

. /etc/rc.subr

name="ucgetif"
rcvar="ucgetif_enable"

start_cmd="${name}_start"

Bsd_dmesg="/sbin/dmesg"
Bsd_grep="/usr/bin/grep"
Bsd_sed="/usr/bin/sed"
Bsd_awk="/usr/bin/awk"
Bsd_cat="/bin/cat"
Bsd_ifconfig="/sbin/ifconfig"
Bsd_msgd="/var/run/dmesg.boot"

ucgetif_start() {
      eth_patern="XXXX"
      eth_X=$(${Bsd_cat} /etc/rc.conf | ${Bsd_grep} $eth_patern)
      if [ -n $eth_X ]; then
        eth_ifs=$(${Bsd_dmesg} | ${Bsd_sed} -n 's/^\(.*\):\ Ethernet\ address:.*/\1/p')
        while ps -axg | grep -vw grep | grep -w dmesg > /dev/null; do sleep 1; done
        $Bsd_sed -i '' -e "s/$eth_patern/$eth_ifs/g" /etc/rc.conf
        while ps -axg | grep -vw grep | grep -w sed > /dev/null; do sleep 1; done
        $Bsd_ifconfig $eth_ifs name "genet0"
        while ps -axg | grep -vw grep | grep -w ifconfig > /dev/null; do sleep 1; done
      fi  
}

run_rc_command "$1"

Thank you for your patience 😀
 

Attachments

  • fb1.jpg
    fb1.jpg
    106.9 KB · Views: 37
The problem is that /etc/rc loads /etc/rc.conf and runs run_rc_script.
This means that any modification to /etc/rc.conf will no be seen by rc(8) and the interface XXXX does not exists.
This make sense because otherwise every scripts would have to 'reread' rc.conf every time.
The trick here is to rename the interface directly.
 
Tip, you can use ifconfig_DEFAULT="....". That will always use the first detected ethernet card. Which is usually fine for most generic servers or embedded devices with only one ethernet adapter.

If you need to use a specific interface (for firewall rules for example), you could do ifconfig_DEFAULT_name="eth0". Then base everything on the eth0 interface name. This little 'trick' would work fine for systems that only have one ethernet connection. No need to figure out if it's ng0, or em0, or igb0, it will automagically pick the first interface.
Talking about firewalls and FreeBSD OS with many ethernet interfaces ( saying 4 interfaces [ em0, gbe0, em1, rl0 ] ) in this order.
My question is : Will the kernel detect interfaces in the same order in every boot ?
Thank you.
 
Talking about firewalls and FreeBSD OS with many ethernet interfaces ( saying 4 interfaces [ em0, gbe0, em1, rl0 ] ) in this order.
My question is : Will the kernel detect interfaces in the same order in every boot ?
Thank you
I do not know, I'd say yes.

But as SirDice pointed out:
If you just need a list of interfaces; ifconfig -l. No need to grep through system logs.

So your rc script should most likely look something like this.
Bash:
myscript_start()
{
        local iface=    # make sure there is no conflict with an other $iface variable

        for iface in $(ifconfig -l); do
                case "${iface}" in
                lo[0-9]) : skip ;;
                ng0)
                         /sbin/ifconfig ng0 inet 192.0.x.y/24 name eth0
                         /sbin/route add default 192.168.x.y
                         ;;
                em[0-9])
                         /sbin/dhclient ${iface}
                         ;;
                ... ... ...
                esac
        done
}
 
Maybe /etc/start_if.IF is more suited for this?
rc.conf(5)
Code:
                 If the /etc/start_if.⟨interface⟩ file is present, it is read
                 and executed by the sh(1) interpreter before configuring the
                 interface as specified in the ifconfig_⟨interface⟩ and
                 ifconfig_⟨interface⟩_alias⟨n⟩ variables.
 
I do the same thing. I think this is a great way to learn where everything is on the filesystem.
It depends on the direction from which you approach the issue.
People who script for a variety of Unix variants avoid hard coding paths like the plague.
They set and export the PATH to cover all usual circumstances:
Code:
[strand.283] $ uname -s; which gzip
FreeBSD
/usr/bin/gzip

[pi3b.814] $ uname -s; which gzip
Linux
/bin/gzip
 
Why do you feel you need to hardcode the paths?
I think it is an heritage from other programming language ( PASCAL ) that I use frequently on UNIX systems ( FreeBSD, OpenBSD), & It is good for me to memory commands locations.

Else, Yes I can not do that.

Thank you all.
 
Hello.
While booting, I see a warning
/etc/rc: WARNING: $ucgetif_enable is not set properly - see rc.conf(5)., the script is the same.
Some one could you tel me the reason ??
PS: After the first start of FreeBSD, the script above will be executed since ucgetif_enable="YES" in rc.conf
the script do changes, and comment also the #ucgetif_enable variable in rc.conf because it is one shoot script.
Thank you.
 
While booting, I see a warning
/etc/rc: WARNING: $ucgetif_enable is not set properly - see rc.conf(5)., the script is the same.
Some one could you tel me the reason ??
The warning massage results from this piece of code from /etc/rc.subr:
Code:
...
checkyesno()
{
        eval _value=\$${1}
        debug "checkyesno: $1 is set to $_value."
        case $_value in

                #       "yes", "true", "on", or "1"
        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
                return 0
                ;;

                #       "no", "false", "off", or "0"
        [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
                return 1
                ;;
        *)
                warn "\$${1} is not set properly - see rc.conf(5)."
                return 1
                ;;
        esac
}
...

To silence the message add to /usr/local/etc/rc.d/ucgetif
Code:
...
: ${ucgetif_enable:="NO"}
...
 
Back
Top