Reputation: 3083
I have to get the hostname for a network device out of its config file. The file looks like:
...
hostname=T14Z18
ipaddress=192.168.0.1
...
How does one do that? I am ssh'ing into the machine.
Upvotes: 3
Views: 90
Reputation: 185025
With gnu-grep if -P
pcre regex switch is implemented :
grep -oP 'hostname=\K.*' configfile
__
^
|
restart the match trick
Upvotes: 2
Reputation: 12383
Use grep
and cut
like that:
$ grep '^hostname=' configfile | cut -d= -f2
T14Z18
You can also combine the above with ssh
command in Bash
:
$ ssh "$(grep '^hostname=' configfile | cut -d= -f2)"
Everything between $(
and )
will be replaced by the output of the command inside the parenthesis so it would be the same as if you typed ssh T14Z18
manually.
This feature is called command
substitution
in Bash
.
Also notice that OpenSSH
that you probably use has its own config
stored in ~/.ssh/config
that you can use to create aliases. For
example, the following entry creates an alias called rpi
:
Host rpi
User pi
Hostname 192.168.1.161
You can now just do ssh rpi
and user and hostname will be found
automatically by OpenSSH
client. You can of course use a hostname such as T14Z18 is in your example instead of IP address if you have DNS
server in your network.
Upvotes: 3
Reputation: 92854
With simple awk
command:
awk -F'=' '$1 == "hostname"{ print $2; exit }' configfile
Upvotes: 3