Shaking the ketchup bottle is a euphemism for male masturbation. The metaphor comes from both the vigorous shaking motion applied to a ketchup bottle, and the "splooging" of ketchup when you've shaken it for long enough. Note: If what comes out is actually ketchup coloured, then you should very likely consult a doctor.
Shaking the ketchup bottle definition by Urban Dictionary