LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2005, 03:24 PM   #1
lp449
Member
 
Registered: Oct 2004
Location: Olsztyn, Poland
Distribution: Debian Squeeze / CentOS 6
Posts: 60

Rep: Reputation: 15
Simple bash script


Hello,

I would need a simple script written in bash to be put in cron to be executed every five minutes. the script itself would have to grep DNS adresses from /etc/resolve.conf and then every five minutes ping one of them. If DNS responds then do nothing, otherwise it would have to execute folowing commands "rm /etc/dhcpc/dhcpcd-eth0.pid && dhcpcd -d eth0" Would someone help me to write it? I try to learn bash, but for now I don't know how to write such script but need it ...
 
Old 08-02-2005, 04:03 PM   #2
rarsa
Member
 
Registered: Nov 2004
Location: Canada
Distribution: Puppy Linux/ Mint
Posts: 211

Rep: Reputation: 31
My recommendation?

Read a shell scripting tutorial like This one is very good. I followed it and I started writting scripts right away.

Start writting the script and send specific questions if you get stuck.

You will learn more and may even have fun in the process.

I also recommend as a general good development practice to start small and grow your script from there. Don't try to write all at once. Set a list of 'tasks' to implement and implement and test one by one, e.g.

1. Grep an address from resolve.conf

Write this part test it until it works. Once it's working go to the next item in the list.

2. Based on the grep result execute and echo command (the echo command is just a stub for the real code that will be added later)

Add this part, test your script until this works, then go to the next item in the list

3. Remove the pid files

Add this part, test your script

Etc...

This approach will allow you to learn the commands and play with them until you get the desired result.

Good luck.

(yes, yes, I know that this post was longer than writing the script, but I'm sure that will help you more)
 
Old 08-02-2005, 04:16 PM   #3
maxut
Senior Member
 
Registered: May 2003
Location: istanbul
Distribution: debian - redhat - others
Posts: 1,188

Rep: Reputation: 50
Code:
#!/bin/bash

# reads every nameserver ip addr. and assign them to NS variable in a while loop.
cat /etc/resolv.conf|grep -i "nameserver"|awk -F " " '{ print $2 }'|while read NS ; do

# it pings each nameserver four times (ping -c 4) and then checks if all of 4 packets turns back. 
if [ $(ping $NS -c 4 |grep "transmitted" | awk -F " " '{print $4}') -eq 4 ] ; then
echo "$NS: all of ping replies: ok"
else
echo "$NS: some or all of ping replies: failed"
fi
done
i hope it helps u.

good luck.


edit: if u have trouble with awk, try following:
awk -F "\ "
instead of
awk -F " "

Last edited by maxut; 08-02-2005 at 04:19 PM.
 
Old 08-02-2005, 04:25 PM   #4
lp449
Member
 
Registered: Oct 2004
Location: Olsztyn, Poland
Distribution: Debian Squeeze / CentOS 6
Posts: 60

Original Poster
Rep: Reputation: 15
Thank you all very much it helped me a lot
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple bash script Soulful93 Programming 1 06-04-2005 07:26 PM
Simple Bash Script Help njdownes Programming 2 03-05-2005 07:35 AM
Simple Bash Script dmedici Programming 9 12-31-2004 03:48 AM
Help with simple bash script - please tw001_tw Linux - Software 5 12-19-2004 09:02 PM
Simple BASH script fio Linux - General 5 01-18-2003 03:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 03:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration