Malik bin Abu Amir reported,

He used to hear the recitation of Umar bin Khattab from the house of Abu Jahm while Umar was leading the people in prayer in the mosque of the Messenger of Allah ﷺ.Kaab al-Ahbar said, "By the One who parted the sea for Musa, Suhayb told me that Muhammad, the Messenger of Allah ﷺ, never saw a town he intended to enter without saying when he caught sight of it, 'O Allah, Lord of the seven heavens and all that they overshadow, Lord of the seven earths and all that they carry, Lord of the devils and all that they lead astray, Lord of the winds and all that they scatter, we ask You for the good of this town, the good of its people, and the good of what is in it, and we seek refuge in You from its evil, the evil of its people, and the evil of what is in it.'"Kaab swore by the One who parted the sea for Musa that these were the supplications of Dawud whenever he saw the enemy.

Azami 11201

Sahih

عن مالك بن أبي عامر

أنه كان يسمع قراءة عمر بن الخطاب وهو يؤم الناس في مسجد رسول الله ﷺ من دار أبي جهم، وقال كعب الأحبار: والذي فلق البحر لموسى لأن صهيبا حدثني أن محمدا رسول الله ﷺ لم ير قرية يريد دخولها إلا قال حين يراها: اللهم رب السموات السبع وما أظللن، ورب الأرضين السبع وما أقللن، ورب الشياطين وما أضللن، ورب الرياح وما ذرين، فإنا نسألك خير هذه القرية وخير أهلها، ونعوذ بك من شرها، وشر أهلها، وشر ما فيها. وحلف كعب بالذي فلق البحر لموسى لأنها كانت دعوات داود حين يرى العدو.

الأعظمي 11201صحيح
Contents
Reference
Commentary
Ayat
Collection
Jami al-Kamil
Book
Supplications, remembrances, and prayer upon the chosen Prophet ﷺ
Chapter
Supplications for riding and travel
Section
What to say when seeing a town one wishes to enter
Numbering
Jami al-Kamil 11201 (2nd edition)
Azami 11201