Login Register






Need Help Creating My First Script filter_list
Author
Message
Need Help Creating My First Script #1
Hi all.

Im trying to learn the very basics of coding. Ive decided moving forward to learn python as my base then go from there.
I want to create a simple script for my Kali install that will boost the Tx for the alpha radio and also change the macaddress randomly..

currently i have to type each command 1 at a time..
iwconfig wlan0
ifconfig wlan0 down
iw reg set NZ
ifconfig wlan0 up
iwconfig wlan0

macchanger -r wlan0

how would i combine this into a small executable python script?
not looking for someone to do it for me..just some tips and advice or maybe another script that i can disassemble /reverse and change to make work.

Reply

RE: Need Help Creating My First Script #2
All your doing is issuing a sequential list of commands that dont change and you dont require the output of any of the commands to further the next command? I would think this is more suited to a shell script...

Im not a Python coder, but i just did a quick google search and this appears to be the module that you need:

http://docs.python.org/2/library/subprocess.html

Reply

RE: Need Help Creating My First Script #3
It's much better to use Bash Shell script for linux. Since your commands are quite simple, and this is not complex enough to use python.
But if you still want to stick with python, search for following modules.

Quote:os module (os.sys)
os module (os.popen)
subprocess

The first one is much simpler.
Since you wrote
Quote:not looking for someone to do it for me

I won't write that one out for you. But for any other guys, below is the code. (@wnrwnrchxdnr if you changed your mind, just take a peek :Sarcastic: )

[spoiler='Full Source Code Biggrin']
Bash Shell Script
Spoiler:
Code:
#!/bin/sh iwconfig wlan0 ifconfig wlan0 down iw reg set NZ ifconfig wlan0 up iwconfig wlan0 macchanger -r wlan0 # This much simple LOL


Python Script
Spoiler:
Code:
#!/usr/bin/python import os os.system('iwconfig wlan0') os.system('iwconfig wlan0 down') os.system('iw reg set NZ') os.system('ifconfig wlan0 up') os.system('iwconfig wlan0') os.system('macchanger -r wlan0') # os.system is used to execute os commands

[/spoiler]

Reply

RE: Need Help Creating My First Script #4
Wow..Thank you kindly gents.
Ill try using both ways just so i can have a better picture of whats going on.

Cheers!

Reply

RE: Need Help Creating My First Script #5
success!

ok so i cheated a bit and used the python script [zero-uplink] added but had to modify a few things because i was getting errors

Original
Code:
#!/usr/bin/python import os os.system('iwconfig wlan0') os.system('iwconfig wlan0 down') os.system('iw reg set NZ') os.system('ifconfig wlan0 up') os.system('iwconfig wlan0') os.system('macchanger -r wlan0') # os.system is used to execute os commands

Modified i had to make "ifconfig wlan0 down" from "iwconfig"
then i kept getting error with macchanger line executing ..
"Cant Change MAC: Interface being used or down ...."

i tried moving that command after the "boost tx ie set NZ" but before telling WLAN0 to come back up and it seems to do the trick.

Final :
############
#!/usr/bin/python
import os
os.system('iwconfig wlan0')
os.system('ifconfig wlan0 down')
os.system('iw reg set NZ')
os.system('macchanger -r wlan0')
os.system('ifconfig wlan0 up')
os.system('iwconfig wlan0')
# os.system is used to execute os commands
###########
Not sure if its the best most elegant way to do things but it works and im happy.
Totally shaved 48 seconds off my routine. :Thumbs-Up:

Reply

RE: Need Help Creating My First Script #6
(01-06-2014, 08:58 AM)wnrwnrchxdnr Wrote: Not sure if its the best most elegant way to do things but it works and im happy.

Well, as stated earlier a shell script would probably have been the most efficient. But if your intent was to get this done in python rather than efficiency then this will probably suffice.

And add code tags please. not sure why you decided to use them on one piece but not the other but its expected you use them for any code you post.

Reply

RE: Need Help Creating My First Script #7
You could use Bash to do this. Using os.system or os.poppen is like calling Bash commands

Reply

RE: Need Help Creating My First Script #8
(01-06-2014, 12:55 PM)hunt3r972 Wrote: You could use Bash to do this. Using os.system or os.poppen is like calling Bash commands

Quite frankly... you're beating a dead horse... 3 of the previous 5 responses prior to yours included the suggestion that shell scripts would be better/faster/easier.

Reply

RE: Need Help Creating My First Script #9
(01-06-2014, 10:45 AM)Geoff Wrote:
(01-06-2014, 08:58 AM)wnrwnrchxdnr Wrote: Not sure if its the best most elegant way to do things but it works and im happy.

Well, as stated earlier a shell script would probably have been the most efficient. But if your intent was to get this done in python rather than efficiency then this will probably suffice.

And add code tags please. not sure why you decided to use them on one piece but not the other but its expected you use them for any code you post.

Hi Geoff..thanks for the heads up ..i forgot to add the Final code when i saved. Then when i tried to edit the post i was able to add code tags. I will be sure to do it right next time..

re: shell script vs python
Im sorry but at this point i dont know what "efficient means.. Why would bash shell be more efficient than a python script?

I would assume efficiency would come into play when dealing with volumeous amounts of code
This script was like 6 lines so im not sure how much more efficient it could be.

Just trying to understand how you guys think so i can get better at making decisions.
thanks again for the advice.

Reply

RE: Need Help Creating My First Script #10
Well, a shell script would have been fewer lines of code to accomplish the same thing. While both call an interpreter of sorts, Python needs additional modules/libraries and while im not expert on python... I am pretty sure that the os.system() call simply executes a shell command.

So what youve done is written a python script that calls the python interpeter that references an external library that simply pipes the command to the shell interpreter.

a shell script would simply pipe the command to the shell interpreter.

Do you see how this might be considered inefficient? unnecessarily complex? note i said unnecessarily complex, i dont mean that its a complex script lol. Youre right that in reality the size of the script makes this a moot point... i doubt very much you would ever notice a performance issue. But for simple things like this where all you are doing is automating a list of commands, its in my opinion (not that its right), that a simple shell script would be best practice.

Reply