#!/bin/bash
for ip in 'seq 1 254'; do
ping -c 1 $0.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done
the file is ipsweep.sh
when i run it show me this
root#kali:ping: ./ipsweep.sh.seq: Name or service unkown
First issue:
Replace 'seq 1 254' with $(seq 1 254).
If you replace both ' with backticks it works too, but it's old syntax.
Second issue:
Replace $0 with $1 if you want to provide this part (192.168.1, e.g.) on the command line. $0 contains name of your script.
Related
I wrote a simple ping sweeper using bash script. I also use grep command to filter out the result I want. The problem is, the console keep printing out error message: "ping: recvmsg: No route to host" no matter what grep command I tried. I tried to write the output into a file, and there is no error message inside the file but they still appear on the console. I want to know what causes the console to print out error message like that and how to disable it, thanks.
Here is the script I wrote.
#!/bin/bash
for ip in $(seq 1 254); do
#ping -c 1 10.11.1.$ip | grep -v "recvmsg" | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1 &
ping -c 1 10.11.1.$ip | grep -v "recvmsg" |grep -v "ping" | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1| sort -d >> report &
done
wait
And here is the error message
ping: recvmsg: No route to host
You can use the redirectors for stderr (standard error) you only need put this at the end of your command 2> error.log
#!/bin/bash
for ip in $(seq 1 254); do
#ping -c 1 10.11.1.$ip | grep -v "recvmsg" | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1 &
ping -c 1 10.11.1.$ip 2> error.log | grep -v "recvmsg" |grep -v "ping" | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1| sort -d >> report &
done
wait
I'm trying read a file which contains lines like this:
Run COMMAND with options "OPTIONS" and arguments "ARGUMENTS"
Then I want to execute this command with given options and arguments. For example I'd like to execute these commands:
Run pwd with options "" and arguments ""
Run ls with options "-al" and arguments "$HOME"
Run ls with options "-al" and arguments "Example: \"strange folder name\""
This is my code
#!/bin/bash
while read -r line
do
COMMAND=$(echo "$line" | cut -d" " -f 2)
OPTIONS=$(echo "$line" | cut -d" " -f 5 | tr -d '"')
ARGUMENTS=$(echo "$line" | cut -d" " -f 8)
$COMMAND $OPTIONS $ARGUMENTS
done <$1
First example is working as it should, second one is giving me error ls: cannot access $HOME: No such file or directory' and third one is not storing the name of the folder to $ARGUMENTS correctly.
second one is giving me error ls: cannot access $HOME: No such file or directory'
This is because the folder named $HOME does not exist. I am not talking about the value of $HOME variable, but the string literal. The shell does not execute the parameter expansion in your situation.
third one is not storing the name of the folder to $ARGUMENTS correctly
This is because -f 8 only extract column 8, try -f 8- to extract the 8th column and all the others until the end of line.
You can give a try to this version below:
while read -r line; do
COMMAND=$(printf "%s" "${line}" | cut -d" " -f 2)
OPTIONS=$(printf "%s" "${line}" | cut -d" " -f 5 | tr -d '"')
ARGUMENTS=$(printf "%s" "${line}" | cut -d" " -f 8-)
$COMMAND $OPTIONS "$(eval printf \"%s\" "$ARGUMENTS")"
done < "${1}"
The eval is a shell built-in command which is used to enable parameter expansion of ARGUMENTS, if applicable.
I have to warn you that the eval is usualy say risky to use.
I am trying to do a simple thing:
To get the second number in the the line with the second occurence of the word TER and lower it by one and further process it. The tr -s ' ' is there because the file is not delimited by tabs, but by different amounts of whitespaces.
My script:
first_res_atombumb= grep 'TER' tata_sbox_cuda.pdb | head -n 2 | tail -1 |tr -s ' '| cut -f 2 -d ' '
echo $((first_res_atombumb-1))
but this only returnes:
255
-1
Of course I want to have 254.
adding | tr -d '\n' does not help either, what on earth is going on? I have already asked several people at work noone seems to know.
the lines in question look linke this
TER 128 DA3 4
TER 255 DA3 8
and if I apply grep 'TER' tata_sbox_cuda.pdb | head -n 2 | tail -1 | tr -s ' '| cut -f 2 -d ' ' in the command line i get what i expect, just 255
With bash, I'd write
n_ter=0
while read -a words; do
if [[ ${words[0]} == TER ]] && (( ++n_ter == 2 )); then
echo $(( ${words[1]} - 1 ))
fi
done < file
but I'd use awk
awk '$1 == "TER" && ++n == 2 {print $2 - 1}' file
The problem with your code: you forgot to use the $() command substitution syntax
first_res_atombumb= grep 'TER' tata_sbox_cuda.pdb | head -n 2 | tail -1 |tr -s ' '| cut -f 2 -d ' '
# .................^...............................................................................^
echo $((first_res_atombumb-1))
You're setting the variable to an empty string in the environment of the grep command. Then, since you're not capturing the output of that pipeline, "255" is printed to the terminal. Because the variable is unset in your current shell, you get echo $((-1))
All you need is:
first_res_atombumb=$(grep 'TER' tata_sbox_cuda.pdb | head -n 2 | tail -1 |tr -s ' '| cut -f 2 -d ' ')
# .................^^...............................................................................^
But I'd still use awk.
If I understand your problem correctly you can solve it using AWK:
awk 'BEGIN{v=0} $1 == "TER" {v++;if (v==2) {print $2-1 ;exit}}' tata_sbox_cuda.pdb
Explanation:
BEGIN{v=0} declaring and nulling the variable.
$1 == "TER" execute the command in {} only if it's the second occurence of TER.
{v++;if (v==2) {print $2-1 ;exit}}' increase the value of v and check if it's 2, in this case subtract 1 from the second field and display, exit afterwards (will make the processing faster and will skip unnecessary lines).
I wonder why this script never ends?
#! /bin/bash
for ip in `seq 200 254`; do
ping -c 1 192.168.0.$ip | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1 &
done
But if I choose not to run commands in parallel, script ends up just ok.
#! /bin/bash
for ip in `seq 1 254`; do
ping -c 1 192.168.0.$ip | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1
done
Could somebody explain, please, why is it happens?
Your for loops are doing the exact same thing. The only difference is that in the first instance you are running all commands in the background and will not see a termination. Specifically:
ping -c 1 192.168.0.$ip | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1 &
^^^
Using the & operator at the end tells bash to background the process. Eliminating the & runs the process in the foreground where the execution is visible.
I'm creating a script in Bash to change all MAC addresses of my PC. I can list all network interfaces with this:
ip link | grep "<" | cut -d " " -f 2 | cut -d ":" -f 1 | grep -v lo
And the output of the script is:
eth0
wlan0
Now I need to create a variable for each network interface (to use it in the future), but I don't know how, and Google didn't help me...
Answer:
readarray -t interfaces < <(ip link | grep "<" | cut -d " " -f 2 | cut -d ":" -f 1 | grep -v lo)
echo "${interfaces[0]}" # prints eth0
echo "${interfaces[1]}" # prints wlan0
And to loop over them use for:
for curInterface in "${interfaces[#]}"; do
echo "$curInterface"
done
But there are better ways to parse data:
First of all, instead of grepping < character you can use -o flag. This will output all of the data on single lines. Then you simply need the second word without : character. This is very simple in pure bash:
interfaces=()
while read -r _ curInterface _; do
interfaces+=("${curInterface%:}")
done < <(ip -o link)
Store the output in an array:
interfaces=( $(ip link | awk '/</ { print $2 }' | awk -F: '!/lo/ {print $1}') )
You can create an array from this output, and loop through it after.
my_array=( $(ip link | grep "<" | cut -d " " -f 2 | cut -d ":" -f 1 | grep -v lo) )
You can also this exmaple giving different alternatives redirect output to array
And I could have it simpler like this with one awk command:
readarray -t youravar < <(exec ip link | awk -F': ' '/^[0-9]+:/&&!/ lo: /{print $2}')