从脚本中中断 ssh 命令并将调试日志保存到文件中
Break ssh command from within a script and save debug log to a file
我正在尝试编写一个 bash 脚本,其中 运行s ssh 命令针对指定的 host/hosts 进行调试 (ssh -vvv
)。
我不是想登录服务器,而是想看看服务器提供的所有 kex、mac 和密码。
在使用 -G 选项协商后,我确实创建了一个脚本来查找 kex 算法。
#!/bin/bash
for f in `cat servers.txt`;
do echo "### $f ###";
echo -e "kexalgorithms"
result=$(ssh -G $f uname -a | grep kexalgorithms)
echo $result;
done
但是,我现在意识到这个输出依赖于本地 machine 上的 ssh 配置,不会给我远程服务器提供的确切 kex。
如果我执行 ssh -vvv <host>
它会在
行中提供此信息
debug2:对等服务器 KEXINIT 提案
debug2: KEX算法:curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2- nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256
debug2:主机密钥算法:rsa-sha2-512、rsa-sha2-256、ssh-ed25519
debug2:密码
但是,如果从脚本中 运行 ssh -vvv
它将卡在 username/password 提示符处。
有什么方法可以将标准输出保存到文件(可能以主机名作为文件名)并在密码提示时中断脚本 运行?
提前致谢
使用 nmap
从远程 ssh 服务器获取所有支持的密钥交换算法:
nmap --script ssh2-enum-algos -p 22 YOUR-SSH-SERVER \
| awk -v a='kex_algorithms:' '==a{getline; while( [=10=]~/^\| {7}/ ){ print ; getline }}'
输出(例如):
curve25519-sha256
curve25519-sha256@libssh.org
ecdh-sha2-nistp256
ecdh-sha2-nistp384
ecdh-sha2-nistp521
diffie-hellman-group-exchange-sha256
diffie-hellman-group16-sha512
diffie-hellman-group18-sha512
diffie-hellman-group14-sha256
禁用 PasswordAuthentication
和 PubkeyAuthentication
并使用 GNU grep
:
ssh -o PasswordAuthentication=no -o PubkeyAuthentication=no -vv YOUR-SSH-SERVER 2>&1 | grep -m 1 -Po 'KEX algorithms: \K.*'
输出(例如):
curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16
我正在尝试编写一个 bash 脚本,其中 运行s ssh 命令针对指定的 host/hosts 进行调试 (ssh -vvv
)。
我不是想登录服务器,而是想看看服务器提供的所有 kex、mac 和密码。
在使用 -G 选项协商后,我确实创建了一个脚本来查找 kex 算法。
#!/bin/bash
for f in `cat servers.txt`;
do echo "### $f ###";
echo -e "kexalgorithms"
result=$(ssh -G $f uname -a | grep kexalgorithms)
echo $result;
done
但是,我现在意识到这个输出依赖于本地 machine 上的 ssh 配置,不会给我远程服务器提供的确切 kex。
如果我执行 ssh -vvv <host>
它会在
debug2:对等服务器 KEXINIT 提案
debug2: KEX算法:curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2- nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256
debug2:主机密钥算法:rsa-sha2-512、rsa-sha2-256、ssh-ed25519 debug2:密码
但是,如果从脚本中 运行 ssh -vvv
它将卡在 username/password 提示符处。
有什么方法可以将标准输出保存到文件(可能以主机名作为文件名)并在密码提示时中断脚本 运行?
提前致谢
使用 nmap
从远程 ssh 服务器获取所有支持的密钥交换算法:
nmap --script ssh2-enum-algos -p 22 YOUR-SSH-SERVER \
| awk -v a='kex_algorithms:' '==a{getline; while( [=10=]~/^\| {7}/ ){ print ; getline }}'
输出(例如):
curve25519-sha256 curve25519-sha256@libssh.org ecdh-sha2-nistp256 ecdh-sha2-nistp384 ecdh-sha2-nistp521 diffie-hellman-group-exchange-sha256 diffie-hellman-group16-sha512 diffie-hellman-group18-sha512 diffie-hellman-group14-sha256
禁用 PasswordAuthentication
和 PubkeyAuthentication
并使用 GNU grep
:
ssh -o PasswordAuthentication=no -o PubkeyAuthentication=no -vv YOUR-SSH-SERVER 2>&1 | grep -m 1 -Po 'KEX algorithms: \K.*'
输出(例如):
curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16