Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MOTD Wake Up #3425

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft

Conversation

FriendlyGecko
Copy link

@FriendlyGecko FriendlyGecko commented Apr 28, 2024

This may not be the best way to solve the issue, but it is a way.

Issue we are trying to solve

When attempting to log into a device that uses a MOTD, the login fails. This is because the MOTD requires the user to first send a return character to wake it up. If I use putty to first console in and press enter, I can swap back to my netmiko script and log into the device without issue.

To solve the issue, I added a check (checks if the login process is already started) that will send a return character to the device to first wake it up before going to into the login process. If the user is already logged in, then all this will do is return a blank line (unless there is text in the prompt somehow).

This may be better, but taking critiques on it:

                try:
                    output = self.read_channel()
                    return_msg += output

                    # Search for username pattern / send username
                    if re.search(username_pattern, output, flags=re.I):
                        # Sometimes username/password must be terminated with "\r" and not "\r\n"
                        self.write_channel(self.username + "\r")
                        time.sleep(1 * delay_factor)
                        output = self.read_channel()
                        return_msg += output

                    # Search for password pattern / send password
                    if re.search(pwd_pattern, output, flags=re.I):
                        # Sometimes username/password must be terminated with "\r" and not "\r\n"
                        assert isinstance(self.password, str)
                        self.write_channel(self.password + "\r")
                        time.sleep(0.5 * delay_factor)
                        output = self.read_channel()
                        return_msg += output
                        if re.search(
                            pri_prompt_terminator, output, flags=re.M
                        ) or re.search(alt_prompt_terminator, output, flags=re.M):
                            return return_msg

                    # Support direct telnet through terminal server
                    if re.search(
                        r"initial configuration dialog\? \[yes/no\]: ", output
                    ):
                        self.write_channel("no" + self.TELNET_RETURN)
                        time.sleep(0.5 * delay_factor)
                        count = 0
                        while count < 15:
                            output = self.read_channel()
                            return_msg += output
                            if re.search(r"ress RETURN to get started", output):
                                output = ""
                                break
                            time.sleep(2 * delay_factor)
                            count += 1

                    # Check for device with no password configured
                    if re.search(r"assword required, but none set", output):
                        assert self.remote_conn is not None
                        self.remote_conn.close()
                        msg = (
                            "Login failed - Password required, but none set: {}".format(
                                self.host
                            )
                        )
                        raise NetmikoAuthenticationException(msg)

                    # Check if proper data received
                    if re.search(
                        pri_prompt_terminator, output, flags=re.M
                    ) or re.search(alt_prompt_terminator, output, flags=re.M):
                        return return_msg

                    # MOTD Wake Up
                    self.write_channel("\r")
                    time.sleep(1 * delay_factor)
                    output = self.read_channel()
                    return_msg += output

                    i += 1

@FriendlyGecko
Copy link
Author

This resolves #3424

@FriendlyGecko FriendlyGecko marked this pull request as draft April 29, 2024 09:05
@FriendlyGecko
Copy link
Author

I moved this to a draft, because it warrants a conversation on if this is the best way to resolve the issue or if it should be moved/tweaked

@FriendlyGecko FriendlyGecko marked this pull request as ready for review April 29, 2024 09:07
@FriendlyGecko FriendlyGecko marked this pull request as draft April 29, 2024 09:07
@ktbyers
Copy link
Owner

ktbyers commented Jul 10, 2024

@FriendlyGecko Which platform does this apply to?

Also is this when using a Console or directly via SSH to the device?

@FriendlyGecko
Copy link
Author

FriendlyGecko commented Jul 16, 2024

This specifically affects Cisco devices.

I will set up another lab and test it again, but as far as I remember I ran into the issue with both console and SSH, and this solved both. Please give me a little time to confirm though.

@FriendlyGecko
Copy link
Author

UPDATE: Latest update may make this pull request irrelevant. With my current home lab I was unable to replicate the issue that the pull request was intended to solve. I will test again with my previous lab which have a different IOS and see if that is related.

If I am unable to replicate it there then I will close the pull request and the associated issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants