Click to See Complete Forum and Search --> : where to start when writing a script
happybunny
09-02-2004, 02:52 PM
greetings one and all.
I am not a programmer. I don't "get" programming.
I have tried VB and some windows scripting, but still can't wrap my brain around it.
However, i have a need for a program and thought I would try my hand at it once again.
I want a program to check what my internet IP is currently and then email me what it is on a scheduled basis in case it changes.
I am not looking for anyone to write this for me, but i am looking for advise on even where to start.
Can this be done with perl? python? bash scripting? If I knew all 3, how would I know which one would be best to use? do they all have strengths?
So to all you programmers, how would you tackle this?
Again, I am not asking for simple code, but a starting point.
Thanks in advance
Happy
madcompnerd
09-02-2004, 04:30 PM
Bash.
If you have your mail system setup right you can it with:
ifconfig
sed
mail
At least I think you can :).
First thing, is your computer IP on an internal network, or are you looking for the outside ip? That does change things up a bit, but it's still pretty easy.
Suramya
09-02-2004, 04:38 PM
You can do it with any of the above scripting lang's. Here's a script I had written in Bash a while ago which does exactly what you are looking for. Feel free to go ahead and use it anyway you feel like. Let me know if you have problems.
#!/bin/bash
cd /home/user/myip
wget whatismyip.org
read T1 <index.html
read T2 <old_ip.dat
if [ "$T1" = "$T2" ]; then
echo "IP Is the same doing nothing";
else
cat index.html | sendEmail -f you@domain.com -t destination@domain.com
fi
rm old_ip.dat
mv index.html old_ip.dat
Before you run it for the first time you have to create a file called old_ip.dat with the current IP. then onwards the script runs on its own.
Hope you find this usefull.
- Suramya
fatTrav
09-02-2004, 06:11 PM
i already have a script to do this. i did it in bash. if you want it, i can post it. i use lynx, sendmail, and awk. it's an hourly cron job.
happybunny
09-10-2004, 03:31 PM
Suramya
that is now working great, but the
cat index.html | sendmail....
line isn't working since the email is blank.
Any thoughts?
fatTrav
09-10-2004, 03:49 PM
SENDMAIL=/usr/sbin/sendmail
REC="travis@indexoutofbounds.com"
# the from doesn't matter...
FROM="travis@grendel.org"
$SENDMAIL -f $FROM $REC << EOF
Subject: IP Update
Our IP is: `lynx -dump http://checkip.dyndns.org/ | awk --posix '{ print $4 }'`.
EOF
exit 0;
}
IP=`lynx -dump http://checkip.dyndns.org/ | awk --posix '{ print $4 }'`
OLD=`cat /tmp/ourip`
if [ $IP != $OLD ]; then
rm /etc/cron.hourly/ourip
echo $IP >> /tmp/ourip
email
fi
that's what I use for an hourly cron job. might be messy, but it gets the job done.
bwkaz
09-10-2004, 07:27 PM
Originally posted by happybunny
that is now working great, but the
cat index.html | sendmail....
line isn't working since the email is blank.
Any thoughts? Anything in the Sendmail logs? Is Sendmail configured properly, so that you can use it to send emails from a shell process to destination@domain.com?
Does it work to just "echo test | sendmail -f whatever -t whatever"?
knute
09-10-2004, 07:54 PM
Originally posted by happybunny
greetings one and all.
I am not a programmer. I don't "get" programming.
I have tried VB and some windows scripting, but still can't wrap my brain around it.
However, i have a need for a program and thought I would try my hand at it once again.
I want a program to check what my internet IP is currently and then email me what it is on a scheduled basis in case it changes.
I am not looking for anyone to write this for me, but i am looking for advise on even where to start.
Can this be done with perl? python? bash scripting? If I knew all 3, how would I know which one would be best to use? do they all have strengths?
So to all you programmers, how would you tackle this?
Again, I am not asking for simple code, but a starting point.
Thanks in advance
Happy
Since you want to learn where to start when you are programming something, I'll tell you.
From the looks of it, some here may need a refresher course.
Step's to writing a program
1. Get a pencil and paper
2. Define the problem
3. Break the problem into the steps involved to solve the problem in the order that they need to be done.
4. Break the steps down even further into their components
5. If you haven't already done so, get a pencil and paper and write down the steps necessary in order to complete the project, including conditionals and loops etc, in pseudo code.
6. With the pseudo code that you have created, choose a language to write it in.
7. Write the program -- be sure to include comments on each section so that you know what it does later.
8. Enjoy!
HTH
freakmn
09-10-2004, 08:04 PM
Suramya, you could get around the fact that you have to create old_ip.dat with touch old_ip.dat at the beginning of the script. It wouldn't have the current ip, but it should send the mail the first time, then store the real ip.
happybunny
09-10-2004, 09:13 PM
The script works fine and the email gets sent out, but the cat index doesn't end up in the body of the email.
It is not the script, however, since it does not work "manually" either.
I will continue my efforts trouble shooting sendmail (or some other email client). I have found several docs on this.
I am on my way to getting this solved....thanks to all!
And thanks knute for your input into the original question!
fatTrav
09-10-2004, 10:03 PM
I could never get " cat whatever | " to work with send mail. that is why I made the email() function. So I just circumvented it by making sendmail wait for userinput (the <<EOF), putting in the information, and then telling it when user input was done (EOF).
"I want a program to check what my internet IP is currently and then email me what it is on a scheduled basis in case it changes."
That is exactly what my script does.
Suramya
09-14-2004, 12:44 AM
For some reason I never got an email for any of the replies to this post...
NEway's
freakmn: Thanks for the tip. That fixes some problems.
The script I posted earlier suddenly stopped emailing (I was getting wierd bounceback emails) so I rewrote part of the script:
#!/bin/bash
touch old_ip.dat
wget whatismyip.org
read T1 <index.html
read T2 <old_ip.dat
if [ "$T1" = "$T2" ]; then
echo "IP Is the same doing nothing";
else
(echo "From: ipchanged@yourdomain.com"; echo "To: youraddress@yourdomain.com"; \
echo "Subject: IP Address Update"; echo; echo "Computer's IP Changed. The new IP is:"; \
cat index.html) | sendmail -f ipchanged@yourdomain.com youraddress@yourdomain.com
fi
rm old_ip.dat
mv index.html old_ip.dat
This works for me without any problems (So Far). If you have any questions feel free to ask.
- Suramya
happybunny
09-14-2004, 08:50 AM
I could not get the script to put any info into the body of the message, or attach a file, so i just changed the "from" portion to $T1 and the email it sends comes from the new IP address.
Ill try your changes tonight.
goon12
09-14-2004, 09:50 AM
Suramya
that is now working great, but the
cat index.html | sendmail....
line isn't working since the email is blank.
Any thoughts?
I might be way off but if you want to email the body of a file to yourself ( as the body ), I think you can just do something like
#!/bin/bash
mail -s "Im the subjuect" someone@somedomain.com < the_file
I do it with this script
#!/bin/bash
filename=$1
# Get recipient
echo -n "Who do you wish to send the file to: "
read em_recip
if [ -z $filename ]; then
echo -n "What file do you want to send: "
read filename
fi
# Send the file
mail -s "Sending file: $filename" $em_recip < $filename
-goon12
happybunny
11-03-2004, 01:36 PM
Well, I have finally "perfected" it and thought i'd add it here:
I would prefer to have only the lines that are different emailed to my, but
but this will do. If i get that to work, ill post it here, too.
#!/bin/bash
# Attempt 2 to get dns lookups to work.
# Author: HappyBunny
# Nov 2004
#
# dig will lookup dns info on dns.serverx.org
dig www.domain.org @dns.server1.org |grep www.domain.org > test.txt
dig www.domain.org @dns.server2.org |grep www.domain.org >> test.txt
dig www.domain.org @dns.server3.org |grep www.domain.org >> test.txt
dig www.domain.org @dns.server4.org |grep www.domain.org >> test.txt
dig www.domain.org @dns.server5.org |grep www.domain.org >> test.txt
# Now, compare the test.txt file to the last test run
# and email just the differences to the email list
if [ "$(cat test.txt)" != "$(cat good.txt)" ];then
echo "This is the current results:"> diff.txt;
cat test.txt >> diff.txt;
echo "" >> diff.txt;
echo "This is what this run reports:" >> diff.txt;
cat good.txt >> diff.txt;
mail happybunny@domain.org -s "DNS Info - below are any changes to dns from the last run" < diff.txt
fi
# Now clean up....make the good.txt file current to the latest dig results
rm good.txt && cat test.txt > good.txt
blobaugh
11-03-2004, 01:51 PM
start writing the script on the first line;)
Suramya
11-04-2004, 12:46 AM
Originally posted by blobaugh
start writing the script on the first line;)
Huh? :confused:
blobaugh
11-05-2004, 06:02 AM
line 1, that would be the first line
bwkaz
11-05-2004, 07:32 PM
I think he's answering the question in the subject line of this thread -- "where to start when writing a script?" -- "at the beginning".
;)
Suramya
11-05-2004, 11:05 PM
Originally posted by bwkaz
I think he's answering the question in the subject line of this thread -- "where to start when writing a script?" -- "at the beginning".
;)
Oh... I guess I was esp slow this time... :o