A Brief Discussion on the Method of Achieving Autotelnet Refinement - Network Protocol - Network Technology - Eden Network

by henxue on 2010-07-20 21:52:12

For Telnet, we can implement it in various ways. Here, we will introduce the implementation method of "autotelnet". For more specific content, please refer to the main text. Hopefully, this will be helpful to everyone.

---

### 1. Shell Implementation

File Name: **autotelnet.sh**

Code as follows:

```bash

(sleep 1; echo "root"; sleep 1; echo "123456"; sleep 1; echo "en"; sleep 1; echo "1qazse4"; sleep 1; echo "conf t"; sleep 1;

echo "int fa0/1"; sleep 1; echo "switchport mode multi"; sleep 1; echo "end"; sleep 1; echo "exit") | telnet 10.32.17.10

```

---

### 2. Expect Implementation

File Name: **autotelnet.exp**

Code as follows:

```tcl

#!/usr/bin/expect

set timeout 100

set TERM xterm

set SERVER "10.32.17.10"

set USER "root"

set PASSWD "123456"

spawn telnet

expect "telnet> "

send "open $SERVER\r"

expect "Username:"

send "$USER\r"

expect "Password:"

send "$PASSWD\r"

expect "longjiang-zero>"

send "en\r"

expect "Password:"

send "$PASSWD\r"

expect "longjiang-zero#"

send "conf t\r"

expect "longjiang-zero(config)#"

send "int fa0/1\r"

expect "longjiang-zero(config-if)#"

send "switchport mode multi\r"

expect "longjiang-zero(config-if)#"

send "end\r"

expect "longjiang-zero#"

send "exit\r"

interact

```

---

### 3. Python Implementation

File Name: **autotelnet.py**

Code as follows:

```python

#!/usr/bin/python

import telnetlib

host = '10.32.17.10'

user = 'root'

password = '123456'

commands = ['en', password, 'conf t', 'int fa0/1', 'switchport mode multi', 'end']

tn = telnetlib.Telnet(host)

tn.read_until("Username:")

tn.write(user + "\n")

tn.read_until("Password:")

tn.write(password + "\n")

for command in commands:

tn.write(command + '\n')

tn.write("exit\n")

print(tn.read_all())

print('Finish!')

```

---

### Reference Link:

[Eden Network Technology Column](http://www.edenw.com/tech/net/col/2010-07-20/4820.html)