Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract ip address from variable string

Tags:

bash

unix

I'm trying to create a bash script which will be able to change the "allow from" ip address in the phpmyadmin command file (which im still not sure is possible to do) and restart apache

I'm currently trying to extract an ip address from a variable and after searching the web I still have no clue, here is what I have so far...

#bash shell script
#!/bin/bash

clear
echo "Get client IP address"
ip=$(last -i)
echo $ip

exit
echo "restart apache"
/etc/init.d/apache2 reload

I've tried adding the following line with no luck

ip=$(head -n 1 $ip)

If anyone can tell me how I can extract the first instance of an IP address from the variables $ip I would appreciate it very much.

like image 653
mk_89 Avatar asked Nov 30 '22 01:11

mk_89


1 Answers

ip=$(last -i | head -n 1 | awk '{print $3}')

Update:

ip=$(last -i | grep -Pom 1 '[0-9.]{7,15}')
like image 69
Cyrus Avatar answered Dec 04 '22 06:12

Cyrus