Sayyiduna Abdullah bin Mas'ud (may Allah be pleased with him) narrated: We were with the Prophet (peace and blessings be upon him) in a cave when Surah "Wal-Mursalat" was revealed to him. We were learning these verses from his mouth when a snake came out at us. The Messenger of Allah (peace and blessings be upon him) said: "Kill it." We rushed towards it, then it went into its hole. Then the Messenger of Allah (peace and blessings be upon him) said: "It has been protected from your harm just as you have been protected from its harm."
Hadith Referenceالمعجم الكبير للطبراني / باب العين / 10123