zoukankan      html  css  js  c++  java
  • python连接telnet服务

    import telnetlib
    
    
    def main(host, port, username, password):
        login_hints = [b'login', b'Login']
        login_failed_status = [b"Login incorrect", b'Login failed', b'Login Failed']
        password_hints = [b"Password", b"password"]
        username_flag = False
        password_flag = False
    
        try:
            tn = telnetlib.Telnet(host=host, port=port, timeout=10)
            while True:
                if username_flag:
                    break
    						
                # 如果这里使用decode的话,不同的操作系统可能会因为不兼容导致报错。
                std_out = tn.read_eager().strip(b'\r\n')
    
                if not std_out:
                    continue
    
                for login_hint in login_hints:
                    if login_hint in std_out:
                        tn.write(username.encode() + b'\r\n')
                        username_flag = True
                        break
    
            while True:
                if password_flag:
                    break
    
                std_out = tn.read_eager().strip(b'\r\n')
    
                if not std_out:
                    continue
    
                for password_hint in password_hints:
                    if password_hint in std_out:
                        tn.write(password.encode() + b'\r\n')
                        password_flag = True
                        break
    
            while True:
                std_out = tn.read_very_eager().strip(b'\r\n')
    
                if not std_out:
                    continue
    
                for failed_status in login_failed_status:
                    if failed_status in std_out:
                        return False
                tn.close()
                return True
    
        except Exception as e:
            print(e)
            return False
    
  • 相关阅读:
    sed
    UCOSIII(二)
    UCOSIII(一)
    IIC
    SPI
    vii
    find
    grep
    Scrum项目4.0
    Scrum项目3.0
  • 原文地址:https://www.cnblogs.com/liuhuan086/p/15650008.html
Copyright © 2011-2022 走看看