Hi Burak,
I recently did this same process on one of our lab management switches without any issue. There should be no difference when you run the script via telnet/SSH or through console. It sounds like that's the problem you're describing though - that the same script that works on console doesn't work through a Telnet/SSH.
What doesn't work about it? Are you unable to re-connect to the new IP after running the script? Or are you expecting to see the CLI prompt appear after the script runs - because you won't see that, you'll be kicked from the switch when the IP is unconfigured.
-Drew