Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Linux
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.
 
code:
hostname=$(hostname)
 
#mount shared drive
mount -t cifs -o username= ,password= //111.111.111.111/MLC /mnt
 
#delete file if it already exists locally
if [ -f /$hostname.txt ];
then
	rm -f /$hostname.txt
fi
 
#delete file if it exists in the share
if [ -f /mnt/$hostname.txt ];
then
	rm -f /mnt/$hostname.txt
fi
 
#create new file
touch $hostname.txt
#redirect ip details to the file
ifconfig>$hostname.txt
#copy to the mounted share
cp /$hostname.txt /mnt/$hostname.txt
#unmount
umount /mnt
 

i am able to run other commands in startup, but not only ipconfig command.
 
does anybody help me?
 
Thanks
Posted 2-Oct-12 0:45am
Edited 2-Oct-12 21:49pm
DaveAuld167.1K
v3
Comments
Richard MacCutchan at 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 at 3-Oct-12 3:10am
   
Is it possible that your script's work starts when net interface is not initialized yet?
YogeshDewangan@dell at 3-Oct-12 3:56am
   
No its not the case, because before that we are mounting with IP address.
Richard MacCutchan at 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 at 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:10.94.103.171 Bcast:10.94.103.255 Mask:255.255.255.128
inet6 addr: fe80::250:56ff:feaf:d35/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
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:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
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 at 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 at 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 at 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 at 3-Oct-12 6:26am
   
Hi,
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
good
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 499
1 Nirav Prabtani 338
2 OriginalGriff 175
3 _Amy 165
4 Abhinav S 160
0 OriginalGriff 7,715
1 Sergey Alexandrovich Kryukov 6,911
2 Maciej Los 3,954
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2014
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