I came up with a simple solution to a complex problem.
I have 6 Linux machines on my home network. I don't want to hunt them all down when updates or service is needed, so I use SSH to log into them remotely. I use a private key to avoid password entry and I use non-standard ssh ports to a tad extra security. For my static IP machines (if it has a wired connection, I configure it statically) I simply put the hostname and IP into my /etc/hosts file and the username, hostname, and port into ~.ssh/config. Then in a terminal, I just have to enter "ssh <hostname>" and I'm logged in. To further shortcut this, I put an alias into my ~/.bash_aliases file that does the ssh part, so I just type the hostname.
Example:
My server has a hostname of "server", a static IP of 192.168.1.20, and uses port 4459 for ssh. My username on the server is "overlord".
The .ssh/confg entry looks like:
Host server
Port 4599
User overlord
HostName server
the /etc/hosts entry looks like this:
192.168.1.20 server.mylan server
and the ~/.bash_aliases entry looks like this:
alias server="ssh server"
When I want to log into the server and run an update, simply typing "server" into konsole and I'm logged in and ready to work. Works like a charm.
HOWEVER, some of the machines use wifi and DHCP to connect to the network and so I don't know their IP addresses without checking. The above only works if you know the IP. I could configure them to have fixed IP's but then when they wonder out of the house - to school or Starbucks - they can't connect or need to be re-configured again. I looked at adding zeroconf or other things, but then I'd have to do it to ALL the laptops. There had to be a way to do it with existing tools.
My solution: The arp -a command will print all the hostnames on the network - assume they're configured properly (mine are) with their IPs. So I combined arp with grep, awk, and tr and put it all in my .bash_aliases file.
Example - one of the wireless computers has "Asus" in it's hostname:
arp -a lists all the hostnames and IP's in this format: Asus-CN60 (192.168.1.99) at c3:45:42:69:30:20 [ether] on enp0s31f6
grep Asus lists only the line with the above info
awk '{print $2}' prints the second field from the above line: (192.168.1.99)
tr -d '()' strips the parenthesis and leaves just the IP address
Put them all together with an alias and here's the command in .bash_aliases:
alias asus="ssh -p 4499 `arp -a |grep Asus | awk '{print $2 }' | tr -d '()'`"
Now with one word I can log into that machine using ssh by just typing "asus".
Instead of spending half a day installing and configuring otherwise unneeded tools on all the wifi machines, this command took me less than 2 minutes to perfect and put into place.
I'm sure there are "neater" solutions or maybe even the above could be done better - but it works!
I have 6 Linux machines on my home network. I don't want to hunt them all down when updates or service is needed, so I use SSH to log into them remotely. I use a private key to avoid password entry and I use non-standard ssh ports to a tad extra security. For my static IP machines (if it has a wired connection, I configure it statically) I simply put the hostname and IP into my /etc/hosts file and the username, hostname, and port into ~.ssh/config. Then in a terminal, I just have to enter "ssh <hostname>" and I'm logged in. To further shortcut this, I put an alias into my ~/.bash_aliases file that does the ssh part, so I just type the hostname.
Example:
My server has a hostname of "server", a static IP of 192.168.1.20, and uses port 4459 for ssh. My username on the server is "overlord".
The .ssh/confg entry looks like:
Host server
Port 4599
User overlord
HostName server
the /etc/hosts entry looks like this:
192.168.1.20 server.mylan server
and the ~/.bash_aliases entry looks like this:
alias server="ssh server"
When I want to log into the server and run an update, simply typing "server" into konsole and I'm logged in and ready to work. Works like a charm.
HOWEVER, some of the machines use wifi and DHCP to connect to the network and so I don't know their IP addresses without checking. The above only works if you know the IP. I could configure them to have fixed IP's but then when they wonder out of the house - to school or Starbucks - they can't connect or need to be re-configured again. I looked at adding zeroconf or other things, but then I'd have to do it to ALL the laptops. There had to be a way to do it with existing tools.
My solution: The arp -a command will print all the hostnames on the network - assume they're configured properly (mine are) with their IPs. So I combined arp with grep, awk, and tr and put it all in my .bash_aliases file.
Example - one of the wireless computers has "Asus" in it's hostname:
arp -a lists all the hostnames and IP's in this format: Asus-CN60 (192.168.1.99) at c3:45:42:69:30:20 [ether] on enp0s31f6
grep Asus lists only the line with the above info
awk '{print $2}' prints the second field from the above line: (192.168.1.99)
tr -d '()' strips the parenthesis and leaves just the IP address
Put them all together with an alias and here's the command in .bash_aliases:
alias asus="ssh -p 4499 `arp -a |grep Asus | awk '{print $2 }' | tr -d '()'`"
Now with one word I can log into that machine using ssh by just typing "asus".
Instead of spending half a day installing and configuring otherwise unneeded tools on all the wifi machines, this command took me less than 2 minutes to perfect and put into place.
I'm sure there are "neater" solutions or maybe even the above could be done better - but it works!
Comment