Telnet is obsolete because it’s insecure, an attacker that is eavesdropping your network traffic can easily steal your credentials, because telnet it’s a text-based protocol without encryption, your username and password will be sent in clear text over the network. If it’s available, always use SSH! (/linux/ssh-auto-login-on-linux-and-unix/), but if you must use telnet create and use the script from below.
Tested operating system: Debian 9.1, CentOS 7.3, FreeBSD 11.1 and macOS 10.12
1. Check where is Expect installed, on FreeBSD 11.1 the PATH is /usr/local/bin/expect.
whereis expect
2. Create telnet.sh script and modify the username and password
#!/usr/bin/expect -f if {[llength $argv] < 1} { puts "Usage: ./telnet.sh host"; exit 1; } set timeout 10 set host [lindex $argv 0] set user "my_user" set password "my_password" spawn telnet -l $user $host expect { "?ser*" { send "$user\n" exp_continue } "?ogin*" { send "$user\n" exp_continue } "?assword*" { send "$password\n" interact exit 0; } } exit 1
3. Change permission and execute.
chmod 700 telnet.sh
./telnet.sh host
Warning: Keep your scripts in a safe place! Anyone who has access to your scripts can steal your credentials!