Long-distance travel on cargo ships basically has to be booked in advance, and paid for handsomely, these days. It wouldn't probably be possible to find a ride just by asking around a port, as might still be the case from Zanzibar to the Comoros and on to Madagascar.
This site might give you some ideas of costs and possiblities!