Telnet is a user command and an underlying TCP/IP protocol for accessing remote computers. Through Telnet, an administrator or another user can access someone else's computer remotely.
By default there is no option to disconnect the telnet session automatically. So we can not use this commands in scripts.
But we can use sleep command along with telnet and configure automatic session disconnect.
#sleep 5 | telnet server.example.com 25
The above command will disconnect automatically after 5 seconds. Please note that we need to modify the time limit according to our requirements.