IP check with shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting IP check with shell script
# 1  
Old 02-06-2009
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 Smilie... hope someone can help me ... thx.
# 2  
Old 02-06-2009
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  
Old 02-06-2009
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  
Old 02-09-2009
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.. Smilie..


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  
Old 02-09-2009
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  
Old 02-10-2009
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

This User Gave Thanks to bladez For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to check a file

I'm required to write a simple shell script that when it runs it writes the output which is a simple barcode to a tmp flat file which I can do the bit I'm struggling with... The next time it runs I need to check the tmp output file to see if that barcode is in the output file and if it is send... (5 Replies)
Discussion started by: worky
5 Replies

2. UNIX for Dummies Questions & Answers

Need shell script to check file

Hi Experts, I am not good in writing script. Just stared.I am looking for shell script to check following parameters. 1) Number of files on remote Linux SUSE server.- Any directory and sub directory. 2) I should define number of files in script. Files should be variable. 3) Age of... (2 Replies)
Discussion started by: ApmPerfMonitor
2 Replies

3. Shell Programming and Scripting

Shell script to check for a Occurance

Hi All, I am very new to this forum and Unix too... I need to create a script which has to check for a file's occurrence in a specified location for 4 hours and send out a email when ever the file has arrived. It should also have a timer to send the status message for every 15 minutes if the... (2 Replies)
Discussion started by: shandivi
2 Replies

4. Shell Programming and Scripting

Bash shell script to check if script itself is running

hi guys we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the... (8 Replies)
Discussion started by: hedkandi
8 Replies

5. Shell Programming and Scripting

help on shell script to check line

Hi Scripting Gurus, Can someone help to transform the below logic into a shell script, might be easy for some of you. I have a file with below text, I need if the line has the ":" and the above to it is not a blank line should print " <text>: is incorrect format" Apple: ... (3 Replies)
Discussion started by: usyseng
3 Replies

6. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

7. Shell Programming and Scripting

script check the users in SHELL ?

could you please find a solution for this script that checks if any of a list of users given on the command line is logged in. For each user the script should say whether this user is logged in or not. The script prints an error message if no parameter is given. And ask the user to enter a name... (3 Replies)
Discussion started by: anything
3 Replies

8. Shell Programming and Scripting

Check EOF in shell script

Hi, I need to check whether one file has EOF or not. There's one daemon in our system, which will pick the files FTPed to us. However, sometimes the daemon picks the file before FTP is finished. So I'm planning to add some checking of EOF on the FTPed files in the shell script. Could... (8 Replies)
Discussion started by: ideazhcy
8 Replies

9. Shell Programming and Scripting

check my first shell script

I have written the below script to determine whether a string is palindrome or not ? But its not working, any help to debug it ? I am new to this forum but when I searched for my question, I found that many people refused to answer this question thinking that its Homework question, Therefore I... (2 Replies)
Discussion started by: gridview
2 Replies

10. Shell Programming and Scripting

check in unix shell script so that no one is able to run the script manually

I want to create an automated script which is called by another maually executed script. The condition is that the no one should be able to manually execute the automated script. The automated script can be on the same machine or it can be on a remote machine. Can any one suggest a check in the... (1 Reply)
Discussion started by: adi_bang76
1 Replies
Login or Register to Ask a Question