Two teenagers waved to passengers on a rail platform as they clung on to a train underneath overhead electricity cables, a court heard.
Harris Ahmed, 18, and a 16-year-old boy were sentenced on Monday after posting video of themselves "train surfing" on YouTube.
They each pleaded guilty at north London's Highbury Corner Magistrates' Court to endangering the safety of a person conveyed by the railway.
British Transport Police said the footage, reported to them on October 31, showed youngsters on a train in Brentwood, Essex.
Prosecutor Chiram Mondal said: "This is an offence effectively called train surfing.
"The two defendants posted a video on YouTube where they were identified as jumping on a train and filming themselves riding between the two carriages.
"They are seen to wave to other people on the platform effectively as some form of stunt."
He pointed out that "the most danger was to the defendants themselves" as their arms were very close to the edge and beneath high voltage cabling which could fall down and electrocute them.
If the train had to stop quite suddenly it could have "a detrimental impact" on the passengers, Mr Mondal added.
Ahmed, a student, of Manor Park, east London, was given a 12-month community order, while the 16 year-old, who cannot be named for legal reasons, was given a four-month referral order.
They were each ordered to pay £105 in surcharge and court costs.
Ahmed has since deleted the video of the prank, the court heard.