If so did they cooperate on any extent against the various Shahs of Iran? Did they maintain friendly, indifferent, or spiteful relations?
Yes, the Mughals and Ottomans certainly did know the other existed. We have evidence of direct contact between India and Anatolia dating back to 1000 BCE, and the trade routes across the Indian Ocean were well plied by Indian, Arab, Turkish, Persian, and Portuguese sailors by the rise of the Mughals in the 16th century. Specifically between the two Empires you mentioned, we have records of an Ottoman embassy sent to the 2nd Mughal Emperor Humayun in 1553. By all accounts relations were warm, if not too close. They were both fellow Turkic Muslim states, and they both shared mutual desires of trade and the spread of Islam. There were attempts an an alliance against the Portuguese in the late 16th century, but the Mughal's enthusiam for this venture was tepid to say the least. The Mughals found they had much more to gain through trading with the Portuguese than they did from expelling them from the continent.
As for specifically cooperating against the Persians, I'm unaware of anything too substantial. Until the rise of Nader Shah in the 18th century, Mughal-Safavid relations were close (other than the occasional dispute over territory and tribes in Afghanistan). Persia was revered by the Mughals in a similar way as to how the West revered Roman Empire, as a source of culture, religion, and ideas. Nader Shah broke these warm relations by taking advantage of the staggering and weakened state of the Mughals in the early 18th century. The Mughal Empire had been wracked with succession wars and rebellions since the death of Aurangzeb in 1707, and wasn't paying particularly close attention to developments in Persia as a consequence. Nader Shah, for his part, needed the wealth of India to finance his wars against the Ottomans and establish the empire he envisioned stretching from the Euphrates to the Indus. After his sack of Dehli in 1739, the Mughals attempted to coordinate with the Ottomans to contain Nader Shah, but this was largely thwarted by the instability and weakness of the Mughal Empire by this point in history. Shortly after the death of Nader Shah in 1746 his Persian Empire disintegrated, and the Mughal Empire followed suit shortly thereafter.