【发布时间】:2021-03-13 18:04:50
【问题描述】:
我使用的是 kitty 终端模拟器,所以当我连接到新服务器时,我(通常)需要添加 terminfo(至少,这种方式似乎可以工作)。为此,我编写了一个脚本。当我在做的时候,我添加了一些代码来添加一个公钥,如果用户想要的话。 与问题无关,但这里是代码:
#!/bin/bash
host=$1
ip=$(echo $host | cut -d@ -f2 | cut -d: -f1)
# Check if it is a unknown host
if [[ -z $(ssh-keygen -F $ip) ]]; then
# Check if there are any ssh-keys
if [ $(ls $HOME/.ssh/*.pub > /dev/null | wc -l) -ne 0 ]; then
keys=$(echo $( (cd $HOME/.ssh/ && ls *.pub) | sed "s/.pub//g" ))
ssh -q -o PubkeyAuthentication=yes -o PasswordAuthentication=no $host "ls > /dev/null 2>&1"
# Check if the server has one of the public keys
if [ $? -ne 0 ]; then
echo "Do you want to add a SSh key to the server?"
while true; do
read -p " Choose [$keys] or leave empty to skip: " key
if [[ -z $key ]]; then
break
elif [[ -e $HOME/.ssh/$key ]]; then
# Give the server a public key
ssh $host "mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo \"$(cat $HOME/.ssh/$key.pub)\" >> ~/.ssh/authorized_keys"
break
else
echo "No key with the name \"$key\" found."
fi
done
fi
fi
# Copy terminfo to server
ssh -t $host "echo \"$(infocmp -x)\" > \"\$TERM.info\" && tic -x \"\$TERM.info\" && rm \$TERM.info"
fi
这不是最好的代码,但它似乎工作。当然欢迎提示。
问题是我每次连接新的远程服务器时都需要运行这个脚本(或者我需要跟踪哪个服务器是新的,但这更糟)。有没有办法在我每次连接到服务器时运行这个脚本(脚本检查 ip 是否是已知主机)。 还是有其他方法可以做到这一点?添加公钥很不错,但不是很重要。
希望有人能帮忙, 谢谢!
【问题讨论】: