Reading voltages from ngspice to another program
print is the command you need (in the control deck)
If you need to do some more complex things consider the tclspice extension, there is an optimization example in the manual similar to what you need