Abu Hurairah reported,

The Prophet ﷺ said, "An ifrit from the jinn sprang at me last night," or words to that effect, "trying to interrupt my prayer, but Allah gave me power over him. I wanted to tie him to one of the pillars of the mosque so that you could all see him in the morning, but then I remembered the words of my brother Sulayman, 'My Lord, grant me a kingdom such as shall not belong to anyone after me' [Sad 38:35]."Rawh said, "So He sent him away humiliated."

Bukhari 461

Sahih

حدثنا إسحاق بن إبراهيم قال أخبرنا روح ومحمد بن جعفر عن شعبة عن محمد بن زياد

عن أبي هريرة

عن النبي ﷺ قال إن عفريتا من الجن تفلت علي البارحة أو كلمة نحوها ليقطع علي الصلاة فأمكنني الله منه فأردت أن أربطه إلى سارية من سواري المسجد حتى تصبحوا وتنظروا إليه كلكم فذكرت قول أخي سليمان رب {هب لي ملكا لا ينبغي لأحد من بعدي} قال روح فرده خاسئا

البخاري 461صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Bukhari
Book
Prayer
Chapter
Captive or debtor being tied up in the masjid
Numbering
Darussalam 461
Bukhari 461