Abdullah bin Masud reported,

We were with the Prophet ﷺ in a cave when the surah "By the winds sent forth" [Mursalat 77:1] was revealed to him. We were learning it directly from his lips, fresh as it came down, when suddenly a snake appeared before us.He said, "Kill it!"We rushed at it to kill it, but it escaped us.The Messenger of Allah ﷺ then said, "Allah has spared it from your harm just as He has spared you from its harm."

Muslim 5835

Sahih

حدثنا يحيى بن يحيى وأبو بكر بن أبي شيبة وأبو كريب وإسحق بن إبراهيم واللفظ ليحيى قال يحيى وإسحق أخبرنا و قال الآخران حدثنا أبو معاوية عن الأعمش عن إبراهيم عن الأسود

عن عبد الله قال

كنا مع النبي ﷺ في غار وقد أنزلت عليه والمرسلات عرفا فنحن نأخذها من فيه رطبة إذ خرجت علينا حية فقال اقتلوها فابتدرناها لنقتلها فسبقتنا فقال رسول الله ﷺ وقاها الله شركم كما وقاكم شرها

مسلم 5835صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Muslim
Book
Peace
Chapter
Killing snakes and other creatures
Numbering
Darussalam 5835
Muslim 5835