如何使用Shell脚本将服务器主机的详细信息自动添加到变量/文件中

huangapple go评论81阅读模式
英文:

How to add details automatically of server host into variable/file using shell script

问题

/home/user/.ssh/ 目录下的 known_host 文件中,我们可以获取主机服务器名称的所有详细信息。

例如 - hostname.abcs.com,129.167.29.11 ssh.rsa

Shell 脚本如下 -

#!/bin/bash
HOST=$1
IP_ADDR=$2
ENC_TYP=$3
KEY=$4
.........

然后需要将主机名详细信息添加到第一个变量(HOST),IP 地址添加到第二个变量,加密类型和加密密钥添加到变量中。例如 -

[] > cat HOST

然后输出为 hostname.abcs.com

[] > cat IP_ADDR

然后输出为 129.167.29.11

或者,我们也可以将所有这些详细信息添加到一个文件中,如 hostname.txt。因此,我需要在 shell 脚本中添加自动将服务器主机详细信息添加到文件或变量的功能。

英文:

In /home/user/.ssh/ known_host file we can get all details of host server name.

Ex - hostname.abcs.com,129.167.29.11 ssh.rsa <encryption-key>

Shell script like -

#!/bin/bash
HOST=$1
IP_ADDR=$2
ENC_TYP=$3
KEY=$4
.........

Then need to add hostname details in 1st variable (HOST), IP address in 2nd variable, encryption type & encryption key into variables. Ex-

[] > cat HOST

then output as hostname.abcs.com

[] > cat IP_ADDR

then output as 129.167.29.11

or we can also add all these details in 1 file as hostname.txt. So i need to add in shell script to add server host details automatically in file or in variable.

答案1

得分: 1

主机名可能位于第一列,但第一列也可能是@revoked@cert-authority,在这种情况下,主机名位于第二列。我们现在将忽略这个问题,并假设您没有包含这样的行。(如果有的话,您可以使用awk轻松地将它们过滤掉。)

如果您的文件的每一行的第一个条目都是name,ip_address的形式,您可以使用如下简单的方法:

#!/bin/bash

while IFS=' ,' read name ip_addr key_type b64_enc_key comment; do
    if ! test "$name" = "${name#@}"; then
        echo "WARNING: unexpected marker $name found" >&2
        continue
    fi
    # 更多的验证是个好主意
    echo host = "$name"
    echo ip = "$ip_addr"
    echo type = "$key_type"
    echo key = "$b64_enc_key"
done < known_hosts

但请注意,主机名字段可能只包含一个条目(即,它不包含任何逗号),或者可能包含多于2个条目(即,它包含多于1个逗号),在这种情况下,所有这些列计数将是错误的,并且字段将存储在错误的变量中。您应该比上面的示例更仔细地确保数据的有效性!

英文:

The hostnames may be in the first column, but the first column may also be @revoked or @cert-authority, in which case the hostnames is in the second column. We will ignore that issue for now, and assume you do not have any lines like that. (If you do, you can probably easily filter them out with awk.)

If your file is such that the first entry of every line is of the form name,ip_address, you could get away with something as simple as:

#!/bin/bash

while IFS=&#39; ,&#39; read name ip_addr key_type b64_enc_key comment; do
        if ! test &quot;$name&quot; = &quot;${name#@}&quot;; then
                echo &quot;WARNING: unexpected marker $name found&quot; &gt;&amp;2
                continue
        fi
        # more validation is a good idea
        echo host = &quot;$name&quot;
        echo ip = &quot;$ip_addr&quot;
        echo type = &quot;$key_type&quot;
        echo key = &quot;$b64_enc_key&quot;
done &lt; known_hosts

but note that the hostnames field may be only one entry (ie, it does not contain any commas) or it may be more than 2 (ie, it contains more than 1 comma) in which case all of these column counts will be wrong and the fields will be in the wrong variables. You should ensure the validity of the data much more carefully than is done above!

答案2

得分: 0

如果我正确理解您的问题,从输入行中读取由逗号或空格分隔的多个值,并将这些值放入单独的变量中的答案是使用下面的命令:

#!/bin/bash
while IFS=", " read HOST IP_ADDR ENC_TYP KEY 
  do 
    # 对输入文件中的每一行执行一些操作
    echo HOST=$HOST IP_ADDR=$IP_ADDR 等等
  done < /home/user/.ssh/known_host

只要您的文件/home/user/.ssh/known_host的内容与您所说的匹配。

英文:

If I understand your question correctly, the answer for reading several values, separated by a comma or a space, from an input line and put these in separate variables is using next command:

#!/bin/bash
while IFS=&quot;, &quot; read HOST IP_ADDR ENC_TYP KEY 
  do 
    # for each line in the input file, do something
    echo HOST=$HOST IP_ADDR=$IP_ADDR etc.
  done &lt; /home/user/.ssh/known_host

as far as input your file /home/user/.ssh/known_host matches what you said.

huangapple
  • 本文由 发表于 2023年3月10日 00:38:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定