User Controls
Passing options/args and class questions. (Python)
-
2015-10-15 at 9:42 PM UTCSo i got a script and from within that script i want to import another script and execute a class from it. I never particularly tried this before so i'm probably doing it wrong. In any event. In the first script you can pass some arguments one of which is to scan, if that option is chosen i want the scanner module to be imported and executed but it's not doing what i want, i don't even think it's recognizing the option to scan properly. Here is the relevant snippet of code:
if not len(sys.argv[1:]):
usage()
# Read commandline options
try:
opts, args = getopt.getopt(sys.argv[1:],"hle:t:p:cu",["help","scan","listen","execute","target","port","command","upload"])
except getopt.GetoptError as err:
print str(err)
usage()
for o,a in opts:
if o in ("-h","--help"):
usage()
elif a in ("-s", "--scan"):
import scanner
scanner()
sys.exit()
elif o in ("-1", "--listen"):
listen = True
elif o in ("-e", "--execute"):
execute = a
elif o in ("-c", "--commandshell"):
command = True
elif o in ("-u", "--upload"):
upload_destination = a
elif o in ("-p", "--port"):
port = int(a)
else:
assert False,"Unhandled Option"
Any idea on what's going wrong? -
2015-10-16 at 6:12 PM UTCYou can't call modules, you'll need to call a function in that module. You could create a function in "scanner.py" called "scan" and do "import scanner; scanner.scan()" or you could just import the function from scanner and call it with "from scanner import scan; scan()".
Importing a module executes any code in it that isn't in a function or class, so if "scanner.py" is like that and you just want to execute it, just importing it should do the trick. Just remove the "scanner()" line.
Edit: Somehow I missed the part where you said you wanted to import a class. Importing a class is just like importing a function: "import module; object=module.Class(); object.doSomething()" or "from module import Class; object=Class(); object.doSomething()". -
2015-10-16 at 10:05 PM UTC
You can't call modules, you'll need to call a function in that module. You could create a function in "scanner.py" called "scan" and do "import scanner; scanner.scan()" or you could just import the function from scanner and call it with "from scanner import scan; scan()".
Importing a module executes any code in it that isn't in a function or class, so if "scanner.py" is like that and you just want to execute it, just importing it should do the trick. Just remove the "scanner()" line.
Edit: Somehow I missed the part where you said you wanted to import a class. Importing a class is just like importing a function: "import module; object=module.Class(); object.doSomething()" or "from module import Class; object=Class(); object.doSomething()".
Alright, so where you type object.doSomething() i should put the name of the method from the class i want to execute right? So if i have a method called send_packets i should type object.send_packets() or not? -
2015-10-16 at 10:45 PM UTCOh by the way, my scanning option is not working. If i type:
mainprogram.py -s or --scan it doesn't import and execute the module. When i tried importing it from another script it did execute so my arguments aren't being passed to the script for some reason. -
2015-10-17 at 1:28 AM UTC
Alright, so where you type object.doSomething() i should put the name of the method from the class i want to execute right? So if i have a method called send_packets i should type object.send_packets() or not?
Yeah, that's right. And maybe your scan option doesn't work because you wrote "elif a in ..." instead of "elif o in ..." like all the other ones? -
2015-10-17 at 1:17 PM UTC
Yeah, that's right. And maybe your scan option doesn't work because you wrote "elif a in …" instead of "elif o in …" like all the other ones?
Changed it up still nothing but i solved it by having the scanning execute if no arguments are passed so you start the script. It scans the network and the you CTRL+C to exit scanning mode and display the options for interacting with the host.
Pic related:
-
2015-10-17 at 1:33 PM UTC