Use of the Telnetlib module in Python

Source: Internet
Author: User

First, under Windows to turn on the Telnet service

(see: Seamless integration with Win7 firewall Telnet function test)

1. Windows 2000/xp/2003/vista: The Telnet service is installed by default but is disabled

(1) Turn on Telnet: Run services.msc to open service management, locate the Telnet service entry setting whose startup type is "Automatic" or "manual" (more secure, only enabled when needed), and then start the service.

2. Windos 7: Telnet service is not installed by default

(1) Install Telnet: click "Start" → "Control Panel" → "Programs", "in programs and features" to find and click "Turn Windows features on or off" into the Windows Feature Settings dialog box. Locate and tick "Telnet client" and "Telnet Server", and finally "OK" to complete the installation at a later moment.

(2) Turn on Telnet: Method same as 1 (1)

Second, Linux to open the Telnet service

(See: Configuring Telnet Server under Ubuntu 10.10)

(1) Install telnetd (i.e. Telnet-server): Apt-get Install telnetd

(2) Install xinetd (Telnet-server operation needs to be managed by XINETD): Apt-get Install xinetd

(3) Configure Telnet file: vi/etc/xinetd.d/telnet

(4) Open Xinetd:service xinetd start

Third, using Python to implement Telnet telnet

The Telnetlib library is specifically provided in Python to complete the communication function based on the Telnet protocol.

The code to implement remote login using Telnetlib is as follows:

1 #-*-Coding:utf-8-*-
2
3 Import Telnetlib
4
5 "Telnet telnet: Windows client connects to Linux server"
6
7 # Configuration options
8 Host = ' 192.168.1.2 ' # telnet Server IP
9 username = ' Admin ' # Login user name
Ten password = ' 123456 ' # login password
finish = ': ~$ ' # command prompt (identifies that the previous command has been executed)
12
13 # Connect Telnet Server
TN = Telnetlib. Telnet (Host)
15
16 # Enter Login user name
Tn.read_until (' Login: ')
Tn.write (username + ' \ n ')
19
20 # Enter Login password
Tn.read_until (' Password: ')
Tn.write (password + ' \ n ')
23
24 # After logging in, execute the LS command
Tn.read_until (Finish)
Tn.write (' ls\n ')
27
When the LS command finishes executing, terminate the Telnet connection (or enter exit)
Tn.read_until (Finish)
Tn.close () # tn.write (' exit\n ')

###################################################

#!/usr/bin/env python

Import Getpass
Import Sys
Import Telnetlib

HOST = "10.1.1.1"
user = Raw_input ("Enter Your Remote account:")
Password = Getpass.getpass ()

Print password

TN = Telnetlib. Telnet (HOST)
Print "Begin login."
Tn.read_until ("Login:")
Tn.write (user + "\ n")

If password:
Tn.read_until ("Password:")
Tn.write (password + "\ n")

Print "Login done."
Tn.write ("ls\n")
Tn.write ("exit\n")

Print Tn.read_all ()

This is a simple example.

Using the Telnetlib module in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.