LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-27-2012, 01:18 AM   #1
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Linux Mint
Posts: 263

Rep: Reputation: Disabled
(solved)using variables outside of scope BASH(solved)


i am writing a bash script for some command line programs and i take in user input with the read command but after i leave the if scope the variable loses its value.

I need to use the variable in a different scope. How would i make this possible?

here is an example:
Code:
echo "Please choose a destination port number: "
read d_port

echo "Please choose the redirect port number: "
read to_port

echo "Configuring iptables. "
netstat -ant | grep $to_port > /dev/null 2>&1; result=$?
if [ $result == $0 ]; then
iptables -t nat -A PREROUTING -p tcp --destination-port $d_port -j REDIRECT --to-port $to_port

	else 
	  echo "to_port already in use. "
	  echo "Please choose another redirect port number: "
	  read to_port

	  iptables -t nat -A PREROUTING -p tcp --destination-port $d_port -j REDIRECT --to-port $to_port
fi
Then i try and use the variable again here:
Code:
read -p "Do you want to run sslstrip in the background? (y/n) " RESP
if [ "$RESP" = "y" ]; then
  echo "Running sslstrip in the background. "
  /bin/sh -c /pentest/web/sslstrip/sslstrip.py -a -l $to_port
else
  echo "Exiting"
  exit 0;
fi

Last edited by amboxer21; 03-28-2012 at 02:46 AM.
 
Old 03-27-2012, 04:39 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,195

Rep: Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796
An 'if' will not create a subshell so I would be guessing you are unsetting or not setting the value of to_port. A simple prrof is:
Code:
read -p "Enter to_port value: " to_port

if [[ $to_port == 3 ]]
then
    echo "Your to_port value is correct"
fi

echo "You entered a to_port value of $to_port"
Try pressing 3 and then run again and use a different number.
 
Old 03-27-2012, 04:42 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,516
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
The if [ $result == $0 ] test will never pass unless the name of your script is "0". Try if [[ $result -eq 0 ]]

Notes:
  1. [[ ... ]] is preferred over [ ... ] for reasons explained here.
  2. -eq is a numeric comparison operator. == is a string comparison operator.
 
Old 03-27-2012, 07:39 PM   #4
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Linux Mint
Posts: 263

Original Poster
Rep: Reputation: Disabled
Still no luck.

Here's what i have so far.
Code:
read -p "Do you want to run sslstrip in the background? (y/n) " RESP
if [ "$RESP" == "y" ]; then
sleep 1

read -p "Please choose the redirect port number: " to_port
echo $to_port
while true; do
netstat -ant | grep $to_port > /dev/null 2>&1; result=$?
echo "result = $result"
if [ $result -ne 0 ]; then

echo "Configuring iptables. "
iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-port $to_port
echo $to_port
  echo "Running sslstrip in the background. "
  echo $to_port
  bash -c /pentest/web/sslstrip/sslstrip.py -a -l $to_port &
break;
         else  
	  echo "$to_port already in use. "
	  read -p "Please choose another redirect port number: " to_port
	  echo "result = $result"
	  iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-port $to_port
fi
done
fi
No matter what, i cannot get the to_port variale to work.

Sorry if the code is a little sloppy and un-needed code is present. I have been at it for 2 days straight trying to figure this out.

Last edited by amboxer21; 03-27-2012 at 07:42 PM.
 
Old 03-27-2012, 08:02 PM   #5
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 3,986

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by amboxer21 View Post
i am writing a bash script for some command line programs and i take in user input with the read command but after i leave the if scope the variable loses its value.
That is quite normal behaviour for variables so I assume you mean something else.

I can't connect your two pieces of code. In the first you set to_port, and in the second one you want to use it. But what happens in between? Is it the same file?

Anyway, use the bash -x as stated in my signature. At least you know exactly where the vaiable loses its value and let's take it from there.

jlinkels
 
Old 03-27-2012, 08:32 PM   #6
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Linux Mint
Posts: 263

Original Poster
Rep: Reputation: Disabled
from what i see, everything seems to be fine. Though, sslstrip seems to be using the default port 10000 anyway, instead of the to_port variable i set.

Code:
+ read -p 'Do you want to run sslstrip in the background? (y/n) ' RESP
Do you want to run sslstrip in the background? (y/n) y
+ '[' y == y ']'
+ sleep 1
+ read -p 'Please choose the redirect port number: ' to_port
Please choose the redirect port number: 8080
+ echo 8080
8080
+ true
+ netstat -ant
+ grep 8080
+ result=1
+ echo 'result = 1'
result = 1
+ '[' 1 -ne 0 ']'
+ echo 'Configuring iptables. '
Configuring iptables. 
+ iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-port 8080
+ echo 8080
8080
+ echo 'Running sslstrip in the background. '
Running sslstrip in the background. 
+ echo 8080
8080
+ break
+ bash -c /pentest/web/sslstrip/sslstrip.py -a -l 8080
+ echo ' '
 
+ echo -n 'Displaying gateway ip_address. '
Displaying gateway ip_address. + echo ''

+ netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 wlan0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 wlan0
+ echo ' '
 
+ read -p 'What is your gateway ip? ' gateway
What is your gateway ip? 
sslstrip 0.8 by Moxie Marlinspike running...

Last edited by amboxer21; 03-27-2012 at 08:38 PM.
 
Old 03-27-2012, 10:25 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,308

Rep: Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768
Ah, it's because bash -c doesn't mean what you think it means:
Code:
~/tmp$ cat hello.py
#!/usr/bin/env python
import sys
print('len(args) = %d, args = %s' % (len(sys.argv), sys.argv))
~/tmp$ bash -c ./hello.py 1 2 3
len(args) = 1, args = ['./hello.py']
~/tmp$ bash -c './hello.py $0 $1 $2' 1 2 3
len(args) = 4, args = ['./hello.py', '1', '2', '3']
~/tmp$ bash -c './hello.py 1 2 3'
len(args) = 4, args = ['./hello.py', '1', '2', '3']
~/tmp$ python hello.py 1 2 3
len(args) = 4, args = ['hello.py', '1', '2', '3']
~/tmp$ chmod +x hello.py
~/tmp$ ./hello.py 1 2 3
len(args) = 4, args = ['./hello.py', '1', '2', '3']
Quote:
-c string
Read and execute commands from string after processing the options, then exit. Any remaining arguments are assigned to the positional parameters, starting with $0.
From Invoking Bash - Bash Reference Manual
 
1 members found this post helpful.
Old 03-27-2012, 10:38 PM   #8
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Linux Mint
Posts: 263

Original Poster
Rep: Reputation: Disabled
Sweet, thanks man. Got it.

im running sslstrip as a python script without the bash command. So ./sslstript.py -a -l $to_port

its up and running. I appreciate all the help!!

Last edited by amboxer21; 03-27-2012 at 10:57 PM.
 
Old 03-28-2012, 01:34 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,195

Rep: Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796
Don't forget to mark as SOLVED once you have a solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] (BASH) How to keep variable in scope? Dick Dastardly Programming 8 07-15-2011 12:51 PM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
Scope of shell script variables ? deepakc Linux - Newbie 4 07-07-2009 12:29 AM
[SOLVED] Scope of unnamed pipes in BASH script propofol Programming 13 06-17-2009 12:54 AM
Easy Bash Loop Scope Issue? scsi.king Programming 7 04-25-2007 05:41 AM


All times are GMT -5. The time now is 03:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration