The ticket has an Airline1 flight number for this leg and mentions "Operated by Airline2". I want to know the Airline2 flight number for this leg.
What you are looking for is called the prime code.
Flight Stats is usually useful for this. Simply enter the marketing carrier and marketing flight number.
For instance, on the page for AA 6770 you can see it is actually operated by BA under the code BA 836.
If you don't remember the link, you can Google your marketed flight number and Flight Stats usually comes up on the first page of the results.
Conversely, if I want to book this leg, how can I find out which airline(s) has(ve) code share seats on the flight operated by Airline2?
Whether a codeshare exists on a certain flight depends on the other flights you propose to book it in conjunction with. For instance, you cannot book BA 5330 (that's AA 3206) on its own, because for a foreign carrier to market standalone flights in the US would be in breach of Department of Transport regulations. However you can book it if you have a
British Airways-marketed transatlantic flight on the same ticket. Some codeshares only exist if you are connecting to another flight within a certain time period, for instance BA's codeshares on Loganair in Scotland require a connection within four hours.
There is actually no interface exposed to travel agents for this kind of search. The logic of codeshares is not available for inspection. In general the best way to find codeshares is to Google the prime flight number and have a dig through the results.
An alternative is to constrain a search on either the ITA Matrix or your favourite multicity search engine, such that only the leg you desire will match.