Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
I want to create a shell script that will run nmap on my computer every minute or so. Then, I want it to take the results, and compare them to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Posts
    33

    NMAP Automated Script


    I want to create a shell script that will run nmap on my computer every minute or so. Then, I want it to take the results, and compare them to the previous one. If they are different, I want it to pop-up a window showing open and ports. Also, every hour, I want it to pop-upa window anyway, showing me the open ports.

    Is there a way to get this script to:
    a. Begin running on startup (I have mdk 10)
    b. Run in the background, and then open a terminal window, showing the nmap scan results.

    Thanks!

  2. #2
    Linux Engineer
    Join Date
    Apr 2005
    Location
    Buenos Aires, Argentina
    Posts
    908
    ...

    What makes you think that someone will do an script that does all that and post it for you? I mean, you're not asking for something THAT simple.. I think you should go to read some bash scripting stuff and try to do it yourself.

    Also, if you can code C as your nick tells me, you can probably do some bash scripting as well.

    Good luck.
    serzsite.com.ar
    "All the drugs in this world won\'t save you from yourself"

  3. #3
    Just Joined!
    Join Date
    Apr 2005
    Posts
    33
    I only switched to linux a few months ago. C i know, bash im still working. Thanks anyway ,though

  4. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Quote Originally Posted by serz
    I mean, you're not asking for something THAT simple..
    I'd say it actually is THAT simple.
    Personally, I would put the script in cron, and make it do something like this:
    Code:
    #!/bin/sh
    
    if [ -e /tmp/nmap-out ]; then
        # Use the output file as mutex
        echo already running >&2
        exit 1
    fi
    
    nmap hostname >/tmp/nmap-out 2>&1
    
    if [ -e ~/.nmap-old ] && diff ~/.nmap-old /tmp/nmap-out >/dev/null; then
        diff -u ~/.nmap-old /tmp/nmap-out >/tmp/nmap-diff
        zenity --info --title=nmap --text="$(cat /tmp/nmap-diff)"
        rm -f /tmp/nmap-diff
    fi
    
    mv -f /tmp/nmap-out ~/.nmap-old
    The problem is, of course, just how you want to display it. Here, I used zenity, but countless other solutions are, of course, possible.

    As for displaying the entire nmap output every hour or so, I'd just make a second script for that and put it, too, into cron, just with another interval. Of course, that one would be even simpler:
    Code:
    #!/bin/sh
    
    nmap hostname >/tmp/nmap2-out 2>&1
    zenity --info --title=nmap --text="$(cat /tmp/nmap2-out)"
    rm -f /tmp/nmap2-out
    As for putting stuff into cron, see the manpages for crontab(1) and crontab(5).

  5. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Your first script does not seem to erase /tmp/nmap-out

    This thread made me try out a little experiment earlier:
    I used an at command 1 minute in the future where I put just kedit
    Then I continued doing other things on a different window.
    kedit was never opened ! Does zenity have something different
    which will allow it to open a window ? I don't have zenity so I can't
    experiment with it.

  6. #6
    Just Joined!
    Join Date
    Apr 2005
    Posts
    33
    My idea was to have it display what it would display if I were to open a terminal window, and type: (excluding the prompt)
    Code:
    [admin @localhost admin]$ nmap -sV 192.168.1.103
    In other words, show a terminal window with that command typed in, plus results.

  7. #7
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Quote Originally Posted by Santa's little helper
    Your first script does not seem to erase /tmp/nmap-out
    Oops, typo. I had written "mv -f /tmp/nmap-diff ~/.nmap-old" where, in fact, I meant to write "mv -f /tmp/nmap-out ~/.nmap-old". I've edited the previous post to correct my mistake.

    I hadn't tested the script or anything. I meant it more like a sample for starters than a final solution.

    Quote Originally Posted by Santa's little helper
    This thread made me try out a little experiment earlier:
    I used an at command 1 minute in the future where I put just kedit
    Then I continued doing other things on a different window.
    kedit was never opened ! Does zenity have something different
    which will allow it to open a window ? I don't have zenity so I can't
    experiment with it.
    Maybe at unsets the DISPLAY variable? In any case, it is very clear that cron will unset DISPLAY, so that also needs correcting in my previous sample. I won't correct it, though, since I cannot figure out what setting of DISPLAY you will want. Maybe it's an xauth problem? You may want to redirect stdout and stderr of kedit to a tempfile to diagnose the problem.

  8. #8
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Quote Originally Posted by I_am_the_C_coder_FEAR_ME
    My idea was to have it display what it would display if I were to open a terminal window, and type: (excluding the prompt)
    Code:
    [admin @localhost admin]$ nmap -sV 192.168.1.103
    In other words, show a terminal window with that command typed in, plus results.
    Well, if you so much want to have the command displayed, just "echo" it:
    Code:
    echo '[admin @localhost admin]$ nmap -sV 192.168.1.103'
    As for using a terminal window, that may not be a perfect solution, since it will close again as soon as the program running inside it has finished. Of course, you can fix that with a "read" command or similar to pause it. As I mentioned in my previous post, that script I gave as an example is meant as a sample for starters rather than a complete and final solution, so I leave that up to you.

  9. #9
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Quote Originally Posted by Dolda2000
    Maybe at unsets the DISPLAY variable? In any case, it is very clear that cron will unset DISPLAY, so that also needs correcting in my previous sample. I won't correct it, though, since I cannot figure out what setting of DISPLAY you will want. Maybe it's an xauth problem? You may want to redirect stdout and stderr of kedit to a tempfile to diagnose the problem.
    The error message is kedit: cannot connect to X server
    I also tried a two lines script
    Code:
    at> setenv DISPLAY :0
    at> kedit >& /tmp/err
    but I got the same error.

  10. #10
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I don't know much (if anything at all) about KDE and QT. Could it be that it prints "cannot connect to X server" for multiple condititions, such as no DISPLAY variable, or wrong X authentication data? You may want to check if your display manager sets an alternative XAUTHORITY location or anything like that.

    Alternatively, try with xlogo or some other Xlib program instead of kedit. That just might give a more accurate error message.

Page 1 of 2 1 2 LastLast

Posting Permissions

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