远程控制python

import  telnetlib

def telnetDoSomething(IP,user,passwd,command):
    try:
        # 连接服务器
        telnet = telnetlib.Telnet(IP)
        # 设置调试级别
        telnet.set_debuglevel(2)

        # 读取输入用户名信息
        rt = telnet.read_until("Login username:".encode("utf-8"))
        # 写入用户名
        telnet.write((user + "\r\n")).encode("utf-8")

        # 读取输入密码信息
        rt = telnet.read_until("Login password:".encode("utf-8"))
        # 写入
        telnet.write((passwd + "\r\n")).encode("utf-8")

        # 读取验证IP信息
        rt = telnet.read_until("Domain name:".encode("utf-8"))
        # 写入ip
        telnet.write((IP + "\r\n")).encode("utf-8")

        # 读取登陆成功信息
        rt = telnet.read_until(">".encode("utf-8"))
        # 写入指令
        telnet.write((command + "\r\n")).encode("utf-8")

        # 上面命令成功,会继续读 >
        # 失败,一般不会是>
        rt = telnet.read_until(">".encode("utf-8"))

        # 断开连接
        telnet.close()
        return True
    except:
        return False



if __name__ == "__main__":
    IP = "10.0.142.197"
    user = "xxx"
    passwd = "***"
    command = "tasklist"
    telnetDoSomething(IP,user,passwd,command)




猜你喜欢

转载自blog.csdn.net/qq_41856814/article/details/89487865