How to stop the airodump-ng command after 5 seconds - bash

I'm trying to run the airodum-ng command for 5 seconds then stop it and save the output to the network variable, so I can print the output to a whiptail gui. Unfortunately the command just never stops running does anyone know how to fix this. I tried the timeout method witch you can see here and i also tried to use sleep but no luck.
function aircrack() {
sudo airmon-ng check kill
name=($(sudo /usr/sbin/airmon-ng ))
sudo airmon-ng start ${name[5]}
name=($(sudo /usr/sbin/airmon-ng ))
**netwroks=($(timeout 5s sudo airodump-ng ${name[5]}))**
}

In your code, the non-root timeout tries to kill a root process and fails.
The correct way to use timeout with sudo would be:
sudo timeout 5s airodump-ng ${name[5]}

Related

How to run Bash Script on startup and keep monitoring the results on the terminal

Due to some issues I wont elaborate here to not waste time, I made a bash script which will ping google every 10 minutes and if there is a response it will keep the loop running and if not then the PC will restart. After a lot of hurdle I have been able to make the script and also make it start on bootup. However the issue is that i want to see the results on the terminal, meaning I want to keep monitoring it but the terminal does not open on bootup. But it does open if I run it as ./net.sh.
The script is running on startup, that much I know because I use another script to open an application and it works flawlessly.
My system information
NAME="Linux Mint"
VERSION="18.3 (Sylvia)"
ID=linuxmint
ID_LIKE=ubuntu
PRETTY_NAME="Linux Mint 18.3"
VERSION_ID="18.3"
HOME_URL="http://www.linuxmint.com/"
SUPPORT_URL="http://forums.linuxmint.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/linuxmint/"
VERSION_CODENAME=sylvia
UBUNTU_CODENAME=xenial
The contents of my net.sh bash script are
#! /bin/bash
xfce4-terminal &
sleep 30
while true
do
ping -c1 google.com
if [ $? == 0 ]; then
echo "Ping Sucessful. The Device will Continue Operating"
sleep 600
else
systemctl reboot
fi
done
I have put the scripts in /usr/bin and inserted the scripts for startup at boot in /etc/rc.local
So I did some further research and with help from reddit I realized that the reason I couldnt get it to show on terminal was because the script was starting on bootup but I needed it to start after user login. So I added the script on startup application (which can be found searching on start menu if thats whats it called). But it was still giving issues so I divided the script in two parts.
I put the net.sh script on startup and directed that script to open my main script which i named net_loop.sh
This is how the net.sh script looks
#! /bin/bash
sleep 20
xfce4-terminal -e usr/bin/net_loop.sh
And the net_loop.sh
#! /bin/bash
while true
do
ping -c1 google.com
if [ $? == 0 ]; then
echo "Ping Sucessful. The Device will Continue Operating"
sleep 600
else
systemctl reboot
fi
done
The results are the results of the net_loop.sh script are open in another terminal.
Note: I used help from this thread
If minute interval is usable why not use "cron" to start your?
$> crontab –e
or
$> sudo crontab –e

How to stop a command after a certain time in bash

In bash, I'm running iftop tool with text output. It's a tool that is monitoring network and writting the results periodicaly in the standard output (by default the terminal but could be something else, a file). It's running in foreground until I stop it with ctrl + C
I would like to make it running for a certain period of time (1 minute for example), and then stop the process automatically
How can I make this in bash ?
Here is what I already tried:
sudo iftop -nNPt -L 100 -i wlp0s20f3 > iftop.out & pid=$!; sleep 20; sudo kill $pid
But, 1) it does not kill the process and 2) it does not redirect to the file
Using the timeout command in Linux should work, the syntax is below,
timeout [OPTION] DURATION COMMAND [ARG]...
I guess you can try putting everything after & in the next line. For this, you'll have to put the whole commands in a function in your .bashrc or in a script :
sudo iftop -nNPt -L 100 -i wlp0s20f3 > iftop.out &
pid=$!; sleep 20; sudo kill $pid

Using timeout with docker run from within script

In my Travis CI, part of my verification is to start a docker container and verify that it doesn't fail within 10 seconds.
I have a yarn script docker:run:local that calls docker run -it <mytag> node app.js.
If I call the yarn script with timeout from a bash shell, it works fine:
$ timeout 10 yarn docker:run:local; test $? -eq 124 && echo "Container ran for 10 seconds without error"
This calls docker run, lets it run for 10 seconds, then kills it (if not already returned). If the exit code is 124, the timeout did expire, which means the container was still running. Exactly what I need to verify that my docker container is reasonably sane.
However, as soon as I run this same command from within a script, either in a test.sh file called from the shell, or if putting it in another yarn script and calling yarn test:docker, the behaviour is completely different. I get:
ERRO[0000] error waiting for container: context canceled
Then the command hangs forever, there's no 10 second timeout, I have to ctrl-Z it and then kill -9 the process. If I run top I now have a docker process using all my CPU forever. If using timeout with any other command like sleep 20 && echo "Finished sleeping", this does not happen, so I suspect it may have something to do with how docker works in interactive mode or something, but that's only my guess.
What's causing timeout docker:run to fail from a script but work fine from a shell and how do I make this work?
Looks like running docker in interactive mode is causing the issue.
Run docker in detached more by removing the -it and allowing it to run in default detached mode or specify -d instead of -it and so:
docker run -d <mytag> node
or
docker run <mytag> node

Rtcwake run script after waking up

I want to run script after executing and waking up from rtcwake command. Next commands are run procedurally but I want to have other part of script running only after fully execution command. How to achieve it avoiding hacks like sleep? Sometimes it takes a few seconds for typing password and when I do it in more than 10 seconds it runs already script after rtcwake.
sudo rtcwake -m mem -t $DESIRED &
sleep 10
echo "Hello again $USER!"

Background task after logging out

I am trying to make a surveillance camera turn on after a specified amount of time after I leave.
I imagined it to be something like this:
sleep 60 && sudo service motion start &> /dev/null &
but the background task seems to get deleted when I log out. Is there a way to make it stick around even after I leave? AND also to have the root permissions?
EDIT:
Okay I ended up making a script that does it instead of using a single command, it looks about like this:
#!/bin/bash
if (( $UID > 0 )); then
echo "Only root can run this script"
exit 1
fi
if [ -z $1 ]; then
TIMEOUT=30
else
TIMEOUT=$1
fi
sleep $TIMEOUT && service motion start &>/dev/null &
exit 0
use nohup:
sudo nohup service motion start &
In addition to nohup, you can also use screen:
screen
sudo service motion start
Then type CTRL+a then d to detach the screen.
To reattach, you can simply use:
screen -x
For more information:
man screen
There are 2 issues here:
Executing something after you log out. (I guess you are planning to put it in ~/.bash_logout, which is a good idea)
Executing it under sudo, which may be difficult.
Below are the common approaches:
nohup sudo ( sleep 60; service motion start ) &
#Has issues, if sudo authentication is not complete till this point, since background process cannot prompt you for sudo password. Better to call `sudo true` or `sudo echo -n` or similar dumb command, under sudo before calling the actual command.
#You may be tempted to call sudo only for service command, instead of entire command.
However, there is a risk that if sleep interval is long enough, then sudo token may expire.
echo service motion start | sudo at now+1min
#The problem with this command is that the granularity is minute level, not seconds level. Otherwise, this looks clean to me.
Based on your requirement, you can choose either of them, or any other mechanism mentioned in the other answers.

Resources