I was wondering how to extract only certain data from the output of a shell command in Applescript. I want to be able to only pass the IP address into the variable from a "ping -o" command like this:
do shell script "ping -o " & blockedURL
-- Set the IP to blockedIP --
set blockedIP to ..
but I receive this:
"PING example.com (192.0.43.10): 56 data bytes 64 bytes from
192.0.43.10: icmp_seq=0 ttl=239 time=101.587 ms
--- example.com ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/stddev =
101.587/101.587/101.587/0.000 ms"
When I execute the ping command I receive a lot of data I dont need. Is there any way of being able to only recall the (192.0.43.10)?
set a to "PING example.com (192.0.43.10): 56 data bytes 64 bytes from 192.0.43.10: icmp_seq=0 ttl=239 time=101.587 ms
--- example.com ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 101.587/101.587/101.587/0.000 ms"
set text item delimiters to "("
set temp to text item 2 of a
set text item delimiters to ")"
set temp to first text item of temp
return temp
The above is a full applescript solution. You can also use the following to get the IP just using the shell ping -o www.google.com | cut -d'(' -f2|cut -d')' -f1 | head -n1 so in applescript it would look like this :
do shell script "ping -o " & blockedURL & " | cut -d'(' -f2 | cut -d')' -f1 | head -n1"
Related
I am trying to count and display only the words that are repeated more than once in a file. The basic idea is:
You are given a file with names and characters like commas, colons, slashes, etc..
Use the cut command to display only the first names in the file (other commands are also allowed).
Count and then display only the names repeated more than once.
I got to the point of counting and displaying all the names. However, I haven't found a way to display and to count only those names repeated more than once.
Here is a section of the file:
user1:x:80:200:Mia,Spurs:/home/user1:/bin/bash
user2:x:80:200:Martha,Dalton:/home/user2:/bin/bash
user3:x:80:200:Lucy,Carlson:/home/user3:/bin/bash
user4:x:80:200:Carl,Bingo:/home/user4:/bin/bash
Here is what I have been able to do:
Daniel#Daniel-MacBook-Pro Files % cut -d ":" -f 5-5 file1 | cut -d "," -f 1-1 | sort -n | uniq -c
1 Mia
3 Martha
1 Lucy
1 Carl
1 Jessi
1 Joke
1 Jim
2 Race
1 Sem
1 Shirly
1 Susan
1 Tim
You can filter out the rows with count 1 with grep.
cut -d ":" -f 5 file1 | cut -d "," -f 1 | sort | uniq -c | grep -v '^ *1 '
i need to get information, which users have connected to facebook from my proxy. I have a 2 loops.
#i have format og logs, like squid.log.12.10.2017
#with `ls squid.log*` i am working with all squid logs, day by day
for i in `ls squid.log*`; do
echo "There is log $i, that we need to check"
#i am getting count of ip addresses, that were on fb.com and i am writing them to ~/temp_cache
# like this:
# 25 192.168.110.5
# 41 192.168.110.2
# where 192.168.110.5 have connected to fb.com 25 times
# and 192.168.110.2 have connected to fb.com 41 times
zgrep fb.com /var/log/$i | cut -d " " -f1 | sort | uniq -c | sort -n -k 1 >> ~/temp_cache
#i am getting only list of ip, without count of connections to facebook
# like this:
# 192.168.110.5
# 192.168.110.2
ip=$(zgrep fb.com /var/log/$i | cut -d " " -f1 | sort | uniq -c | sort -n -k 1 | awk '{print $2}')
for y in $ip; do
echo "Users from $y:"
# i have system, that we are using for projects, in this system we have log ip-addresses and logins, from these ip addresses
# like for this ip 192.168.110.5, i am getting name of user duke
# main result, like this:
# duke
# the_rock
redmine_users=$(tail -n 500000 /usr/share/redmine/log/production.log | grep -A 3 "$y" | grep "Current user" | awk '{print $3}' | head -n 1)
# i am appending to lines, name of users for these lines
# in a result it should be like this:
# 25 192.168.110.5 duke
# 41 192.168.110.2 the rock
counter=$((counter+1))
sed -i "$counter s|$| $redmine_users |" ~/temp_cache
done
# Delimiter for each day of logs
echo "------------------------------------------------" >> ~/temp_cache
done
For the first looking it works. But it works only for one day. If script is going to second log, i mean squid.log.13.10.2017, it make something like this:
25 192.168.110.5 duke
41 192.168.110.2 the rock
______________________________ hogan
33 192.168.110.1
But i want to do this:
25 192.168.110.5 duke
41 192.168.110.2 the rock
______________________________
33 192.168.110.1 hogan
I tried to run script manually for one day, with existsing line ______________________________ and with changing
counter=$((counter+1))
sed -i "$counter s|$| $redmine_users |" ~/temp_cache
to
counter=1
counter=$((counter+1))
sed -i "$counter s|$| $redmine_users |" ~/temp_cache
But in a result i have:
______________________________
25 192.168.110.5 duke the rock
41 192.168.110.2
How to do, what i want, at least:
______________________________
25 192.168.110.5 duke
41 192.168.110.2 the rock
How to change counter in this construction:
counter=$((counter+1))
sed -i "$counter s|$| $redmine_users |" ~/temp_cache
If I understand correctly, you are trying to get it so the name is on the same line as the count and ip?
Assuming this is correct, you simply need to increase your counter after adding the ---- as well as where it currently is.
I do have multiple other issues with the code but I think this answers the main question. Let me know if I have missed the mark?
i have found. the counter should be in first loop. thanks for attention
So I have a script that creates a tunnel. To do that it uses random ports.
This is the logic for random port generation
RPORT=1
while [ $RPORT -lt 2000 ]
do
RPORT=$[($RANDOM % 3000) + 1]
done
This is good only if the port that it selects isn't in use. If that port is active, I am unable to access that server while that port is being used.
I want to do something like this
while [netsat -nat | grep $RPORT] = true
do
RPORT=$[($RANDOM % 3000) + 1]
So I want to check first if that port is in use, if so, search for another random port, check if it is in use, if no then use it.
Thank you very much in advance for you time and help!
function random_unused_port {
(netstat --listening --all --tcp --numeric |
sed '1,2d; s/[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*:\([0-9]*\)[[:space:]]*.*/\1/g' |
sort -n | uniq; seq 1 1000; seq 1 65535
) | sort -n | uniq -u | shuf -n 1
}
RANDOM_PORT=$(random_unused_port)
This was the function that helped me out!
Thank you Nahuel Fouilleul for the link!
To fix the answer, also because port from 1 to 1000 are reserved seq starts at 1001
grep -F -x -v -f <(
netstat --listening --all --tcp --numeric |
sed '1,2d; s/[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*:\([0-9]*\)[[:space:]]*.*/\1/g' |
sort -nu
) <(seq 1001 65536) | shuf -n 1
I have the following example output from a log file where im trying to get the reverse pointer records for the IP Addresses in column 7 below
2017-01-09 11:25:22.421 0.306 TCP 192.168.1.2:50599 -> 192.0.2.25:443 500 20000 1
2017-01-09 11:30:11.210 0.000 TCP 192.168.1.2:50503 -> 192.0.2.25:443 100 4000 1
2017-01-09 09:01:22.546 0.000 TCP 192.169.1.2:50307 -> 192.0.2.25:443 100 4000 1
If I run this awk command I can extract the reverse records for column 7:
cat test.txt | awk '{print $7}'| grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'| xargs -I % bash -c 'echo "$(dig -x % +short)"'
How do I get the output from the above command to replace whats in column 7 to update it so it will read for example:
2017-01-09 11:25:22.421 0.306 TCP google.com -> 192.0.2.25:443 500 20000 1
2017-01-09 11:30:11.210 0.000 TCP -> 192.0.2.25:443 100 4000 1
2017-01-09 09:01:22.546 0.000 TCP yahoo.com -> 192.0.2.25:443 100 4000 1
Using awk only:
$ awk '{split($7,a,":"); r=""; c="dig -x " a[1] " +short"; c|getline r; $7=r} 1' file
split by : to get the ip from $7 to a[1]
construct the dig command for shell to c var
execute it and store result to r
replace $7 with r and print with 1
Not showing any example output as the test file didn't have ips that would return any reverse.
I'm trying to create script which will ping to remote server and displays only
1) % packet loss
2) average round trip latency in ms
for packet loss i created line
`ping -c 3 -s 14 x.x.x.x|grep packet|awk '{print $7}'|cut -d'%' -f1`
which only gives packet loss,
my problem starts when ip is not reachable output of line changes
and hence i'm not able to capture both
for reference i'm showing output of both scenarios
`/pefmephbir >ping -c 3 -s 14 10.9.50.225`
PING 10.9.50.225 (10.9.50.225): 14 data bytes
--- 10.9.50.225 ping statistics ---
**3 packets transmitted, 0 packets received, 100% packet loss**
`/pefmephbir >ping -c 3 -s 14 10.9.50.220`
PING 10.9.50.220 (10.9.50.220): 14 data bytes
22 bytes from 10.9.50.220: icmp_seq=0 ttl=63 time=0 ms
22 bytes from 10.9.50.220: icmp_seq=1 ttl=63 time=0 ms
22 bytes from 10.9.50.220: icmp_seq=2 ttl=63 time=0 ms
--- 10.9.50.220 ping statistics ---
**3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0/0/0 ms**
hope someone with expertise in scripting can help me out
TIA
Try this:
ping ... | awk '/packet loss/{x="Loss:" $7} /round-trip/{x="Trip:" $4} END{print x}'
If it sees a line with "packet loss" in it, it creates a string x with the packet loss percentage. If it sees a line with "round-trip" it overwrites the string x with the round trip time. At the end, it prints whatever the string x is set to.
In the light of your comments...
awk '/packet loss/ && /100/{x="Loss: " $7} /round-trip/{split($4,a,/\//);x="Ave: " a[2]} END{print x}'
So, now the "packet loss" line must also contain "100" before we pick it up. And the 4th field of the "round-trip" line is split into an array a[] using / as separator, then we take the second element of a[] as the average and store that in our output string x.
Using sed:
ping ... | sed -n -e 's/.*\(100% packet loss\).*/\1/p' \
-e 's_.*min/avg/max = [0-9]*/\([0-9]*\)/[0-9]*.*_\1_p'