Reputation: 89
It's kind of hard to explain by situation. I'll try the best I can.
I'm making a program to make using driftnet easier for new users.
You first enter the gateway IP and then the target IP, the your interface. In the program, once you enter all those, it opens a new terminal window, you then start the second program which flips the original order of the IPs. I could have it for the user to manually switch it, but I want it to just automatically switch it. To do that I have to use global
to keep the input information, so it will just switch it. The problem is when I run the second program it just starts the first one all over again.
#This it the first program
#"Driftnet.py"
import os
import time
from subprocess import call
def drift():
global gateway
gateway = raw_input("Gateway IP > ")
time.sleep(0.5)
global target
target = raw_input("Target IP > ")
time.sleep(0.5)
global inter
inter = raw_input("Interface > ")
drift()
call(["gnome-terminal"])
os.system("arpspoof -i " + inter + " -t " + gateway + " " + target)
I run that, input everything, then it opens the second terminal, and I run the second program where it switches the IPs.
#This is the second program
#"Driftnet2.py"
import os
import time
from subprocess import call
import Driftnet
os.system("arpspoof -i " + Driftnet.inter + " -t " + Driftnet.target + " " + Driftnet.gateway)
When I run that it pretty much just runs the frist program, starting with the question of "Gateway IP > "
I have utterly no clue what I'm doing wrong.
Thanks
Upvotes: 2
Views: 112
Reputation: 43166
This is what the if __name__=='__main__':
construct exists for. Change the code in Driftnet.py to this:
import os
import time
from subprocess import call
def drift():
global gateway
gateway = raw_input("Gateway IP > ")
time.sleep(0.5)
global target
target = raw_input("Target IP > ")
time.sleep(0.5)
global inter
inter = raw_input("Interface > ")
if __name__=='__main__':
drift()
call(["gnome-terminal"])
os.system("arpspoof -i " + inter + " -t " + gateway + " " + target)
It's possible that you also have modify Driftnet2.py a little. I'm not sure if you'll be able to access the global variables by doing import Driftnet
. If you're getting errors, try import __main__ as Driftnet
instead.
Upvotes: 0
Reputation: 941
I don't know what "Driftnet" is, and I'm not sure exactly what you're trying to do, but I can explain the behavior you're seeing.
In Driftnet2.py, you call:
import Driftnet
which causes the Python code in Driftnet.py to be evaluated. That is what the import
statement does. All of your code is top-level (except the drift()
method, which is called from the top-level), so importing it runs it.
You have only one method, and no top-level variables, so the global
declarations are useless. (It almost seems like you think the global
keyword is for IPC, but I'm not sure.)
To design this program, you need to first step back and answer some basic questions. Like, if you want to transfer information from Driftnet.py in one process to Driftnet2.py in another process, how is that transfer going to occur? (Command-line flags? Environmental variables? Unix domain sockets?) Once you know what you want your program to do, we can help you implement it.
Upvotes: 1
Reputation: 315
Have you tried adding the global variable after import etc...then calling the var inside functions as global?
import os
import time
from subprocess import call
gateway = ''
target = ''
inter = ''
Upvotes: 0