在 Python 中验证 Telnet 会话的身份验证

发布于 2024-11-13 11:43:28 字数 1085 浏览 3 评论 0原文

我正在尝试远程登录到思科交换机并在其上运行几个命令。我可以检查主机是否不存在,但不确定如何检查用户名或密码是否正确。这是我到目前为止得到的(这是我课程的一部分)

def login(self):
    if self.user_name and self.password: 
        try:
            self.connection=telnetlib.Telnet(self.telnet_host)
            try:
                self.connection.read_until('sername:',1)
                self.connection.write(self.user_name+'\r\n')
                self.connection.read_until('assword:',1)
                self.connection.write(self.password+'\r\n')
                self.connection.read_until(self.prompt,1)
                print "Connected"
                self.loggedON=True
            except EOFError:
                print "Authentication to "+ self.telnet_host+" failed.\n"
                return
        except: 
            print "Can't connect to "+self.telnet_host+"\n"
            return
    else:
        if not self.user_name:
            self.user_name=raw_input("Username: ")
            self.login()
        else:
            self.password=raw_input("Password: ")
            self.login()

即使密码或用户名错误,它仍然会说它已连接。

I am trying to telnet into a Cisco Switch and run a couple of commands on it. I am able to check if the host doesn't exist, not sure how to check if the username or password is correct. This is what I got so far(This is part of my class)

def login(self):
    if self.user_name and self.password: 
        try:
            self.connection=telnetlib.Telnet(self.telnet_host)
            try:
                self.connection.read_until('sername:',1)
                self.connection.write(self.user_name+'\r\n')
                self.connection.read_until('assword:',1)
                self.connection.write(self.password+'\r\n')
                self.connection.read_until(self.prompt,1)
                print "Connected"
                self.loggedON=True
            except EOFError:
                print "Authentication to "+ self.telnet_host+" failed.\n"
                return
        except: 
            print "Can't connect to "+self.telnet_host+"\n"
            return
    else:
        if not self.user_name:
            self.user_name=raw_input("Username: ")
            self.login()
        else:
            self.password=raw_input("Password: ")
            self.login()

It will still say it is connected even if the wrong password or username.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

行至春深 2024-11-20 11:43:28

您还可以尝试 Exscript

from Exscript.util.start import quickstart

def do_something(conn):
    conn.autoinit()
    conn.execute('show version')

quickstart('telnet://localhost', do_something)

quickstart() 函数要求用户输入用户名和密码(如果这不是您想要的,请使用 start())。登录失败(和其他错误)会自动处理。您可能还想查看 Exscript.util.start

You could also try Exscript:

from Exscript.util.start import quickstart

def do_something(conn):
    conn.autoinit()
    conn.execute('show version')

quickstart('telnet://localhost', do_something)

The quickstart() function asks the user for username and password (use start() if that is not what you want). Login failure (and other errors) are handeled automatically. You may also want to look at Exscript.util.start.

星星的軌跡 2024-11-20 11:43:28

首先,你不应该有这样的一揽子尝试/例外块。更严格地捕获异常。另外,正如其他人评论的那样,您可能会考虑 SNMP。

话虽如此,如果您继续使用 Telnet,您还不如重用别人的代码。例如,我通过简单的 Google 搜索找到了这个

First of all, you shouldn't have a blanket try/except block like that. Catch exceptions more narrowly. Also, as others have commented, you might consider SNMP.

Having said that, if you push ahead with Telnet, you might as well just reuse someone else's code. I found this for example with a simple Google search.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文