-
-
Save fliedonion/1b7ccf9f05b5ed97c2ebf442bdf1eb78 to your computer and use it in GitHub Desktop.
Fork for Update source for python3 and my pxssh testing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##!/usr/bin/env python3 | |
import os | |
import sys | |
from pexpect.pxssh import pxssh as PXSSH | |
import getpass | |
class Debug_PXSSH(PXSSH): | |
def sendline(self, input='') -> int: | |
if self.echo_enabled: | |
sys.stderr.write('\n[sendline::input={!r}]'.format(input)) | |
return PXSSH.sendline(self, input) | |
def try_read_prompt(self, timeout_multiplier:float): | |
prompt = PXSSH.try_read_prompt(self, timeout_multiplier) | |
sys.stderr.write('\n[try_read_prompt::prompt={!r}]'.format(prompt)) | |
sys.stderr.flush() | |
return prompt | |
def sync_original_prompt (self, sync_multiplier=1.0): | |
from pexpect import TIMEOUT | |
import time | |
sys.stderr.write('\n[sync_original_prompt]') | |
sys.stderr.flush() | |
self.sendline() | |
time.sleep(0.1) | |
try: | |
self.try_read_prompt(sync_multiplier) | |
except TIMEOUT: | |
pass | |
self.sendline() | |
x = self.try_read_prompt(sync_multiplier) | |
self.sendline() | |
a = self.try_read_prompt(sync_multiplier) | |
self.sendline() | |
b = self.try_read_prompt(sync_multiplier) | |
ld = self.levenshtein_distance(a,b) | |
len_a = len(a) | |
if len_a == 0: | |
sys.stderr.write('\n[len_a=0]') | |
return False | |
val = float(ld)/len_a | |
sys.stderr.write('\n[val={}, ld={}, len_a={}]\n' | |
.format(val, ld, len_a)) | |
sys.stderr.flush() | |
if val < 0.4: | |
return True | |
sys.stderr.write('\n[! distance too far:' | |
'\n{!r}\n{!r}]\n'.format(a, b)) | |
sys.stderr.flush() | |
return False | |
# setup variables | |
username = os.environ['USER'] | |
hostname = input('hostname: ') | |
username = input('username [{}]: '.format(username)).strip() or username | |
port = int(input('port [22]: ').strip() or '22') | |
password = getpass.getpass('password: ') | |
# create and begin session, | |
session.echo_enabled = False # to suppress echo password. | |
os.environ["TERM"] = "dumb" | |
session.login(hostname, username, password, port=port, original_prompt=r"[#$%] ") | |
session.echo_enabled = True | |
# note: | |
# os.environ["TERM"] = "dumb" : for colorized remote. without this connect to remote is extreamly slow. | |
# : terminal_type='dumb' not work. | |
# original_prompt=r"[#$%] " : if not set I got 'could not synchronize with original prompt'. | |
# : in my case, remote is raspi (bash) so `r"[#$] "` also OK. `r"[#$]"` does not. | |
session.sendline('uptime') | |
session.prompt() | |
sys.stderr.write('\n[session.before={!r}]'.format(session.before)) | |
session.logout() | |
sys.stderr.write('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had error 'could not synchronize with original prompt'.
Finally, I have success to connect using normal user with this code.
I tested this with:
local: Mac OS X 10.15.7 with zsh.
remote: raspberry pi Raspbian stretch with bash.