我正在尝试完成我的 bash 脚本,以便找到 LAN 上的哪些 SSH 服务器仍然使用 ssh 密钥处于活动状态,但我在这一步被冻结了:
#!/bin/bash
# LAN SSH KEYS DISCOVERY SCRIPT
</etc/passwd \
grep /bin/bash |
cut -d: -f6 |
sudo xargs -i -- sh -c '
[ -e "$1" ] && cat "$1"
' -- {}/.ssh/known_hosts |
cut -d' ' -f1 |
tr ',' '\n' |
sed '
/^\[/{
s/\[\(.*\)\]:\(.*\)/\1 \2/;
t;
};
s/$/ 22/;
' |
sort -u |
xargs -l1 -- sh -c '
if echo "~" | nc -q1 -w3 "$1" "$2" | grep -q "^SSH"; then
echo "#### SUCCESS $1 $2";
else
echo "#### ERROR $1 $2";
fi
' --
# TIME TO TEST WHICH SSH servers are still active with the SSH keys
# AND THIS IS WHERE I AM FROZEN...
# Would love to have bash script that could
# ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers
# Would also be very nice if it could save active
# SSH servers with the valid keys in output.txt in the format
# username:local-IP:/path/to/SSH_key
还有另一种更容易阅读(但创建了一些临时文件):
#!/bin/bash
# LAN SSH KEYS DISCOVERY SCRIPT
# TRYING TO FIND THOSE SSH KEYS NOW
cat /etc/passwd | grep /bin/bash > bash_users
cat bash_users | cut -d ":" -f 6 > cutted.bash_users_home_dir
for bash_users in $(cat cutted.bash_users_home_dir)
do
ls -al $bash_users/.ssh/*id_* >> ssh-keys.txt
done
# DISCOVERING THE KNOWN_HOSTS NOW
for known_hosts in $(cat cutted.bash_users_home_dir)
do
cat $bash_users/.ssh/known_hosts | awk '{print $1}' | sort -u >>
hosts_known.txt
sleep 2
done
hosts_known=$(wc -l hosts_known.txt)
echo "We have $hosts_known known hosts that could be still active via SSH
keys"
# TIME TO TEST WHICH SSH servers are still active with the SSH keys
# AND THIS IS WHERE I AM FROZEN...
# Would love to have bash script that could
# ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers
# Would also be very nice if it could save active
# SSH servers with the valid keys in output.txt in the format
# username:local-IP:/path/to/SSH_key
所以现在,正如上面的 bash 脚本中所述,我正在尝试找到如何执行某种操作ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers
并将积极登录转储到输出.txt文件格式如下:
username:local-IP:/path/to/SSH_key
回答多普·戈蒂评论:
我现在正在尝试弄清楚如何将其集成到我的 bash 脚本中。这是我目前的尝试:
users=$(cat cutted.bash_users)
ssh_keys=$(cat ssh-keys.txt)
if ssh -l $users -i $ssh_keys $hosts_known -o BatchMode=yes -o
ConnectTimeout=5
then echo $users:$hosts_known:$ssh_keys SUCCESS >> results.txt
else
echo FAILED
fi
答案1
不幸的是,我似乎无法添加评论(需要 50 声望点),这就是我向你们添加我的回复的原因。另外,我之前编辑过我的问题,但收到了此通知:
您建议的编辑正在等待审核。 Patrick Mevzek 于 2 小时前评论了此内容:拒绝此编辑旨在针对该帖子的作者,作为编辑没有任何意义。它应该写成评论或答案。
欢迎来到 U&L,您知道 ssh 有返回码吗?与 -o BatchMode=yes 一起使用可以帮助自动化测试。 (例如,如果 ssh ... ;则 echo good for ... >> good.txt ;否则 echo bad for ... >> bad.txt ; fi) – Archemar 54 分钟前 还有 cat|grep|cut|for可以归结为 for homedir in $(awk -F: '$7 ~ //bin/bash/ { print $6 }' /etc/passwd);做.... – DopeGhoti 1 分钟前
所以只是为了澄清:您想要做的是,对于每个用户,对于其known_hosts中的每个主机,尝试使用~username/.ssh中找到的每个假定私钥登录到每个主机? – DopeGhoti 25 分钟前 : 是的
这里;
所以现在我正在尝试弄清楚如何将这个想法集成到我的 bash 脚本中,例如:
users=$(cat cutted.bash_users)
cat ssh-keys.txt | awk '{print $9}' > cutted.ssh_keys.txt
ssh_keys=$(cat cutted.ssh_keys.txt)
if ssh -l $users -i $ssh_keys $hosts_known -o BatchMode=yes -o ConnectTimeout=5
then echo $users:$hosts_known:$ssh_keys SUCCESS >> results.txt
else
echo $users:$hosts_known:$ssh_keys FAILED >> not-active.txt
fi
如果你们知道如何实现这一点,我将非常乐意学习如何做到这一点
谢谢