shell scripting

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

shell scripting

pluto mars
Hi,

I need some help with shell scripting (/bin/sh) in ubuntu.

In a shell script I want to turn down an interface, say eth0 and then turn it up.
So something like

ifdown eth0
ifup eth0

But what I want in my shell script is to check and wait till eth0 is up and then start doing something next in my shell script.
So something like "if ( eth0 not is up ) { sleep 1 }. But how do I do this in shell scripting ?

And then also I want something similar for checking if syslog process is up or not. Because actually I am writing syslog through that interface to another computer. So I want to check if eth0 is up. If up, check if syslog process is up, if not start, if yes, write something to syslog.

regards,

pluto

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: shell scripting

ping
there is no good way with pure shell script without using expect-like kind of tools..

from iPhone


On Nov 10, 2013, at 0:23, pluto mars <[hidden email]> wrote:

> Hi,
>
> I need some help with shell scripting (/bin/sh) in ubuntu.
>
> In a shell script I want to turn down an interface, say eth0 and then turn it up.
> So something like
>
> ifdown eth0
> ifup eth0
>
> But what I want in my shell script is to check and wait till eth0 is up and then start doing something next in my shell script.
> So something like "if ( eth0 not is up ) { sleep 1 }. But how do I do this in shell scripting ?
>
> And then also I want something similar for checking if syslog process is up or not. Because actually I am writing syslog through that interface to another computer. So I want to check if eth0 is up. If up, check if syslog process is up, if not start, if yes, write something to syslog.
>
> regards,
>
> pluto
> --
> ubuntu-users mailing list
> [hidden email]
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: shell scripting

Colin Law-4
In reply to this post by pluto mars
On 9 November 2013 16:23, pluto mars <[hidden email]> wrote:

> Hi,
>
> I need some help with shell scripting (/bin/sh) in ubuntu.
>
> In a shell script I want to turn down an interface, say eth0 and then turn
> it up.
> So something like
>
> ifdown eth0
> ifup eth0
>
> But what I want in my shell script is to check and wait till eth0 is up and
> then start doing something next in my shell script.
> So something like "if ( eth0 not is up ) { sleep 1 }. But how do I do this

If no-knows a better way you could ping the router until it replies,
or ping the machine you want to connect to which would allow you to
cope with that machine being down also.

> in shell scripting ?
>
> And then also I want something similar for checking if syslog process is up
> or not. Because actually I am writing syslog through that interface to
> another computer. So I want to check if eth0 is up. If up, check if syslog
> process is up, if not start, if yes, write something to syslog.

Could you just look for the process?

Colin

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: shell scripting

Hal Burgiss
In reply to this post by pluto mars
On Sat, Nov 9, 2013 at 11:23 AM, pluto mars <[hidden email]> wrote:
In a shell script I want to turn down an interface, say eth0 and then turn it up. 
So something like

ifdown eth0
ifup eth0

But what I want in my shell script is to check and wait till eth0 is up and then start doing something next in my shell script.

untested pseudo code ...

function is_up () { 
  IFACE=eth0
    if ! ifconfig | grep $IFACE >/dev/null 2>&1 ||\
      ! ifconfig $IFACE | grep UP >/dev/null 2>&1; then
         # maybe do additional network test here like ping google.com to see if interface is up and connection is usable 
          return 1
   fi
   return 0
}

while ! is_up; do 
  # probably need timeout logic here, ie count loops, exit on error
  sleep 1;
done 

 
So something like "if ( eth0 not is up ) { sleep 1 }. But how do I do this in shell scripting ?

And then also I want something similar for checking if syslog process is up or not. Because actually I am writing syslog through that interface to another computer. So I want to check if eth0 is up. If up, check if syslog process is up, if not start, if yes, write something to syslog.


if ps aux |grep syslogd |grep -v grep > /dev/null .... blah. 
 

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: shell scripting

Marius Gedminas-2
In reply to this post by pluto mars
On Sat, Nov 09, 2013 at 05:23:19PM +0100, pluto mars wrote:
> I need some help with shell scripting (/bin/sh) in ubuntu.
>
> In a shell script I want to turn down an interface, say eth0 and then turn
> it up.

If you told us why you want to write this shell script, we might be able
to come up with a better way to solve whatever problem you're trying to
solve.

> So something like
>
> ifdown eth0
> ifup eth0
>
> But what I want in my shell script is to check and wait till eth0 is up and
> then start doing something next in my shell script.
> So something like "if ( eth0 not is up ) { sleep 1 }. But how do I do this
> in shell scripting ?

Do you use DHCP?  If so, it may not be sufficient to wait for the
interface to be up, you probably also want for a valid DHCP lease.

The simplest way I can think of is to wait until the default route gets
configured:

    while ! ip r | grep -q default; do
        sleep 1
    done

> And then also I want something similar for checking if syslog process is up
> or not. Because actually I am writing syslog through that interface to
> another computer. So I want to check if eth0 is up. If up, check if syslog
> process is up, if not start, if yes, write something to syslog.

Why would syslog not be up?  Upstart ensures rsyslog will be present via
/etc/init/rsyslog.conf.

You can check for its presence and with something like

    if [ -z "$(pidof -s rsyslogd)" ]; then
        service rsyslogd start
    fi

or just issue the 'service rsyslogd start' unconditionally, it does
nothing if rsyslogd is already running.

Marius Gedminas
--
Gates' Law: Every 18 months, the speed of software halves.

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

signature.asc (197 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: shell scripting

Oliver Grawert
In reply to this post by pluto mars
hi,
Am Samstag, den 09.11.2013, 17:23 +0100 schrieb pluto mars:

> Hi,
>
>
> I need some help with shell scripting (/bin/sh) in ubuntu.
>
>
> In a shell script I want to turn down an interface, say eth0 and then
> turn it up.
>
> So something like
>
>
> ifdown eth0
>
> ifup eth0
>
>
> But what I want in my shell script is to check and wait till eth0 is
> up and then start doing something next in my shell script.
>
> So something like "if ( eth0 not is up ) { sleep 1 }. But how do I do
> this in shell scripting ?
>
try using an upstart job instead ;) this will be 10x easier, smaller and
perfectly reliable (vs polling the device or pinging a router or
whatever)
http://upstart.ubuntu.com/cookbook/

alternatively an if-up.d script could work as well (though there you
still would have to poll for syslog running which upstart gives you for
free via events)

/etc/network/if-up.d/openssh-server is a good example of an if-up.d
script that triggers on specific interfaces (if you have ssh installed)

ciao
        oli

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

signature.asc (205 bytes) Download Attachment