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.

1
  • BTW, this is potentially very much related to 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. Commented Mar 11, 2018 at 16:16

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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).
3

With simple awk command:

awk -F'=' '$1 == "hostname"{ print $2; exit }' configfile

1 Comment

More efficient than grep | cut, and also has the effect of head -n 1, preventing multiple matches.
2

With - if -P regex switch is implemented :

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

restart the match trick

Support of \K in regex

6 Comments

Should probably specify "with GNU grep, when compiled with optional PCRE support" to be a little more up-front about the portability constraints.
Post edited. It's not PCRE but perl regex to be more precise
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.
The man grep is not that clear, it says perl regex and PCRE, that is not the same
grep -P is implemented by pcresearch.c in the codebase. See git.savannah.gnu.org/cgit/grep.git/tree/src/pcresearch.c for the source. It's absolutely PCRE.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.