A 25-year-old amateur surfer was being attacked by a shark off the coast of Australia.

Then in a moment of pain and panic, he remembered a move he learned online — and punched the shark on the nose to escape its grasp.

Charlie Fry, a British doctor who was in Australia for work, said he had seen a YouTube video of professional surfer Mick Fanning after the Australian escaped from a Great White in 2015, according to the Associated Press. Fanning told a reporter at the time that he punched the shark and got away during a competition on Jeffreys Bay in South Africa.

When the same thing happened to Fry on Monday, he told Nine Network’s “Today Show,” he thought of that viral video.

“I was like: ‘Just do what Mick did. Just punch it in the nose.’”

Fry was surfing with friends when he was attacked off the coast of Avoca Beach in New South Wales, not far from Sydney.

“I was out surfing and I got this massive thud on my right-hand side. It completely blindsided me,” he told Nine Network, according to the AP. “I thought it was a friend goofing around. I turned and I saw this shark come out of the water and breach its head.”

“So I just punched it in the face with my left hand and then managed to scramble back on my board, shout at me friends and luckily a wave came, so I just sort of surfed the wave in,” he added.

Fry said he sustained minor wounds to his right shoulder and upper arm, but he explained that he didn’t notice his injuries as he was frantically swimming toward the shore.

He told Australia’s “Today Show” that the only thing on his mind at the time was this: “I’m about to die — I’m literally about to die.”

“So I was just sort of surfing and going, ‘get in as fast as possible — ride the wave for as long as you can and then just start paddling for your life essentially,’” he said. “It was very hectic, very very hectic.”

Fry was treated for his injuries at a nearby hospital and was ordered to stay out of the ocean for about a week.

At that point, he told reporters, “I’ll be racing to get back in.”

One shark researcher told BBC News that the effectiveness of throwing a punch really depends on the shark’s intentions.

“The worst thing is to try to run away,” Ryan Johnson told the BBC. “It’s like throwing a stick for a dog. Fleeing often can entice a shark. … Standing your ground and trying to make yourself big and going vertical in the water is always the best response to make a shark keep its distance from you.”

But Johnson told the BBC that when a shark actually attacks, “you have to do everything in your power” to try to escape.

“Punching him in the face, going for his eyes, try to keep your hands out of his mouth,” he said, adding to use “something hard — be it a camera, a stick, a rock.”

“In a situation where you don’t have that, going for the face and the gills and trying to keep your hands out of its mouth is always the best thing,” Johnson said.

Following Fanning’s attack in 2015, he told the news media that he was “waiting for the teeth to come at me” as he fought to escape from the shark’s jaws.

“I was just sitting there, I was just about to start moving, and then I felt something grab, just get stuck in my leg rope,” he said at the time. “I instantly just jumped away, and then it just kept coming at my board. I was kicking and screaming.

“I just saw fins. I didn’t see the teeth. I was waiting for the teeth to come at me as I was swimming. I punched it in the back.”

Fry credits Fanning for showing him how it’s done.

“So Mick, if you’re watching or listening, I owe you a beer,” Fry said Monday. “Thank you very much.”

Read more: