Script that will random choose an IP address


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script that will random choose an IP address
# 1  
Old 02-02-2016
Script that will random choose an IP address

Hi,

I need to write a bash script that will random choose and login into these below ip addresses.
Code:
         192.168.116.130
         192.168.116.131
         192.168.116.132
         192.168.116.133

I'm new into scripting and I need to enhance my logic. Below is what i did

Code:
#!/bin/bash
p=$1
localip=$2
port=$3
localport=$4


if [ "$#" -ne 4 ]; then
    exit 1
fi


case "$2" in


	1)  return_string="Milon1, 192.168.116.130, 22"
		echo "$return_string"
		;;
	
	2)  return_string="Milon2, 192.168.116.131, 22"
		echo "$return_string"
		;;
	
	3)  return_string="Milon3, 192.168.116.132, 22"
		echo "$return_string"
		;;
	
	*) return_string="Milon4, 192.168.116.133, 22"
	   echo "$return_string"
	   ;;
esac


Last edited by Don Cragun; 02-03-2016 at 06:43 AM.. Reason: Add CODE tags.
# 2  
Old 02-02-2016
Please use code tags as required by forum rules!

So - the random value would be supplied as $2 between 1 and 4?

---------- Post updated at 20:36 ---------- Previous update was at 19:05 ----------

Just some thoughts as a starting point:
Code:
IP=(192.168.116.13{0..3})
for ((i=1; i<=8; i++)); do R=$((RANDOM%4)); echo "Milon$R, ${IP[$R]}, 22"; done
Milon2, 192.168.116.132, 22
Milon3, 192.168.116.133, 22
Milon2, 192.168.116.132, 22
Milon0, 192.168.116.130, 22
Milon1, 192.168.116.131, 22
Milon1, 192.168.116.131, 22
Milon3, 192.168.116.133, 22
Milon1, 192.168.116.131, 22

# 3  
Old 02-02-2016
The problem is I'm very new in bash script.

Will the below script produce the below results?


Code:
#!/bin/bash
p=$1
localip=$2
port=$3
localport=$4

if [ "$#" -ne 4 ]; then
    exit 1
fi

IP=(192.168.116.13{0..3})
for ((i=1; i<=8; i++)); do R=$((RANDOM%4)); echo "Milon$R, ${IP[$R]}, 22"; done




Results:

Code:
Milon2, 192.168.116.132, 22
Milon3, 192.168.116.133, 22
Milon2, 192.168.116.132, 22
Milon0, 192.168.116.130, 22
Milon1, 192.168.116.131, 22
Milon1, 192.168.116.131, 22
Milon3, 192.168.116.133, 22
Milon1, 192.168.116.131, 22


Last edited by Don Cragun; 02-03-2016 at 06:44 AM.. Reason: Add CODE tags.
# 4  
Old 02-03-2016
Why don't you run it so you can see?

And, I guess (as you don't explicitly explain) that you want script parameters to hold IP addresses and ports, and that the "22" is a port (indicating you want to login via ssh). But, again, there's no indication about what is what.

Why don't you step back and write a detailed specification?
# 5  
Old 02-03-2016
I want the system to randomly pick a honeypot sensor for a user to login.

Code:
ip=$1
localip=$2
port=$3
localport=$4
if [ "$#" -ne 4 ]; then
    $username = $5
    $password = $6
fi

IP=(192.168.116.13{0..3})
for ((i=1; i<=8; i++)); do R=$((RANDOM%4)); echo "Milon$R, ${IP[$R]}, 22"; done

return_string="Sensor_Name, Honey_IP, Honey_Port"

if [ "$#" -ne 4 ]; then
    return_string="$return_string, $username, $password"
fi

echo "$return_string"


Last edited by Milon; 02-03-2016 at 10:53 AM.. Reason: mistake
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Random script error with rndc

Hello coders, been running into an strange behavior into one of my script and i'm wondering if my code wouldn't be responsible. Bash on rhel 5.7 This is a basic check to see if bind is up and running on server. # rndc check INFO="Checking rndc" for DNS_SERVER in ${DNS_MASTER_SERVERS};... (10 Replies)
Discussion started by: maverick72
10 Replies

2. Shell Programming and Scripting

Random number generating script?

Having a hard time with this. Very new to scripting and linux. Spent all sunday trying to do this. Appreciate some help and maybe help breaking down what the syntax does. Create a Bash program. It should have the following properties • Creates a secret number between 1 and 100 i. The... (3 Replies)
Discussion started by: LINUXnoob15
3 Replies

3. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

4. Shell Programming and Scripting

Generate random numbers in script

i want to generate a random number through a script, and even if anyone reads the script, they wont be able to figure out what the random number is. only the person who setup the script would know it. something like this could work: random the full thread is here: ... (13 Replies)
Discussion started by: SkySmart
13 Replies

5. Shell Programming and Scripting

how to choose random columns

Hello! Can anybody suggest about the fastest way of extracting "n" random columns from a very large file (tab separated) having thousands of columns, where n can be any specified number. Thanks! (10 Replies)
Discussion started by: mira
10 Replies

6. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

7. Shell Programming and Scripting

choose random text between constant string.. using awk?

Hallo I have maybe a little bit advanced request.... I need to choose one random part betwen %.... so i have this.. % text1 text1 text1 text1 text1 text1 text1 text1 text1 % text2 text2 text2 text2 text2 % text3 text3 text3 tetx3 % this choose text between % awk ' /%/... (8 Replies)
Discussion started by: sandwich
8 Replies

8. Shell Programming and Scripting

random script termination

I'm writing a script to archive data. First, the files are all rsync'd to the archive directory via NFS mounts(I know not the most efficient, but the only choice in this situation), then I use md5sum to validate the transfers. During execution of the script, it will exit for no apparent reason. It... (6 Replies)
Discussion started by: mph
6 Replies

9. Shell Programming and Scripting

random function script

hi, I need a command which picks the records randomly from the file. For example. i am having some 10000 entries in a file and need to extract the lines randomly without repeating the numbers. Do anybody have any idea on how to get this out. (4 Replies)
Discussion started by: prash_b
4 Replies

10. Programming

random ip address

suppose that my network client process does not possess an effective root UID. is it possible to change the client host machine ip address to a random number? (3 Replies)
Discussion started by: xtrix
3 Replies
Login or Register to Ask a Question