LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   3 related questions on bash command usage. (https://www.linuxquestions.org/questions/linux-newbie-8/3-related-questions-on-bash-command-usage-780219/)

lupusarcanus 01-06-2010 04:31 AM

3 related questions on bash command usage.
 
Hello all.

I am using aircrack-ng to perform testing on my network, using Ubuntu 9.10.
However that is not what my question is about, just a little background info. This may not seem to fit in this section of the forum, but it does (read whole post).

I will try to make this as short and well explained as I can, but go ahead and ask if I miss something.

What I am trying to do is make a shortcut in the GNOME menu for a few particular commands. I know how to do that rather easily; but keep that in mind before you ask "why not just do it the other way?" I feel I may learn something from it while making this handy shortcut, so I feel the need to post here, since this compound issue is a bit hard to Google out. Forgive me if I am wrong.

To use aircrack-ng, I have to put my wireless card into monitor mode, which works, but with a loophole. I have to click on Network Manager and disable networking first. Now I know that many of you don't use Ubuntu or Network Manager, so unless you know what I am talking about, assume what I am saying here is not allowing Linux to associate with any of the AP's, without turning the card "off" or disallowing any other features temporarily.
So my first question is how do I do that in the command line?

After that, what I do is more simple:
Code:

(Answer to first question)
sudo su
ifconfig wlan0 down
iwconfig wlan0 mode monitor
ifconfig wlan0 up
airodump-ng wlan0

My second question is how to format the above so that it is one streamlined command in which one is executed after another?

Do you use a "|" in between each command like so:

Code:

sudo su | ifconfig wlano down | iwconfig wlano mode monitor
?

Or do you use a ";" in between commands like so:

Code:

sudo su; ifconfig wlano down; iwconfig wlan0 mode monitor
?

My third question is much simpler, will this work?

(If my commands are a little messed up, it's because I haven't used aircrack for quite some time)

Thanks

EricTRA 01-06-2010 04:40 AM

Hello,

You can put all the commands in a file and make it executable. That way all command will be executed line by line one after the other. Then in your Gnome, call that script.

If you want to execute commands sequentially on one line use the && operand like this:
Code:

sudo su && ifconfig wlano down && iwconfig wlano mode monitor
The | is for piping output of one command into the next command.

If it will work? Don't know, why don't you try it out? Run the commands from a terminal to see what they do and where (if) they fail before putting them in a shell script. Solve each problem first so that you don't have to troubleshoot the whole script at once without knowing which part fails.

You might want to take a look into this guide to learn all about Bash scripting: Bash Guide for Beginners

Kind regards,

Eric

lupusarcanus 01-06-2010 04:42 AM

Quote:

Originally Posted by EricTRA (Post 3815985)
Hello,

You can put all the commands in a file and make it executable. That way all command will be executed line by line one after the other. Then in your Gnome, call that script.

If you want to execute commands sequentially on one line use the && operand like this:
Code:

sudo su && ifconfig wlano down && iwconfig wlano mode monitor
The | is for piping output of one command into the next command.

If it will work? Don't know, why don't you try it out? Run the commands from a terminal to see what they do and where (if) they fail before putting them in a shell script. Solve each problem first so that you don't have to troubleshoot the whole script at once without knowing which part fails.

Kind regards,

Eric

That sounds like it work perfectly. But how do I do the said "disable networking" thing in the CLI?

EricTRA 01-06-2010 04:48 AM

Hi,

Calling
Code:

/etc/init.d/networking stop
on the console will stop networking if that's what you mean. You could also look at the networking script mentioned in the previous command to see what exactly it does.

Kind regards,

Eric

lupusarcanus 01-06-2010 04:53 AM

Hi.

Si I made the beginnings of my script:

Quote:

#! /bin/bash
sudo su && ifconfig wlan0 down && iwconfig wlan0 mode monitor && ifconfig wlan0 up
And then Google'd how to make it executable and did so:

Quote:

chmod u+x /home/andrew/Desktop/airodump-ng_auto
And then I ran it, and it did not do anything after I entered my password. It seems that "sudo su" bit stalwarts the rest of the script from being executeable.

EricTRA 01-06-2010 04:57 AM

Hi,

Delete the sudo su part from the script and enter a su console manually, then run the script without the sudo part to see if the rest works. That way you can make sure that the sudo su part is the culprid.

I have almost never used sudo (I'm always root), so don't really know how to bypass it.

Kind regards,

Eric

EricTRA 01-06-2010 04:59 AM

What you could do, if I remember correctly is change the SUDOERS file to list this command in order to be executed by you without a password. Also that way you could call the commands directly using sudo and without entering a su shell.

Kind regards,

Eric

lupusarcanus 01-06-2010 05:10 AM

Quote:

Originally Posted by EricTRA (Post 3815998)
Hi,

Delete the sudo su part from the script and enter a su console manually, then run the script without the sudo part to see if the rest works. That way you can make sure that the sudo su part is the culprid.

I have almost never used sudo (I'm always root), so don't really know how to bypass it.

Kind regards,

Eric

Hi Eric.

The /etc/init.d/networking stop command seemed to not work.
Console output:
Code:

root@mylinuxbox:/home/andrew# /etc/init.d/networking stop
 * Deconfiguring network interfaces...                                          Ignoring unknown interface eth0=eth0.
Ignoring unknown interface wlan0=wlan0.
                                                                        [ OK ]

Without that, the script won't be able to run correctly, because for some reason Linux decides to automagically reconnect to the network when I tell it to go "down" It only very quickly disconnects from the network and then reconnects.

So to answer your question, I don't think the script actually worked, unfortunately, unless I have done something wrong. (which is plausible)
What I did is change the script to include a command that would produce output.

Code:
Code:

#! /bin/bash
ifconfig wlan0 down && iwconfig wlan0 mode monitor && lspci

And when I clicked "Run" nothing happened. I tried it again, this time saying "Run In Terminal" and a Terminal window appear ever so quickly and dissappaered, having nothing changed.

EricTRA 01-06-2010 05:14 AM

Hi,

Just noticed something. You've got wlan0 that you want to operate on, the wireless interface. If I'm not mistaking that doesn't get handled by the networking script. Try this and see if you get the result you want:
Code:

sudo service network-manager stop
Kind regards,

Eric

repo 01-06-2010 05:16 AM

Are these commands working? the way you want it?

Quote:

(Answer to first question)
sudo su
ifconfig wlan0 down
iwconfig wlan0 mode monitor
ifconfig wlan0 up
airodump-ng wlan0

If yes, put them in a script and run it as root

lupusarcanus 01-06-2010 05:19 AM

Quote:

Originally Posted by EricTRA (Post 3816007)
Hi,

Just noticed something. You've got wlan0 that you want to operate on, the wireless interface. If I'm not mistaking that doesn't get handled by the networking script. Try this and see if you get the result you want:
Code:

sudo service network-manager stop
Kind regards,

Eric

Thanks again Eric.

This command worked to perfection, so now all thats left is to figure out whats wrong with my script and how to bypass sudo su.

lupusarcanus 01-06-2010 05:20 AM

Quote:

Originally Posted by repo (Post 3816009)
Are these commands working? the way you want it?




If yes, put them in a script and run it as root

These command are working, sir. I was asking if putting them in a script and running as root would work. Apparently the script did not. See above posts to look at script. Thanks

EricTRA 01-06-2010 05:22 AM

Check out the SUDOERS file. In there you can set options to use with commands when executed by certain users. So if you put in there that for your user, when executing the commands in that script no sudo password should be asked, then it would run in my opinion. But again, I'm not very familiar with sudo so if you encounter any problems with it, just post them here and I'm sure someone will give you an answer (or just out of curiosity I might look into it).

Kind regards,

Eric

repo 01-06-2010 05:23 AM

Try this, and run it as root from a terinal

Quote:

#! /bin/bash
service network-manager stop
ifconfig wlan0 down
iwconfig wlan0 mode monitor
ifconfig wlan0 up
airodump-ng wlan0

lupusarcanus 01-06-2010 05:40 AM

Quote:

Originally Posted by repo (Post 3816020)
Try this, and run it as root from a terinal

Hi. I re-did the file to those specifications, and it resulted in the said flashing of the terminal with no output or action. (It flashes very fast so I can't really see if anything happened but the command "airodump-ng" should result in sustained output)

I wasn't sure where to find the SUDOERS file so I did a search in nautilus as root and it found many, many files. One that I came across had this:

Code:

# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.
#

Defaults        env_reset

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root        ALL=(ALL) ALL

# Uncomment to allow members of group sudo to not need a password
# (Note that later entries override this, so you might need to move
# it further down)
# %sudo ALL=NOPASSWD: ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL)

Apparently I am of the admin group?

EricTRA 01-06-2010 05:43 AM

Hi,

That indeed is the sudoers file, but don't just edit it like that. You have to use:
Code:

sudo visudo
as a user in order to edit it. To check out if you are of the admin group (which I think you are if you're the only user, and since you can use sudo), type in a terminal as your user:
Code:

id
and it'll show you all the groups and ids assigned to your user.

Kind regards,

Eric

EricTRA 01-06-2010 05:46 AM

Also, what would be very useful to you is to look at the sudoers man page:
Code:

man sudoers
It's pretty extensive reading but I think worth the while. You can set a lot in the sudoers list.

Kind regards,

Eric

lupusarcanus 01-06-2010 05:47 AM

Quote:

Originally Posted by EricTRA (Post 3816044)
Hi,

That indeed is the sudoers file, but don't just edit it like that. You have to use:
Code:

sudo visudo
as a user in order to edit it. To check out if you are of the admin group (which I think you are if you're the only user, and since you can use sudo), type in a terminal as your user:
Code:

id
and it'll show you all the groups and ids assigned to your user.

Kind regards,

Eric

Hello once again Eric.

"id" returns:
Code:

andrew@mylinuxbox:~$ id
uid=1000(andrew) gid=1000(andrew) groups=4(adm),20(dialout),24(cdrom),46(plugdev),104(lpadmin),115(admin),120(sambashare),1000(andrew)


EricTRA 01-06-2010 05:48 AM

Quote:

Originally Posted by leopard (Post 3816041)
Hi. I re-did the file to those specifications, and it resulted in the said flashing of the terminal with no output or action. (It flashes very fast so I can't really see if anything happened but the command "airodump-ng" should result in sustained output)

Did you still leave a
Code:

sudo su
in your script? I think it's best to leave it out of the script and just use it like this:
Code:

sudo yourscriptname
Kind regards,

Eric

lupusarcanus 01-06-2010 06:03 AM

Thanks Eric!

That did the trick. I made a GNOME menu entry and used "gksudo /home/andrew/Desktop/airodump-ng_auto" (airodump-ng_auto was my script name) and it worked like a charm.

I attached a few screenshots of the script in action!

EricTRA 01-06-2010 06:05 AM

Hello Andrew,

Glad you got it running as you wanted. If you consider your thread as solved please mark it as such using the Thread Tools.

Kind regards,

Eric

lupusarcanus 01-06-2010 06:13 AM

Quote:

Originally Posted by EricTRA (Post 3816066)
Hello Andrew,

Glad you got it running as you wanted. If you consider your thread as solved please mark it as such using the Thread Tools.

Kind regards,

Eric

Sure did! It's in my sig! +3 Thank Icon clicks!

Here is the script that I used for future reference of this thread.

Code:

#! /bin/bash
service network-manager stop
ifconfig wlan0 down
iwconfig wlan0 mode monitor
ifconfig wlan0 up
airodump-ng wlan0


vrmartin2 01-06-2010 08:12 AM

&& vs ||
 
Also note that you join commands with && if you want to stop the sequence if any of the commands fail. "echo 1 && echo 2" means echo 1 and if that statement succeeds, then run the command "echo 2" and on and on any number of commands. On the other hand "echo 1 || echo 2" means run "echo 1" and then only if that fails, run "echo 2"

And if you really don't care what the return status is or the return status is undefined (unlikely on Linux but very prevalent in Windows) then just use the semicolon (";").

internetplayer 01-06-2010 08:55 AM

it looks great to me i am curious to know further.


All times are GMT -5. The time now is 08:42 PM.