Find the answer to your Linux question:
Results 1 to 3 of 3
OK, I need help with a part of my script I am trying to make. What I have to do is to write a script to ping a host, and ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    4

    Newb Need Help Bash Script

    OK, I need help with a part of my script I am trying to make. What I have to do is to write a script to ping a host, and then if the host it up connect to it using telnet.

    This is what I have so far:

    #! /bin/bash
    echo "Input an IP address to Ping"
    read IP
    echo "How many times do you want to Ping?"
    read Count
    echo "Pinging $IP $Count times"
    ping $IP -c $Count || { echo "Nobody Home Go Away" >&2; exit; }
    rup $IP || { echo "Host is Up" >&2; exit; }

    It works. It pings and lets the user know if the host is up or down. Now I am having trouble moving forward. If the host is up, I need to start the telnet part. So how do I say, if host is up, telnet? I basically need the bridge between the up host being pinged and starting the telnet part.

    Thanks for any help in advance.

    Joe

  2. #2
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Welcome to the forums!

    You will probably want to read the Advanced Bash Scripting Guide. Because an 'if' statement is indeed what you need here.

    Something like this can work:
    Code:
    ping -c 4 $IP > /dev/null && VAR="1" || VAR="0"
    if [ "$VAR" -eq "1" ] ; then
         echo "Starting Telnet session. Are you sure you don\'t want to \'vim $0\' "
         echo "and change this to a SSH session\?"
         echo "Something can be said for good security practises, y\'know."
         telnet --all-options --all-extras $IP
    else
         echo "Seems they are out"
         exit 1
    fi
    Oops, now I've written it for you. I hope it wasn't a homework question?
    Can't tell an OS by it's GUI

  3. #3
    Just Joined!
    Join Date
    Dec 2008
    Posts
    4

    Bash

    This is what I got now:

    #! /bin/bash
    echo "Input an IP address to Ping"
    read IP
    echo "How many times do you want to Ping?"
    read Count
    echo "Pinging $IP $Count times"
    if [ $? = "1" ] ; then
    echo "Remote Host is Down"
    else
    echo "host is up"
    fi

    It works, I put in telnet $IP before fi, it seemed to work but I'm running Ubuntu in Virtual box and I can seem to ping any ip but my own local. I can get on the interent fine and everything, but if I try to ping google or anyone one it won't work. It tried google in windows and it pings fine. I guess i'll try a live cd.

Posting Permissions

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