The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
About shell script to check well formedness of XML Kochu77 Shell Programming and Scripting 2 11-21-2008 02:24 PM
script check the users in SHELL ? anything Shell Programming and Scripting 3 02-01-2008 11:38 AM
Check EOF in shell script ideazhcy Shell Programming and Scripting 8 07-20-2007 03:29 PM
check my first shell script gridview Shell Programming and Scripting 2 05-14-2007 03:34 PM
check in unix shell script so that no one is able to run the script manually adi_bang76 Shell Programming and Scripting 1 11-16-2006 10:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-06-2009
bladez bladez is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 3
IP check with shell script

hi guys ..newbie here i would like to create a simple script tat will detect wether the machine has IP or not ...

but it seems that my script doesnt really work it kept rebooting...

i set this script during boot so that it will check else it will reboot

it a shell script thou...

Code:
 
if [!`hostname -i`] then
       echo "System rebooting in 3s";
       sleep 3;
       reboot;
else
       echo "Your Current IP is : `hostname -i`";
       echo "Starting System in 3s";
       sleep 3;

But this end result was my system end up infinite loops of reboots ... hope someone can help me ... thx.
  #2 (permalink)  
Old 02-06-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,262
I guess this host is getting it's IP via dhcp? Assuming this maybe correct the problem why it didn't get an IP.

Also checking for an IP I would use ifconfig instead maybe.
  #3 (permalink)  
Old 02-06-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,839
At which point of the boot process did you insert your script? Before or after your NIC is brought up? Is there anything else that might reboot the system? Also, why do you reboot the whole system if it didn't get an IP instead of just restarting the DHCP client?

As for your test, I don't think that 'hostname' ever returns nothing as long as there's at least a Loopback available.
Code:
testbed:~ # ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:23:56:F8
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          [...]

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          [...]

testbed:~ # /etc/init.d/network stop
Shutting down network interfaces:
    eth0      device: Advanced Micro Devices [AMD] 79c970 [PCnet32 LANdone(rev 40)
Shutting down service network  .  .  .  .  .  .  .  .  .  .  .  .  .  done
testbed:~ # hostname -i
127.0.0.2
testbed:~ #
  #4 (permalink)  
Old 02-09-2009
bladez bladez is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 3
IP Check Shell Script

Yea i get IP via DHCP , well i am using D-Link switches with 24 pc's attached to it so sometimes som pc's doesnt get ip therefore i dun wan the pc to sit idle so at first yes i did thought of making it reboot but after i found the /etc/init.d/network start/stop i am gonna replace that with the reboot command instead so it will stop n start the NIC again untill it gets ip.

of course i did put this script after the NIC is started for the first time by the OS itself but then during the boot my script checks it to ensure it has IP n not jus loopback. coz i wanna ensure the pc is always on the network all the time.

there no other script that reboots the pc but the script that is below somehow something is wrong i dunno wat makes it keep restarting the network forever instead of checking whether it already has IP or not :


i hv a feeling that my code is wrong somewhr pls help... anyone.. ..


Code:
 
 
if [!`hostname -i`] then
            /etc/init.d/network restart;
else
            echo "IP : `hostname -i`";
fi

Last edited by bladez; 02-10-2009 at 01:25 AM..
  #5 (permalink)  
Old 02-09-2009
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,929
First off, replace that restart with something else, really hard to debug anything when failure means your machine starts over. Have it print out the output of 'hostname' and 'hostname -i'.
  #6 (permalink)  
Old 02-10-2009
bladez bladez is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 3
Solution...

i have found solution to it ... just to share it...

Code:
 
echo "Checking NIC status... Please Wait.";
host **Your Prefered WWW Address**>/dev/null
if [ $? -eq 0 ];
then
echo "Getting Client IP...";
sleep 3;
echo "Your IP : `ifconfig | gawk '/inet addr/{sub(".*:", "", $2);print $2; exit}'`";
else
echo "Re-initiating NIC...";
/etc/rc.d/init.d/network restart;
echo "Re-checking NIC Status...";
ping;
fi
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0