Total Noob BASH scripting question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Total Noob BASH scripting question
# 1  
Old 04-04-2018
Total Noob BASH scripting question

Hello All,

I have a file of ip addresses called activeips.txt

What I'm trying to do is run a simple bash script that has a loop in it. The loop is a cat of the IP addresses in the file.

The goal is to run 2 nmap commands to give me outputs where each address in the list has an OS fingerprint and then a top 20 open ports scan.

The problem is that the script as written, not matter what I do, will run the nmap commands at the same time and they cycle through without waiting for each other. They literally run through the whole list of IPs independent of each other.

What I want to do is get the first address in the file, run the first nmap command, then the second nmap command and then go to the next address, run the first nmap, second nmap, next address....so on and so on.

All help is appreciated for a scripting NOOB

Thanks

DP
Here's the script:

Code:
#!/bin/bash

DISCOVERY (){
nmap -v -p 139,445 --script=smb-os-discovery $address |grep -v "smb-os-discovery" | grep -v "OS CPE" | grep -v "Computer name" | grep -v "NetBI$
}
PORTS (){
nmap -sT --top-ports 20 $address
}

for address in $(cat activeips.txt);do
DISCOVERY
PORTS
done


Last edited by vgersh99; 04-04-2018 at 05:35 PM.. Reason: code tags, please!
# 2  
Old 04-04-2018
what you had should do what you want - 2 calls to nmap one after the other.
Here's a slightly modified version:
Code:
#!/bin/bash

DISCOVERY ()
{
   addr="${1}"
   nmap -v -p 139,445 --script=smb-os-discovery "${addr}" |grep -v 'smb-os-discovery' | grep -v 'OS CPE' | grep -v 'Computer name' | grep -v 'NetBI'
}
PORTS ()
{
  addr="${1}"
  nmap -sT --top-ports 20 "${addr}"
}

while read address junk
do
   DISCOVERY "${address}"
   PORTS "${address}"
done < activeips.txt

# 3  
Old 04-04-2018
OK...I see where you're going.

The activeips.txt file already exists....from another script. It has the list of IPs for the nmaps to scan....so outputting to activeips.txt like you have as the last line is no quite correct

So using what you have above...would the loop look like this?

Code:
for address in $(cat activeips.txt);
do
   DISCOVERY "${address}"
   PORTS "${address}"
done


Last edited by vgersh99; 04-04-2018 at 06:07 PM.. Reason: start using code tags, please!
# 4  
Old 04-04-2018
Code:
so outputting to activeips.txt like you have as the last line is no quite correct

I'm not outputting to - I'm reading from.

you don't need cat activeips.txt- test I've provided
# 5  
Old 04-04-2018
Ahhh ... I see what you're saying with the active ips file...I didn't pay attention to the < for input instead of output.

So I try it, and I get output of the discovery nmapping the first IP, then I get ports output of the first ip, and then it outputs the ports again for the next ip in the file, then it outputs the ports again for the next ip in the file, and then eventually the Discovery output comes out for the 2nd ip in the file. It's like the 2 functions are running independent of each other. The goal is Discovery for first IP, then Ports for First IP, then Discovery for 2nd, Ports for second...and so on.

The script you have is doing the same thing my original one is...where the 2 functions seem to run loops of their own without regard to each other
# 6  
Old 04-04-2018
that's hard to believe...
could you post the content of activeips.txt using code tags.
# 7  
Old 04-04-2018
it's a simple txt tile with these lines

Code:
10.0.0.13
10.0.0.16
10.0.0.22
10.0.0.17
10.0.0.19
10.0.0.20
10.0.0.12
10.0.0.11
10.0.0.15
10.0.0.24
10.0.0.63
10.0.0.62
10.0.0.80
10.0.0.101
10.0.0.103
10.0.0.109
10.0.0.112
10.0.0.117
10.0.0.110


Last edited by vgersh99; 04-04-2018 at 06:44 PM.. Reason: once again - code tags!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Noob question: How to check the total number of inputs entered by user?

Say I have this line: read -p "Enter 3 numbers: " num1 num2 num3; I want to write a while loop that repeatedly asks for input if the number of inputs entered is not equal to 3. I don't know the correct command to find the number of inputs entered. Help, please? (4 Replies)
Discussion started by: jejemonx
4 Replies

2. Shell Programming and Scripting

Noob to scripting needs some assistance

Hello, I am in a Unix class and have been out of town. I have been tasked to generate a couple of scripts and ahve never done it before. I have a virtual machine running Ubuntu. The task is below Prompt the system administrator for all valid input parameters Generate a menu to ask which... (1 Reply)
Discussion started by: jkeeton81
1 Replies

3. Shell Programming and Scripting

Beginner Bash Scripting Question

Hello, I am new to Linux and studying to become a Unix System Admin. I am taking a course in which I was practicing creating a bash script to ping a particular IP address. The script can be found below: #/bin/bash echo "Enter the IP address" read ip if then ping -c 1 $ip if ;... (3 Replies)
Discussion started by: shah9250
3 Replies

4. Shell Programming and Scripting

Noob Expect Scripting Question

I'm having some difficulty with convincing Expect to do what I need.. I have a loop that waits for input, a specific phrase of text followed by a single word. I need Expect to capture that word following the specific phrase. It should then store the word in a variable. I'm fairly sure it's... (6 Replies)
Discussion started by: LongLeafTea
6 Replies

5. Shell Programming and Scripting

Bash scripting question

have a script that calls child scripts depending on conditions. All of the child scripts source in a common file that contains shared functions. At the moment each script has to source this file itself, is there a way for the master script to automagically source the file for them? For... (3 Replies)
Discussion started by: redman
3 Replies

6. Shell Programming and Scripting

Noob to Shell Scripting

Hello. I'm attempting to create a shell script to assist me by saving time with one of my hobbies. I am an Android Enthusiast and currently build a few roms for one of the devices. One of the roms I make is ported from a different device to mine (I get the original for the HTC Desire HD and... (3 Replies)
Discussion started by: JHutson456
3 Replies

7. UNIX for Dummies Questions & Answers

Noob scripting question with android ADB commands

Hi I'm pretty new to scripting and I've been googling around looking for an answer but have yet to come up with a proper solution. I work with multiple android devices at a time and I'm looking to simplify my life with a script. Basically I'm looking for a script that takes the device ID's and then... (2 Replies)
Discussion started by: Onyoursix
2 Replies

8. Shell Programming and Scripting

Total noob script

Hi i am a total noob at shell scripting. i was wondering if somebody could help me with my script. i want the script to search the dev folder for the burner file because they are different between distrubutions? as i under stand it. this i the script. #!/bin/bash echo "Script för att bränna 360... (4 Replies)
Discussion started by: MatsO
4 Replies

9. Shell Programming and Scripting

Bash scripting, question about a variable

Hey, So I've run into a problem, due to my limited knowledge of Bash scripting. Basically I've got a long script and I want to understand it before I even try and edit it. As long as I don't understand the script, I will not bother editing it. Anyway, the following variable confuses me... (5 Replies)
Discussion started by: abciscool
5 Replies

10. Shell Programming and Scripting

Bash scripting question re: newlines

I'm writing a bash script to search the contents of a postfix log. To keep the script's output readable (since multiple lines from the log file need to be echo'ed) I am setting the IFS variable to an empty string so that the line breaks in my grep results are preserved. I am storing the results... (4 Replies)
Discussion started by: retrovertigo
4 Replies
Login or Register to Ask a Question