
맥락에 따라 SSH를 사용하는 여러 시스템의 인프라가 있습니다. 각 mchine의authorized_keys 파일 덕분에 비밀번호 없이 머신의 루트로 ssh를 통해 연결합니다. 우리는 인프라에 정기적으로 새로운 시스템을 추가하고 있습니다.
문제는 다음과 같은 스크립트를 만드는 것입니다.
- 모든 머신에 ping을 보냅니다(모든 머신 이름이 포함된 파일을 구문 분석하여).
- ping이 성공하면 비밀번호 없이 SSH 연결을 테스트합니다(명령 사용
ssh -o BatchMode=yes $machine uname -a
). - SSH가 작동하지 않고 다음 메시지 때문인 경우
Are you sure you want to continue connecting (yes/no)?
(예를 들어 이 시스템에 대한 첫 번째 SSH 연결이기 때문에), 예상 스크립트를 사용하여 "yes"를 보냅니다. - SSH가 작동하지 않고 비밀번호를 요청했기 때문에 작동하지 않는 경우 예상 스크립트를 사용하여 "CTRL + C"를 보내십시오.
내 문제는 두 가지 조건 3과 4가 모두 하나의 컴퓨터에서 발생할 수 있고 내 스크립트에서 continue 문을 사용하는 방법을 알 수 없다는 것입니다.
이 특정 사례는 "예"를 요청한 후 비밀번호도 요청하는 시스템에 대한 것입니다.
스크립트는 다음과 같습니다.
for machine in `cat ${liste} | grep -v \#`
do
ping -c1 ${machine} 2>&1 >/dev/null
if [ $? -eq 0 ]
then
echo ${machine} >> ${pingok}
ssh -o BatchMode=yes ${machine} uname -a &> $verifssh 2>&1
echo $? > ${exitcode}
if grep -q "255" "$exitcode"
then
cut -c 15-74 $verifssh > $verifssh2
if grep "ication failed." "$verifssh2"
then
expect ${scriptexpectknownhosts} ${machine} 2>&1 >/dev/null
continue 3
elif grep "Permission denied (publickey,password,keyboard-interactive)." "$verifssh2"
then
expect ${scriptexpectknownhosts} ${machine} 2>&1 >/dev/null
echo "${machine} -> The machine asks for a password" >> "${sshnok}"
fi
elif grep -q "0" "$exitcode"
then
echo "${machine} works with ssh"
echo "${machine}" >> ${sshok}
fi
else
echo "${machine}" >> "${pingnok}"
fi
done
다음은 예상 스크립트입니다(두 가지 상황을 모두 처리합니다).
set machine [lindex $argv 0]
spawn ssh $machine
expect {
"Are you sure you want to continue connecting (yes/no)? " {send "yes\r";exp_continue}
-exact "Password: " {close}
-re $prompt {send "exit\r";close}
}
간단히 말해서 내 문제는 "예"라고 대답한 다음 암호가 필요한 컴퓨터의 경우 파일에 등록하고 싶지만 ${sshnok}
작동 continue
하지 않는다는 것입니다. continue
// 시도했지만 continue 2
여전히 continue 3
이전 루프로 돌아가고 싶지 않습니다.
답변1
의견에서 제안한 대로 나는 을 삭제했고 continue
여러 개 대신에 elif
몇 가지 진술을 더 수행했습니다 if
.
기계용cat ${liste} | grep -v \#
do
echo "."
ping -c1 ${machine} 2>&1 >/dev/null
if [ $? -eq 0 ]
then
echo ${machine} >> ${pingok}
ssh -o BatchMode=yes ${machine} uname -a &> $verifssh 2>&1
echo $? > ${exitcode}
if grep -q "255" "$exitcode"
then
cut -c 15-74 $verifssh > $verifssh2
if grep "ication failed." "$verifssh2"
then
expect ${scriptexpectknownhosts} ${machine} 2>&1 >/dev/null
fi
ssh -o BatchMode=yes ${machine} uname -a &> $verifssh 2>&1
cut -c 15-74 $verifssh > $verifssh2
if grep "Permission denied (publickey,password,keyboard-interactive)." "$verifssh2"
then
expect ${scriptexpectknownhosts} ${machine} 2>&1 >/dev/null
echo "${machine} -> Probleme de cle ssh (demande un mdp)" >> "${sshnok}"
fi
ssh -o BatchMode=yes ${machine} uname -a &> $verifssh 2>&1
echo $? > ${exitcode}
if grep -q "0" "$exitcode"
then
echo "${machine}" >> ${sshok}
fi
elif grep -q "0" "$exitcode"
then
echo "${machine}" >> ${sshok}
elif grep -q "1" "$exitcode"
then
echo "wtf 1"
fi
else
echo "${machine}" >> "${pingnok}"
fi
done
모든 답변에 진심으로 감사드립니다!