You should be using cmdGen.getCmd() call instead of nextCmd() call. There's no 'next' OID past the leaf one hence empty response.
Here's a bit optimized version of your code. It should run as-is right from your Python prompt:
from pysnmp.entity.rfc3413.oneliner import cmdgen
def getCounter(ip, community, mibName, counterName, indexNum=None):
if indexNum is not None:
mibVariable = cmdgen.MibVariable(mibName, counterName, int(indexNum))
else:
mibVariable = cmdgen.MibVariable(mibName, counterName)
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.getCmd(
cmdgen.CommunityData(community),
cmdgen.UdpTransportTarget((ip, 161)),
mibVariable.addMibSource("/path/to/mibs")
)
if not errorIndication and not errorStatus:
return varBindTable
#from pysnmp import debug
#debug.setLogger(debug.Debug('msgproc'))
print(getCounter('demo.snmplabs.com',
'recorded/linux-full-walk',
'HOST-RESOURCES-MIB',
'hrSWRunPerfMem',
970))
Performance wise, it's recommended to reuse CommandGenerator instance to save on [heavy] snmpEngine initialisation happening under the hood.