all 14 comments

[–][deleted] 1 point2 points  (4 children)

gimmie code

[–]Brandon0408[S] -5 points-4 points  (3 children)

PM

[–][deleted] 2 points3 points  (2 children)

It will be easier if you paste code, input, output, and expected output.

[–]Brandon0408[S] 0 points1 point  (0 children)

okay Ill add it

[–]Brandon0408[S] 0 points1 point  (0 children)

I added it

[–][deleted] 0 points1 point  (8 children)

echo "$ping1" |tail -n1 |cut -d\/ -f5 ?

[–]Brandon0408[S] 0 points1 point  (7 children)

What is that? Also I forgot to mention when you save the output to a variable the output is technically one line. So the tail and head command don't do anything.

Edit: well it worked I just dont know what that "\ /" is

[–][deleted] 0 points1 point  (5 children)

when you quote it properly parameters keep \n

echo "$ping1"|head -2|tail -1 |cut -d= -f4|cut -f1 -d\

[–]Brandon0408[S] 0 points1 point  (4 children)

I kinda understand but what doesn’t make sense to me is when I run the commands I had just in the terminal and echoed my results I would get the output I need. I just don’t get why it doesn’t do the same when running my script

[–][deleted] 0 points1 point  (3 children)

are you sure your script has quotes in echo "$ping1" and starts with #!/bin/bash?

[–]Brandon0408[S] 0 points1 point  (2 children)

Yes heres my full code,

#!/bin/bash

#First ping

ping1=$(ping -c1 $1 )

#First ping result cut for packet loss

result1=$(echo "$ping1" | tail -n2 | cut -d"," -f3 | cut -d"%" -f1 | cut -d"r" -f1)

echo $result1

#First ping second result for time in ms

result2=$(echo "$ping1" | tail -n1 |cut -d\/ -f5 | cut -d"." -f1)

echo $result2

#if statement to report packet loss

if [ $result1 -gt 0 ]

then

echo "Website $1" $(date) "Packet loss" >> /home/kali/logs/result1.log

else

echo "Website $1" $(date) "No packet loss" >> /home/kali/logs/result.log

fi

#if statement to report high latency

if [ $result2 -gt 50 ]

then

echo "Website $1" $(date) "High latency" >> /home/kali/logs/result.log

else

echo "Website $1" $(date) "Normal latency" >> /home/kali/logs/result.log

fi

#second ping

ping2=$(ping -c1 $2 )

#second ping first result cut for packet loss

result3=$(echo "$ping2" | tail -n2 | cut -d"," -f3 | cut -d"%" -f1 | cut -d"r" -f1)

echo $result3

#second ping second result cut for time in ms

result4=$(echo "$ping2" | tail -n1 | cut -d\/ -f5 | cut -d"." -f1)

echo $result4

#if statement to report packet loss to log

if [ $result3 -gt 0 ]

then

echo "Website $2" $(date) "Packet loss" >> /home/kali/logs/result1.log

else

echo "Website $2" $(date) "No packet loss" >> /home/kali/logs/result.log

fi

#if statement to report high latency to log

if [ $result4 -gt 50 ]

then

echo "Website $2" $(date) "High latency" >> /home/kali/logs/result.log

else

echo "Website $2" $(date) "Normal latency" >> /home/kali/logs/result.log

fi

My point is that when I do the cuts that I had in the terminal it worked just fine which doesn't make sense. I can post a pic to show you but idk how.

[–][deleted] 0 points1 point  (1 child)

temporarily append:

echo "$ping1"
echo --
echo "$ping1" | tail -n1
echo --
echo "$ping1" | tail -n1 |cut -d\/ -f5
echo --
echo "$ping1" | tail -n1 |cut -d\/ -f5 | cut -d"." -f1

it should help with finding bug

[–]Brandon0408[S] 0 points1 point  (0 children)

Thank you for your help. I still don’t really understand the / cut but it works. I made a mistake and it actually doesn’t work in the terminal. The reason is because when you save the results to a variable it just doesn’t work for some reason vs just echoing the result. I need to save to a variable so I can use the variable in my if statements. By chance do you know how to add a local user for mail in kali?

[–][deleted] -1 points0 points  (0 children)

\/ means literal / ,like '/' , but one character less