Amazon for the older one for the PS2, Amazon or a game shop for the composite cable. If you're running from a TV, this is the cable you need for the PS2:
And this is the one you need for the PS3:
That composite cable for PS3 also works with PS2 (the YPbPr component cable (with 5 prongs) is HD, the PS2 one isn't) . This obviously requires that you have these ports on your TV. The three prong PS2 port is not in the back of my TV, but the side for some reason.
If you're running from a PC monitor and legit only have one HDMI port in the back of it, you'll need converters and splitters as well as cables.
So for PS2, 3 and 4, you'll need a 3 or 4-way HDMI splitter, an aftermarket PS2-HDMI converter (and a compatible cable for the PS2 that attaches to it), and HDMI cables for the PS3 and PS4 (which I assume you own). The actual switching between them is done on the device itself. Something like this:
My TV thankfully has a shitload of ports, so I actually have cables for every console I own connected to it even though the consoles themselves are sitting on shelves not plugged in at the moment (I recently redecorated and haven't had to use anything that's not my PS4 or PC since then). So I've got composite cables for the PS2, component for PS3, HDMI for the PC and PS4, Scart for a PSOne and the very old style RF-cable (for analogue TV antennae - these have been discontinued in the UK for a long time now) for a Dreamcast. All of these are plugged in to the TV so that when I want to play a console it's just a matter of turning it on and switching the channel.
One other thing I'll say is that if you're going to plug all the consoles in at the same time, get a surge-protected bar socket (bar sockets are also known as extension leads). In the UK they look like this:
This will save your devices from fusing (although tbh I'm not sure how non-UK electronics work in that respect, there's no actual protection for grounding in your two-prong plugs and everything is in danger of exploding at any moment).