You can look for the CAS ids in PubChem and their download files, but the data there can be pretty dirty (e.g. a mixture might have all the CAS ids listed). If you're lucky, then you can make the link via ChemSpider.
You can use an Entrez query to retrieve all PubChem compound (or substance) entries for a particular MeSH identifier. In this case you need to use the MeSH identifier that Entrez/NCBI uses, not the ones used by the National Library of Medicine MeSH download, which are different. For instance, for this small set of MeSH identifiers (in a shell script):
for i in 68001697 68020228 68020313 68045424 68006727 68007287 68004364; do curl "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi?dbfrom=mesh&id=$i&db=pccompound" > pchem_compound_with_mesh_$i.xml sleep 15 done;
(in a shell script, the
$i in the URL shown would be replaced by the things that you are iterating in the for using
You can see each mesh id accesing ncbi like this: http://www.ncbi.nlm.nih.gov/mesh/68001697