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

Paramiko port forwarding is not working with UTF-8 #1640

Open
eeeeethan2333 opened this issue Mar 12, 2020 · 0 comments
Open

Paramiko port forwarding is not working with UTF-8 #1640

eeeeethan2333 opened this issue Mar 12, 2020 · 0 comments

Comments

@eeeeethan2333
Copy link

I have a target server which can be connected by a jump server(we call it bastion server). I use Paramiko port forwarding as below code. So the flow will be like:

local -> bastion server 200.200.200.200:12345 -> target server 10.10.10.10:12345

Everything was fine until I found it acting weird when I pass Chinese characters

# -*- coding: utf-8 -*-
import paramiko
from paramiko.ssh_exception import SSHException
import time
import os

SSH_PORT = 12345   # fake
SSH_USER = 'root'  
KEY_FILE_NAME = 'ssh.key'
dest_host_ip = '10.10.10.10'   # fake
bastion_ip = '200.200.200.200'   # fake
key_path = os.path.join('./', KEY_FILE_NAME)


def _get_proxy_sock(bastion_ip, key):
    try:
        new_ssh = paramiko.SSHClient()
        new_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        new_ssh.connect(
            bastion_ip,
            port=SSH_PORT,
            username=SSH_USER,
            pkey=key,
            allow_agent=False, look_for_keys=False
        )
        proxy_socket = new_ssh.get_transport().open_channel(
            'direct-tcpip', (dest_host_ip, SSH_PORT), (bastion_ip, SSH_PORT)
        )
        return proxy_socket
    except(paramiko.AuthenticationException,
           paramiko.BadAuthenticationType,
           SSHException):
        raise Exception('paramiko_proxy_error')


k = paramiko.RSAKey.from_private_key_file(key_path)
proxy_socket = _get_proxy_sock(bastion_ip, k)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
    dest_host_ip,
    port=SSH_PORT,
    username=SSH_USER,
    key_filename=key_path,
    sock=proxy_socket,
    timeout=10,
    allow_agent=False,
    look_for_keys=False
)

channel = ssh.invoke_shell()
out = channel.recv(9999)
channel.send('ls 哈哈\n')           # command is here
# channel.send('ls\n')
while not channel.recv_ready():
    time.sleep(3)

out = channel.recv(9999)
print(out.decode("utf-8"))

ssh.close()

The result I got is:

ls 哈哈
-bash: HISTCONTROL: readonly variable
-bash: HISTSIZE: readonly variable
root@sg-misc-200-200-200-200:~# ls
root@sg-misc-200-200-200-200:~# 

However, the expected result is:

root@sg-misc-200-200-200-200:~# ls 哈哈
ls: cannot access 哈哈: No such file or directory

Masters, any advices on this? orz

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

No branches or pull requests

1 participant