Yes, I know that there are other option, rsh, ssh, etc. that are better. However, those aren't always options. Some switches only use telnet, some OSes only have telnet enabled or available (i.e. a fresh install of AIX 6.1). This will explain how to automate telnet using both a hereto doc (using bash, though other shells may support it) and expect.
a hereto doc is in this form:
so, for telnet:
#!/bin/bash # Define variable HOST='server.domain.com' USER='root' PASS='hobo3' # Telnet telnet $HOST <<EOF echo "$USER" sleep 5 echo "$PASS" sleep 5 echo "cd /var/tmp" sleep 3 echo "> .test" EOF
so, this will send all the follow commands to telnet. Login, create a file and logout. All automated. This is not the most efficient, but can be more universal, as bash, and some other shells, support hereto docs.
using expect (This will also use a hereto doc, but is a bit more efficient):
#!/bin/bash # Define variable HOST='server.domain.com' USER='root' PASS='hobo3' expect <<EOF spawn telnet $HOST expect "login:" send "$USER\r" expect "$USER's Password:" send "$PASSWD\r" expect -re "\n# " send "chuser fsize=-1 root\r" expect -re "\n# " send "ulimit -f unlimited\r" expect -re "\n# " send "cd /var/scripts \r" expect -re "\n# " send "rm -f aix-*\r" expect -re "\n# " send "sleep 2\r" EOF
So, expect has a lot of features, for a better explanation of expect use man expect. The ones used here are spawn which spawns the process, expect which waits for the input to match your string, and send which sends output to the process. This allows you program for specific situations, and is faster than using the hereto doc with telnet, as you don't need to sleep between each command.