Ata bin Abi Marwan (may Allah have mercy on him) narrates from his father (may Allah have mercy on him) that Ka'b (may Allah have mercy on him), swearing by the One who split the sea for Musa (peace be upon him), stated that Sayyiduna Suhayb (may Allah be pleased with him) told him: Whenever Muhammad (peace and blessings be upon him) saw a town that he intended to enter, he would recite this supplication as soon as he saw it: "O Allah, Lord of the seven heavens and all that they shade, Lord of the seven earths and all that they carry, Lord of the devils and all whom they lead astray, and Lord of the winds and all that they scatter! So we ask You for the good of this town and the good of its people, and we seek refuge in You from its evil, the evil of its people and the evil of all that is in it."
These are the words of the narration of Abu Ya'la al-Mawsili (may Allah have mercy on him).
And in the narration of Imam al-Tabarani (may Allah have mercy on him), Sayyiduna Suhayb (may Allah be pleased with him) told him that the Messenger of Allah (peace and blessings be upon him) (would recite this supplication), and his wording has "inna nas'aluka" (We ask You), and the rest of the supplication is the same.
Abu Hatim al-Busti (may Allah have mercy on him) narrated it from Muhammad bin al-Hasan bin Qutaybah, from Ibn Abi al-Sari, from Hafs bin Maysarah, with a similar meaning.