A chemistry teacher who tried to protect his students by opening fire on Taliban militants during a deadly attack at the Bacha Khan University has been hailed as a "martyr" and a "gentleman".Lecturer Syed Hamid Husain, an assistant professor of chemistry at the Bacha Khan University in Charsadda, ordered his students to stay inside as Taliban gunmen stormed the school on Wednesday, leaving at least 21 people dead.Students told of how he opened fire on gunmen as they rampaged across campus, giving the young people time to flee before he was cut down in a hail of bullets."We saw three terrorists shouting, 'Allah is great!' and rushing towards the stairs of our department," one man told reporters."One student jumped out of the classroom through the window. We never saw him get up."He described seeing Husain holding a pistol and firing at the attackers. "Then we saw him fall down and as the terrorists entered the (registrar) office we ran away."Geology student Zahoor Ahmed said Husain had warned him not to leave the building after the first shots were fired. "He was holding a pistol in his hand," he said."Then I saw a bullet hit him. I saw two militants were firing. I ran inside and then managed to flee by jumping over the back wall.""They fired directly at" the professor, sociology student Muhammad Daud told AFP, describing Husain as "a real gentleman and a respectable teacher".Tributes were paid to the slain academic on Twitter."Martyr of #education: Prof Hamid who was killed by terrorists in #BachaKhanUniversity#Pakistan," tweeted journalist and academic Raza Ahmad Rumi.Another message on the site read: "Very #sad news Prof Dr Syed Hamid from Chemistry dept embraced shahadat (martyrdom) in an attack in #BachaKhanUniversity#Charsadda."President Mamnoon Hussain confirmed the lecturer had died, and expressed his grief and condolences to the man's family.Security officials said an operation to halt the bloodshed at the school ended several hours after it began, with police saying at least 21 people had been killed.The military said it had killed four extremists. It was not clear if they were included in the death toll. The Taliban have claimed responsibility for the attack.Teachers in Khyber-Pakhtunkhwa were given permission to carry firearms in the classroom after Taliban militants massacred more than 150 people, the majority of them children, at a school in the city of Peshawar in 2014.The attack on an army-run school in the city, some 50 kilometres from Charsadda, was the deadliest in Pakistani history, and saw heavily armed militants go from room to room slaughtering students and staff. Teachers' associations had objected to arming staff, saying it was not their job to fight off militants.