Click here to Skip to main content
13,355,335 members (74,810 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want to run shell script which has "ifconfig" command written and placing output to another file.

Actually we dont know the ip address of the machine, we have deployed Linux on it. we want to know IP address of that machine once it gets boot up. so on boot up it will create one file with ifconfig output, so we can read ip address.


#mount shared drive
mount -t cifs -o username= ,password= // /mnt

#delete file if it already exists locally
if [ -f /$hostname.txt ];
	rm -f /$hostname.txt

#delete file if it exists in the share
if [ -f /mnt/$hostname.txt ];
	rm -f /mnt/$hostname.txt

#create new file
touch $hostname.txt
#redirect ip details to the file
#copy to the mounted share
cp /$hostname.txt /mnt/$hostname.txt
umount /mnt

i am able to run other commands in startup, but not only ipconfig command.

does anybody help me?

Posted 2-Oct-12 1:45am
Updated 2-Oct-12 22:49pm
Richard MacCutchan 2-Oct-12 9:12am
Without some more detail it is impossible to guess what is happening. Please edit your question and show the command that you are trying to run and any output from the script.
skydger 3-Oct-12 3:10am
Is it possible that your script's work starts when net interface is not initialized yet?
YogeshDewangan@dell 3-Oct-12 3:56am
No its not the case, because before that we are mounting with IP address.
Richard MacCutchan 3-Oct-12 4:28am
What happens when you run ifconfig manually, what output do you see? Also what does get written into the $hostname.txt file?
YogeshDewangan@dell 3-Oct-12 4:53am
it works properly when i run it manually.
and it gives output like

eth1 Link encap:Ethernet HWaddr 00:50:56:AF:0D:35
inet addr: Bcast: Mask:
inet6 addr: fe80::250:56ff:feaf:d35/64 Scope:Link
RX packets:65 errors:0 dropped:0 overruns:0 frame:0
TX packets:112 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:8809 (8.6 KiB) TX bytes:13998 (13.6 KiB)

lo Link encap:Local Loopback
inet addr: Mask:
inet6 addr: ::1/128 Scope:Host
RX packets:14 errors:0 dropped:0 overruns:0 frame:0
TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:824 (824.0 b) TX bytes:824 (824.0 b)

so i can extract ip address from this.

but not at machine start ups.
Richard MacCutchan 3-Oct-12 5:15am
Are you sure you are not running it too early? The startup scripts run in fairly strict order and you cannot run this command until all the network interfaces have been started properly. It's a while since I worked on Linux but if you check through the scripts you should be able to see where it can run.
YogeshDewangan@dell 3-Oct-12 5:23am
may be, but i can not find any solution for this. how to delay the script or how can we be sure that it should run at last.
Richard MacCutchan 3-Oct-12 5:30am
Look at the etc/rc man pages and make sure your script runs at the appropriate section after all the network setup.
MidhunLC 3-Oct-12 6:26am
In the script above,we are mounting a windows partition and copying a file to it as the script proceeds.
The ifconfig command is executed after the mount is done.
So definitely,network setting is done before ifconfig is run.
Or else mount wouldnt work
I have tried redirecting other commands to the file and its working without any trouble at all.(whoami,top,man (any command))
If I try commands like ifconfig or ip addr show then the output is empty.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

If you have no any other interfaces except these two listed above, then I suggest you to check your script using full path to ifconfig. I assume it is /sbin/ifconfig. Maybe the PATH environment variable does not contain /sbin at script running time or there are too few rights to execute it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180111.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100