pythontelnet.py

来自「在python下」· Python 代码 · 共 34 行

PY
34
字号
# -*- coding: utf-8 -*-
'''程序的目的:您只要将下面内容中的IP地址、用户名和密码替换成您想登录的计算机
就可以访问这台计算机,并且执行相关命令'''

import telnetlib

host = {'ip': '0.0.0.0', 'user': 'username', 'password':'password','commands':'commands'}

host['ip']='10.10.10.10'
host['user']='username'
host['password']='yourpassword'
#下面一行是3个命令,可以根据需要进行编辑
host['commands']=['cd ', 'pwd','ls']

def do(host):
    #print host
    tn = telnetlib.Telnet(host['ip'])
    #tn.set_debuglevel(2)

    tn.read_until("帐号: ",2)
    tn.write(host['user'] + "\n")
    tn.read_until("密码: ",2)
    tn.write(host['password'] + "\n")

    for command in host['commands']:
        tn.write(command+'\n')
    
    tn.write("exit\n")
    print tn.read_all()
    
    print '远程执行任务结束!!!'

do(host)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?