Changing account passwords in a script

Is it safe to change account passwords using just the UNIX passwd command instead of the Cpanel interface or a Cpanel script?

I have a number of accounts with very weak passwords, and I want to write a script to change them all. I am thinking of using a password generator (like apg) and feeding the new passwords in through stdin like this: