where to start when writing a script


Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: where to start when writing a script

  1. #1
    Join Date
    Jan 2004
    Location
    boston, mass USA
    Posts
    1,878

    where to start when writing a script

    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

  2. #2
    Join Date
    Dec 2002
    Posts
    1,008
    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.

  3. #3
    Join Date
    Mar 2003
    Location
    Earth [I think...]
    Posts
    414
    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.

    Code:
    #!/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
    --------------------------------------------------
    My Website: http://www.suramya.com
    My Blog: http://www.suramya.com/blog
    Registered Linux User #: 309391
    --------------------------------------------------

    *************************************************
    Disclaimer:
    Any errors in spelling, tact, or fact are transmission errors.
    *************************************************

  4. #4
    Join Date
    Mar 2002
    Location
    Alton, Illinois, USA
    Posts
    1,126
    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.
    "You are not beaten until you admit it." --George S. Patton

    "Reading and learning, it's what linux users do"
    "Double clicking" doesn't work on Linux

    G4L | ZSH - The Z Shell

    Apple OS X, FreeBSD, and Smoothwall

  5. #5
    Join Date
    Jan 2004
    Location
    boston, mass USA
    Posts
    1,878
    Suramya

    that is now working great, but the

    cat index.html | sendmail....

    line isn't working since the email is blank.

    Any thoughts?

  6. #6
    Join Date
    Mar 2002
    Location
    Alton, Illinois, USA
    Posts
    1,126
    Code:
    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.
    "You are not beaten until you admit it." --George S. Patton

    "Reading and learning, it's what linux users do"
    "Double clicking" doesn't work on Linux

    G4L | ZSH - The Z Shell

    Apple OS X, FreeBSD, and Smoothwall

  7. #7
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    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"?

  8. #8
    Join Date
    Dec 1999
    Location
    Fargo, ND
    Posts
    1,816

    Re: where to start when writing a script

    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
    Knute

    You live, you die, enjoy the interval!

  9. #9
    Join Date
    Feb 2003
    Location
    Minnesota
    Posts
    268
    Suramya, you could get around the fact that you have to create old_ip.dat with
    Code:
    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.
    Linux has changed me...
    I'm a GNU man now!

  10. #10
    Join Date
    Jan 2004
    Location
    boston, mass USA
    Posts
    1,878
    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!

  11. #11
    Join Date
    Mar 2002
    Location
    Alton, Illinois, USA
    Posts
    1,126
    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.
    "You are not beaten until you admit it." --George S. Patton

    "Reading and learning, it's what linux users do"
    "Double clicking" doesn't work on Linux

    G4L | ZSH - The Z Shell

    Apple OS X, FreeBSD, and Smoothwall

  12. #12
    Join Date
    Mar 2003
    Location
    Earth [I think...]
    Posts
    414
    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:

    Code:
    #!/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
    --------------------------------------------------
    My Website: http://www.suramya.com
    My Blog: http://www.suramya.com/blog
    Registered Linux User #: 309391
    --------------------------------------------------

    *************************************************
    Disclaimer:
    Any errors in spelling, tact, or fact are transmission errors.
    *************************************************

  13. #13
    Join Date
    Jan 2004
    Location
    boston, mass USA
    Posts
    1,878
    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.

  14. #14
    Join Date
    Jan 2001
    Location
    Worcester, MA
    Posts
    722
    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
    Code:
    #!/bin/bash
    mail -s "Im the subjuect" someone@somedomain.com < the_file

    I do it with this script
    Code:
    #!/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
    Last edited by goon12; 09-14-2004 at 09:53 AM.

  15. #15
    Join Date
    Jan 2004
    Location
    boston, mass USA
    Posts
    1,878
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •