Once you can PING apple.com, your next step might be to back up your Mac, and then work on upgrading macOS.
How to download and install macOS - Apple Support
Mountain Lion is so old that the version of Safari running on it probably won't be able to access this Support article, and the version of the App Store probably couldn't use the App Store links for High Sierra / Mojave / Catalina even if by some miracle Safari worked.
You're going to want to enlist the aid of a modern Mac or PC.
- Use the modern Mac or PC to download the .DMG file containing the Sierra installer onto a USB drive (a portable HDD or SSD, or a USB flash drive). This will not create a bootable installer, just a drive with a data file on it. If the drive is Mac-formatted, use HFS+ – not APFS. APFS didn't exist in Mountain Lion's day and Mountain Lion won't be able to make heads or tails of it.
- Transfer the USB drive to the old iMac. Copy the .DMG file to the iMac, then eject and unplug the drive.
- Open (mount) the .DMG file and run the Sierra installer application inside.
Once you have successfully installed Sierra, the old Mac should have versions of Safari and the App Store that are new enough for you to be able to directly access the Apple Support article from the old Mac itself. Then you can do the next upgrade, to High Sierra, Mojave, or Catalina. (You may want to make a bootable clone backup of the Sierra installation first, just to have something to fall back on in case something goes wrong during the final upgrade, or at a future date.)