Mishkat al-Masabih 4118
Abus Sa’ib said:
We went in to visit Abu Sa'id al-Khudri, and while we were sitting we heard a movement under his couch. When we had looked and found a snake in it I jumped up to kill it, but Abu Sa'id who was engaged in prayer made a gesture to me indicating that I should sit down, so I sat down. When he finished he pointed to a room in the house and asked if I saw it, and when I said I did he told me that a servant of theirs who had recently been married had occupied it. They went out with God’s messenger to the Trench, and that young man was asking permission from God’s messenger to leave after staying half the day and would return to his wife. One day when he asked God's messenger’s permission he told him to carry his weapons with him as he feared Quraiza might do him harm. The man took his weapons and returned, and finding his wife standing between the two doors he was smitten with jealousy and made to pierce her with his lance, but she told him to put away his lance and enter the house to see what had made her come out. He entered and found a huge snake coiled on the bedding, so he made for it with the lance and pierced it with it and then went out and fixed it in the ground in the house, but the snake quivered and attacked him, and no one knew which of them died first, the snake or the young man. They went to God's messenger and mentioned that incident to him, asking him to supplicate God to restore him to life for them, but he replied, "Ask forgiveness for your friend." Then he said, "These houses have resident jinn, so when you see one of them give it a warning three times. 1 If it goes away, well and good; otherwise kill it, for it is an infidel.'' He then told them to go and bury their friend. A version reports him as saying, ''In Medina there are jinn who have accepted Islam, so when you see one of them pronounce a warning to it for three days and if it appears to you after that kill it, for it is only a devil." 
1. Hiarrijiu ‘alaiha thalithan . The verb means to make things difficult. This phrase is explained as meaning that one should tell the snake three times that it will be in difficulties if it returns, and that it must not blame one if, after this warning, it is driven away and killed.

Muslim transmitted it.
وَعَن أبي السَّائِب قَالَ: دَخَلْنَا عَلَى أَبِي سَعِيدٍ الْخُدْرِيِّ فَبَيْنَمَا نحنُ جلوسٌ إِذ سمعنَا تَحت سَرِيره فَنَظَرْنَا فَإِذَا فِيهِ حَيَّةٌ فَوَثَبْتُ لِأَقْتُلَهَا وَأَبُو سَعِيدٍ يُصَلِّي فَأَشَارَ إِلَيَّ أَنِ اجْلِسْ فَجَلَسْتُ فَلَمَّا انْصَرَفَ أَشَارَ إِلَى بَيْتٍ فِي الدَّارِ فَقَالَ: أَتَرَى هَذَا البيتَ؟ فَقلت: نعم فَقَالَ: كَانَ فِيهِ فَتًى مِنَّا حَدِيثُ عَهْدٍ بِعُرْسٍ قَالَ: فَخَرَجْنَا مَعَ رَسُولِ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ إِلَى الْخَنْدَقِ فَكَانَ ذَلِكَ الْفَتَى يَسْتَأْذِنُ رَسُولَ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ بِأَنْصَافِ النَّهَارِ فَيَرْجِعُ إِلَى أَهْلِهِ فَاسْتَأْذَنَهُ يَوْمًا فَقَالَ لَهُ رَسُولُ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ: «خُذْ عَلَيْكَ سِلَاحَكَ فَإِنِّي أَخْشَى عَلَيْكَ قُرَيْظَةَ» . فَأَخَذَ الرَّجُلُ سِلَاحَهُ ثُمَّ رَجَعَ فَإِذَا امْرَأَتُهُ بَيْنَ الْبَابَيْنِ قَائِمَةٌ فَأَهْوَى إِلَيْهَا بِالرُّمْحِ لِيَطْعَنَهَا بِهِ وَأَصَابَتْهُ غَيْرَةٌ فَقَالَتْ لَهُ: اكْفُفْ عَلَيْكَ رُمْحَكَ وَادْخُلِ الْبَيْتَ حَتَّى تَنْظُرَ مَا الَّذِي أَخْرَجَنِي فَدَخَلَ فَإِذَا بِحَيَّةٍ عَظِيمَةٍ مُنْطَوِيَةٍ عَلَى الْفِرَاشِ فَأَهْوَى إِلَيْهَا بِالرُّمْحِ فَانْتَظَمَهَا بِهِ ثُمَّ خَرَجَ فَرَكَزَهُ فِي الدَّارِ فَاضْطَرَبَتْ عَلَيْهِ فَمَا يُدْرَى أَيُّهُمَا كَانَ أَسْرَعَ مَوْتًا: الْحَيَّةُ أَمِ الْفَتَى؟ قَالَ: فَجِئْنَا رَسُولَ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ وَذَكَرْنَا ذَلِكَ لَهُ وَقُلْنَا: ادْعُ اللَّهَ يُحْيِيهِ لَنَا فَقَالَ: «اسْتَغْفِرُوا لِصَاحِبِكُمْ» ثُمَّ قَالَ: «إِنَّ لِهَذِهِ الْبُيُوتِ عَوَامِرَ فَإِذَا رأيتُم مِنْهَا شَيْئا فحرِّجوا عَلَيْهَا ثَلَاثًا فإنْ ذَهَبَ وَإِلَّا فَاقْتُلُوهُ فَإِنَّهُ كَافِرٌ» . وَقَالَ لَهُمْ: «اذْهَبُوا فَادْفِنُوا صَاحِبَكُمْ» وَفِي رِوَايَةٍ قَالَ: «إِنَّ بالمدينةِ جِنَّاً قد أَسْلمُوا فَإِذا رأيتُم مِنْهُم شَيْئًا فَآذِنُوهُ ثَلَاثَةَ أَيَّامٍ فَإِنْ بَدَا لَكُمْ بَعْدَ ذَلِكَ فَاقْتُلُوهُ فَإِنَّمَا هُوَ شَيْطَانٌ» . رَوَاهُ مُسلم