简体   繁体   English

重新格式化 ssh-key 以导入 gcp 项目元数据

[英]Reformatting ssh-key to import into gcp project metadata

I want to add an ssh-key generated in a shell script to my gcp project metadata.我想将在 shell 脚本中生成的 ssh 密钥添加到我的 gcp 项目元数据中。 The problem is, that I don't really know how to format the generated key to be in the format which is need for the project metadata.问题是,我真的不知道如何将生成的密钥格式化为项目元数据所需的格式。 The ssh-key I have looks like this:我拥有的 ssh-key 如下所示:

ssh-rsa AAAAB3.... username

The format that is stated in the documentation is this: 文档中规定的格式是这样的:

username:ssh-rsa AAAAB3....

Is there a way to reformat the key within my shell script using echo and cat?有没有办法使用 echo 和 cat 重新格式化我的 shell 脚本中的密钥?

My best try is this: echo $USERNAME:$(cat ~/.ssh/id_rsa.pub) , but this still leaves the trailing username at the end.我最好的尝试是: echo $USERNAME:$(cat ~/.ssh/id_rsa.pub) ,但这仍然在最后留下尾随的用户名。

Assuming you are using bash, this should do the trick:假设您使用的是 bash,这应该可以解决问题:

# Use the following line to read the key from a file
# KEY_WITH_USERNAME=$(cat ~/.ssh/id_rsa.pub)

KEY_WITH_USERNAME="ssh-rsa AAAAB3.... username"
USERNAME=${KEY_WITH_USERNAME##* }

KEY_WITHOUT_USERNAME=${KEY_WITH_USERNAME%"$USERNAME"}

echo $USERNAME:$KEY_WITHOUT_USERNAME

Outputs:输出:

username:ssh-rsa AAAAB3....

See related questions about how to remove pre- or suffix from a string in Bash and how to split a string and get the final part .请参阅Bash 中有关如何从字符串中删除前缀或后缀以及如何拆分字符串并获取最后一部分的相关问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM