KABUL, Afghanistan (AP) — Gunmen armed with machine guns and rocket-propelled grenades attacked an Indian Consulate in western Afghanistan on Friday, though its diplomatic staff escaped unharmed, police said.

At least three gunmen opened fire on the consulate from a nearby building in Herat, provincial police chief Abdul Sami Qatra said. Qatra said police and soldiers killed three assailants in a gunbattle that saw the building catch fire as authorities evacuated neighbors.

Syed Akbaruddin, a spokesman for India's Ministry of External Affairs, said a deployment of the paramilitary Indo-Tibetan Border Police at the consulate held off the assault until Afghan forces arrived. He said all the Indians at the consulate were safe.

"Our consulate and our diplomatic presence in Afghanistan have been under threat," Akbaruddin told TimesNow TV, without elaborating.

No group immediately claimed responsibility for the attack. However, a group called Ansar al-Tawhid that appears to be based in Afghanistan posted a video online May 17 that threatened India, according to the SITE Intelligence Group, which monitors extremist websites.

Herat lies near Afghanistan's border with Iran and is considered one of the safer cities in the country. In September 2013, Taliban gunmen launched a similar assault on the U.S. Consulate in the city, killing at least four Afghans but failing to enter the compound or hurt any Americans.

Foreign embassies and consulates remain a favorite target of insurgents in Afghanistan, but many are protected by high walls and multiple gates, as well as security forces.

India has invested more than $2 billion in Afghan projects, including roads and power projects. Afghan President Hamid Karzai spoke to India's Prime Minister-designate Narendra Modi after the assault and called it "an attack on Afghanistan, India and our shared interests," according to a statement.

But India remains a target. In August 2013, a botched bombing against the Indian Consulate in the Afghan city of Jalalabad near the border with Pakistan killed nine people, including six children. No Indian officials were hurt. Two attacks on the Indian Embassy in Kabul in 2008 and 2009 killed 75 people.

Groups known for targeting Indian interests include Lashkar-e-Taiba, which was blamed for the 2008 attack on the Indian city of Mumbai that killed 166 people, and the Haqqani network, which is based in Pakistan's lawless tribal areas along the border with Afghanistan.

Lashkar-e-Taiba has been active in Afghanistan in recent years, often teaming up with insurgent groups operating in the eastern part of the country near the frontier with Pakistan. In 2010, two Kabul guest houses popular among Indians were attacked, killing more than six Indians. India blamed that attack on the group.

Sameer Patil, a national security expert at the Mumbai-based think tank Indian Council on Global Relations, said it was likely that anti-India elements from Pakistan were behind the attack. India and Pakistan have fought three wars since their independence from Britain in 1947, though relations recently have thawed slightly between the two nuclear-armed countries.

"It is likely that by carrying out such violence, anti-India elements in Pakistan may be testing the resolve of India's new government, led by (Modi), on terrorism and how he responds to such acts of violence," Patil in a statement.

Pakistan condemned the attack in a statement, saying that "no cause justifies (the) targeting of diplomatic missions."

The violence comes as Afghans prepare for a second round of presidential elections on June 14. The first round was relatively peaceful, but no candidate won a majority forcing a runoff vote between the top two candidates — Abdullah Abdullah and Ashraf Ghani Ahmadzai.