I wanted to get the IP of my DHCP server into a bash variable.
like : IP="192.168.1.254"
I know this IP can be found in /var/lib/dhcp/dhclient.leases or in /var/log/syslog but I don't know of to extract it and put it in variable during my script (bash)
EDIT: file dhclient.leases look's like
lease {
interface "eth0";
fixed-address 192.168.1.200;
option subnet-mask 255.255.255.0;
option routers 192.168.1.254;
option dhcp-lease-time 7200;
option dhcp-message-type 5;
option domain-name-servers 192.168.1.254;
option dhcp-server-identifier 192.168.1.254;
option host-name "bertin-Latitude-E6430s";
option domain-name "laboelec";
renew 1 2015/02/16 10:54:34;
rebind 1 2015/02/16 11:53:49;
expire 1 2015/02/16 12:08:49;
}
I want the IP from line option dhcp-server-identifier 192.168.1.254;.
To more compatibility I finally opted for a simple solution which is to send the IP server like a string on broadcast every seconds. For that I use socat (because netcat can't send message to braodcast)
my DHCP server run this script in background:
#!/bin/bash
interface="eth0"
IP=$(ifconfig $interface | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
Broadcast=$(ifconfig $interface | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f3 | awk '{ print $1}')
Port="5001"
while [ true ];
do
sleep 1
echo $IP | socat - UDP4-DATAGRAM:$Broadcast:$Port,so-broadcast
#to listen: netcat -l -u $Broadcast -p $Port
done
exit 0
Related
I am trying to grep the third octet in IP address to an tap device on remote machine.
ssh -t user#host "/sbin/ifconfig tap0 | grep "inet" | /usr/bin/awk -F'[: ]+' '{ print $4 }' | awk -F'[.]' '{print $3}'"
I am resulting this:
inet addr:10.22.66.77 Bcast:10.22.66.255 Mask:255.255.255.0
When i run the command on the remote machine it shows 66
How to make it working with ssh -t?
Sometimes using perl is simpler:
ssh -t user#host "/sbin/ifconfig tap0" | perl -n -e 'if (/inet\saddr:\d+\.\d+\.(\d+)/) { print "$1\n"}'
it runs regular expression pattern on the local machine match on the third octet following addr: and this is then printed via $1
The pattern match is run on the local machine to avoid problems with escaping " (In your example code the " in the grep inet seems to terminate the ssh...)
Is there any shell command for filtering interface names that are up and running
Run the command /sbin/ifconfig and look for UP.
If you want just the names of the active interfaces, you need some scripting:
ifconfig | awk '/^[^ ]/ { name=$1; } /^ +UP / {print name;}'
You may check it with several commands:
See the content of the network interface file:
cat /sys/class/net/eth0/operstate
Using ip command
ip a | grep -Eq ': eth0:.*state UP'
Or:
ifconfig | grep -Eq ': eth0:.*state UP'
Where eth0 is your interface. Original post
ifconfig | awk -v RS="" '/MULTICAST/ && /UP/ && /RUNNING/ && /BROADCAST/ {print substr($1, 0, length($1)-1)}'
Currently, I am using this command to spoof my MAC address:
macchanger eth0 -A
However, I want to be able to select which vendor I use. I know that the switch -l displays a list of all available vendors, but say I want to use one from Cisco, what command do I use to actually use that specific vendor rather than the system choosing a random vendor for me each time?
You could add a function like this to .bashrc
Then you can: macspoof Apple eth0
macspoof () {
Vendor=$(macchanger -l | grep $1 | shuf -n 1 | awk '{print $3}')
Tail=$(echo $RANDOM | md5sum | sed 's/.\{2\}/&:/g' | cut -c 1-8)
Mac="$Vendor:$Tail"
sudo macchanger -m $Mac $2
}
i'm creating a local simulator (not connected to internet) using SSH connection. I've started sshd on a particular range of port numbers and NATing a range of devices to those. I have to find the currently connected port number.
OS CentOS 5.5
OpenSSH 6.1
I've done the following. It works for normal usage (manual user).But when trying a rigorous testing(automated) it seems like it is failing sometimes to find the port number.
#!/bin/bash
WHOINFO=`who -m`
USERNAME=`echo $WHOINFO | awk 'NR==1{print $1}'`
PTSNUMBER=`echo $WHOINFO | awk 'NR==1{print $2}'`
USERSTR=$USERNAME"#"$PTSNUMBER
PID=`ps -eLf | grep $USERSTR | awk 'NR==1{print $3}'`
if [ -z "$PID" ];
then
exit
fi
PORTSTR=`netstat -natp | grep $PID | awk 'NR==1{print $4}'`
PORTNUMBER=${PORTSTR//*:/}
echo $PORTNUMBER
An OpenSSH server will set the variable $SSH_CLIENT, which contains the current ip, client port and server port separated by spaces:
$ echo "$SSH_CLIENT"
127.0.0.1 59064 22
To get the port number the current session is connected to, you can therefore use echo ${SSH_CLIENT##* }.
I'm writing a script to print ethtool details of ifconfig.
Sample output should be from ifconfig is like,
eth0
eth1
eth2
I have tried using below command,
root#bt# ifconfig | cut -d ":" -f 1
But could not able to achieve the same.
Actually, i need to get these eth* and pass in
root#bt# ethtool <arg1> where arg1=eth*
to get results :-) can you please help me to get crop from ifconfig. ?
No.
$ awk -F: '$1 ~ "eth" { print $1 }' /proc/net/dev
eth0
With grep & ifconfig :
ifconfig | grep -o '^eth[0-9]\+'
Or with only grep :
grep -oP '^ *\Keth[0-9]+' /proc/net/dev