3

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.

MoreScratch
  • 2,933
  • 6
  • 34
  • 65
  • BTW, this is potentially very much related to [Reading key/value parameters from a file into a shell script](https://stackoverflow.com/questions/38964946/reading-key-value-parameters-from-a-file-into-a-shell-script) -- the only difference being that the answers there typically read the *whole* file, whereas here you're trying to extract just a single item. If you plan to read more than just a few items, it'll probably be much more efficient to go with a single-pass approach, as in the answers there. – Charles Duffy Mar 11 '18 at 16:16

3 Answers3

3

With simple awk command:

awk -F'=' '$1 == "hostname"{ print $2; exit }' configfile
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
3

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.

Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
  • Better to quote that: `ssh "$(grep '^hostname=' configfile | cut -d= -f2)"` -- that way if you don't get exactly one word out, the error message will make sense (rather than additional words being treated as parts of a command to send to the remote machine, or 0-words passing the first subsequent argument to ssh as the hostname). – Charles Duffy Mar 11 '18 at 16:08
2

With - if -P regex switch is implemented :

grep -oP 'hostname=\K.*' configfile
                   __
                    ^
                    |

restart the match trick

Support of \K in regex

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 1
    Should probably specify "with GNU `grep`, when compiled with optional PCRE support" to be a little more up-front about the portability constraints. – Charles Duffy Mar 11 '18 at 16:18
  • Post edited. It's not PCRE but perl regex to be more precise – Gilles Quénot Mar 11 '18 at 16:30
  • PCRE == "Perl-Compatible Regex Engine". Compiling `grep` with such support links in the 3rd-party library in libpcre at compile time, so I believe describing it as "PCRE" is entirely appropriate here. – Charles Duffy Mar 11 '18 at 17:27
  • The `man grep` is not _that_ clear, it says _perl regex_ and PCRE, that is not the same – Gilles Quénot Mar 11 '18 at 17:32
  • `grep -P` is implemented by `pcresearch.c` in the codebase. See http://git.savannah.gnu.org/cgit/grep.git/tree/src/pcresearch.c for the source. It's *absolutely* PCRE. – Charles Duffy Mar 11 '18 at 18:00
  • You are picky, thanks for your search ;) I wouldn't wrote in `man grep` _perl regex_, but _PCRE regex_, though. What I meant, is that PCRE is a subset of perl's regex – Gilles Quénot Mar 11 '18 at 18:07