1

i have created a small script for soalris OS which will check which ethernet card is under DHCP control as below :

for i in `/usr/sbin/ifconfig -a | awk '/flags/ {print $1}' | grep -v lo | sed 's/://g'`
do
echo `ifconfig $i dhcp status` 
done >> /tmp/logfile

but this is only creating a logfile at /tmp but not writing the stdout to it.Only stdout is displayed on the promt as below :

ifconfig: e1000g1: interface is not under DHCP control
ifconfig: e1000g1: interface is not under DHCP control

can someone help me to correct if am doing something wrong while re directing for loops output...

2 Answers 2

3

That output is going to stderr not to stdout, so you need to do 2>> to redirect it.

Somewhat simplified command:

ifconfig -a | sed -n '/flags/!d;/lo/d;s/:.*//g;p' | \
      xargs -n 1 I{} ifconfig {} dhcp status 2>> /tmp/logfile 
Sign up to request clarification or add additional context in comments.

5 Comments

thanks a lot perreal for your response..could you please elaborate me the way you have used sed and xargs .. i am new to scripting world..also i have tried to run your script but it gave me below errors :Label too long: /flags/b;/lo/b;s/:.*//g;p
continuation of error :xargs: #args must be positive int: I{} xargs: Usage: xargs: [-t] [-p] [-e[eofstr]] [-E eofstr] [-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] [cmd [args ...]]
@user3484779, ops, I forgot to put an argument, try the updated answer.
@user3484779, basically this uses sed to do the most processing. the b command in sed quits processing a line. Here it says if flags don't match skip the line. If lo matches skip the line. otherwise remove the part after the column, including the column and print the line.
i have tried to run your script but it failed with below error:Label too long: /flags/!b;/lo/b;s/:.*//g;p
0

You are redirecting stdout to /tmp/logfile and the messages you see are printed to stderr which you do not redirect.

{
  ifconfig -a | awk -F: '/flags/&&$1!~/^lo/{print $1}' | while read if; do
    ifconfig "${if}" dhcp status
  done
} >/tmp/logfile 2>&1

This will redirect all output to /tmp/logfile including stderr.

Explanation

The parentheses { ... } allow to group commands so that we can redirect the output of the whole snippet inside at once.

>/tmp/logfile 2>&1 redirects stderr to stdout and writes both to the file /tmp/logfile.

awk -F: '/flags/ && $1 !~ /^lo/ {print $1}':

  1. -F: sets the field delimiter to :
  2. /flags/ && $1 !~ /^lo/ {print $1} means "if the line contains flag and the first column does not start with the string lo print the first column (the interface name)

The while read if; do [...] done loops over those interface names line by line and reads them into the variable $if with each iteration.

3 Comments

thank you very much .. this script gave me the desired result..Could you please explain me the working of the script as i am very new to scripting world..:)
>Also how can we exclude lo0 from output as this is loopback.
@user3484779 I added an explanation and adapted the loopback filter, hope this gets you started!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.