python执行telnet命令

上一篇 / 下一篇  2008-07-25 11:21:26 / 个人分类:开发语言Python

一个简单的用python执行telnet命令的例子

# -*- coding: mbcs -*-

import telnetlib
import ipAddress
import sys

host='122.224.113.164'
port = 20001
command='showall'
timeout = 2

def telnet():
    global command
    try:
        tn = telnetlib.Telnet(host,port) #连接端绑定到主机 HOST  去
    except:
        print   "Cannot open host"
        return   
   
    tn.write(command+'\r\n')
    msg = tn.read_until("$",timeout)
    tn.write("exit\r\n")
    tn.close()
    print msg
    cont = raw_input("\ncommand>")
    if cont != '':
        command = cont
    show()


def help():
    print """python telnet.py [host=<host>] [port=<port>] [timeout=<timeout>] [command=<command>]"""
   
if __name__ == "__main__":
    if len(sys.argv) >= 2:  # additional parameters
        for arg in sys.argv[1:]:
            pv=arg.split("=")
            if len(pv)!=2:
                help()
                sys.exit()
            if pv[0] == "min":
                min=int(pv[1])
            elif pv[0] == "host":
                host=pv[1]
            elif pv[0] == "port":
                port = int(pv[1])
            elif pv[0] == "command":
                command = pv[1]
            elif pv[0] == "timeout":
                timeout = int(pv[1])
            else:
                help()
                sys.exit()
    show()
   
   


TAG: Python telnet 开发语言Python

引用 删除 曾钰   /   2016-02-18 16:18:11
5
引用 删除 05yangfang   /   2014-04-10 11:22:40
5
 

评分:0

我来说两句

我的栏目

日历

« 2024-04-27  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 5645
  • 日志数: 3
  • 建立时间: 2007-11-12
  • 更新时间: 2008-07-25

RSS订阅

Open Toolbar