Python链接Telnet
2016-02-23 19:57
555 查看
# -*- coding: utf-8 -*-
import telnetlib
'''Telnet远程登录:Windows客户端连接Linux服务器'''
# 配置选项
Host = '127.0.0.1' # Telnet服务器IP
username = 'admin' # 登录用户名
password = '123456' # 登录密码
finish = '$' # 命令提示符(标识着上一条命令已执行完毕)
# 连接Telnet服务器
tn = telnetlib.Telnet(Host,port=23,timeout=10)
tn.set_debuglevel(2)
# 输入登录用户名
tn.read_until(b'login: ')
tn.write(username.encode('ascii') + b'\n')
# 输入登录密码
tn.read_until(b'Password: ')
tn.write(password.encode('ascii') + b'\n')
# 登录完毕后,执行ls命令
tn.read_until(finish.encode('ascii'))
tn.write(b'ls\n')
tn.read_until(finish.encode('ascii'))
tn.write(b'/usr/java/test.sh\n')
# ls命令执行完毕后,终止Telnet连接(或输入exit退出)
tn.read_until(finish)
tn.close() # tn.write('exit\n')
import telnetlib
'''Telnet远程登录:Windows客户端连接Linux服务器'''
# 配置选项
Host = '127.0.0.1' # Telnet服务器IP
username = 'admin' # 登录用户名
password = '123456' # 登录密码
finish = '$' # 命令提示符(标识着上一条命令已执行完毕)
# 连接Telnet服务器
tn = telnetlib.Telnet(Host,port=23,timeout=10)
tn.set_debuglevel(2)
# 输入登录用户名
tn.read_until(b'login: ')
tn.write(username.encode('ascii') + b'\n')
# 输入登录密码
tn.read_until(b'Password: ')
tn.write(password.encode('ascii') + b'\n')
# 登录完毕后,执行ls命令
tn.read_until(finish.encode('ascii'))
tn.write(b'ls\n')
tn.read_until(finish.encode('ascii'))
tn.write(b'/usr/java/test.sh\n')
# ls命令执行完毕后,终止Telnet连接(或输入exit退出)
tn.read_until(finish)
tn.close() # tn.write('exit\n')
相关文章推荐
- python正则表达式练习篇
- Python练习-- List Comprehensions
- 《笨办法学Python》 第43课手记
- Python导入matplotlib错误提示ImportError: matplotlib requires pyparsing
- Python socket超时
- 运行独立python 引用其他目录包
- python第七天
- Windows环境下新浪SAE本地开发环境搭建及简单测试
- os x + virtualenv + openCV2 + python2.7 环境打造
- 【LEETCODE】148- Sort List [Python]
- python 转化文件编码 utf8
- python之疑惑函数默认初始化
- [Leetcode]@python 95. Unique Binary Search Trees II
- cmd和python的mysql连接方法
- [Leetcode]@python 94. Binary Tree Inorder Traversal
- PyQt4 Python GUI窗体应用程序
- [Leetcode]@python 93. Restore IP Addresses
- python k-近邻算法
- Python练习-- slice
- Python练习-- loop