Giter Club home page Giter Club logo

Comments (18)

basu45 avatar basu45 commented on July 26, 2024 1

Hi,
Sorry for the inconvenience.
The issue was cause due to cygwin sshd service was not able to open any of the executables.so we changed cygwin's ssh service with openssh and it worked.

Thak you very much for the help!!!

from paramiko.

basu45 avatar basu45 commented on July 26, 2024

Hi @jun66j5 ,
Can I get help on this issue
It's kind of urgent here

from paramiko.

jun66j5 avatar jun66j5 commented on July 26, 2024

I'd suggest using Channel.exec_command instead of sending command via Channel.invoke_shell unless you are familiar with pty and shell.

Also, the script should be posted with ```...``` (code block). See https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks.

from paramiko.

jun66j5 avatar jun66j5 commented on July 26, 2024

Also, that is not a paramiko issue. It is just a support question of paramiko-expect. I'd suggest to send the question to paramiko-expect project, not here.

from paramiko.

basu45 avatar basu45 commented on July 26, 2024

No i mean when command is sent via channel.send()

The channel.recv() is not providing the correct output
Here is the output provided by cahnnel.recv()

b'[6npswl -U postgres Microsoft windows version [version details]. \r\r\n'

In our case expect library is failing as it's using channel.recv() and it's not providing the correct output

Looks more like paramiko issue rather than paramiko expect

We need to use channel.invoke_shell() because we need the expect like interactive shell

Whereas same works perfectly fine with linux

from paramiko.

basu45 avatar basu45 commented on July 26, 2024

I'd suggest using Channel.exec_command instead of sending command via Channel.invoke_shell unless you are familiar with pty and shell.

Also, the script should be posted with ```...``` (code block). See https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks.

My opologies.
Will follow above rules

from paramiko.

jun66j5 avatar jun66j5 commented on July 26, 2024

Use PGPASSWORD enviroment (or PGPASSFILE) via exec_command rather than interactive mode.

from paramiko.

basu45 avatar basu45 commented on July 26, 2024

We needed an interactive mode because we need to query the db and based on query output some additional queries needs to be executed based on previous outputs
Is there any way to find root cause and try to fix it?

from paramiko.

jun66j5 avatar jun66j5 commented on July 26, 2024

Your usage is wrong.

b'[6npswl -U postgres Microsoft windows version [version details]. \r\r\n'

Send command (psql -U ...\r\n) to the channel after receiving the prompt of cmd.exe.

from paramiko.

basu45 avatar basu45 commented on July 26, 2024

Same thing is done the above output is what we are getting after sending psql ...\r\n
Instead of giving psql prompt it's giving the cmd prompt

from paramiko.

jun66j5 avatar jun66j5 commented on July 26, 2024

We needed an interactive mode because we need to query the db and based on query output some additional queries needs to be executed based on previous outputs

If I were to do it, I'd implement it in a PowerShell script (or bat script) and run the script using exec_command from paramiko, and avoid interactive mode.

from paramiko.

basu45 avatar basu45 commented on July 26, 2024

We needed an interactive mode because we need to query the db and based on query output some additional queries needs to be executed based on previous outputs

If I were to do it, I'd implement it in a PowerShell script (or bat script) and run the script using exec_command from paramiko, and avoid interactive mode.

Ok thanks, but the issue is we already have pre built framework around that which worked perfectly fine with linux so we needed same script to work with window ssh

from paramiko.

basu45 avatar basu45 commented on July 26, 2024

Can u plz try it once if you have the windows setup
That would give a clear picture to the issue

from paramiko.

jun66j5 avatar jun66j5 commented on July 26, 2024

Hm, I don't have experience with shell operations on Windows Server from paramiko, and I don't think it works properly, so I can't help you.

from paramiko.

basu45 avatar basu45 commented on July 26, 2024

Hm, I don't have experience with shell operations on Windows Server from paramiko, and I don't think it works properly, so I can't help you.

Ok,can you point me to the right guy
That will be helpful..

from paramiko.

jun66j5 avatar jun66j5 commented on July 26, 2024

Works fine on my environment but I don't recommend using interactive mode if you need it unless solving it yourself.

Script

import os, re, sys, time
import paramiko

ssh_host = 'computer-name'
ssh_user = 'username'
ssh_pass = 'password'
psql_bin = r'"C:\Program Files\PostgreSQL\16\bin\psql.exe"'
psql_host = 'hostname'
psql_user = 'username'
psql_pass = 'password'
psql_dbname = 'dbname'
cmd_prompt_re = re.compile((r'^%s@\S+\s+C:\\[^>]*>' % re.escape(ssh_user)).encode('utf-8'), re.M)
psql_prompt_re = re.compile((r'^%s=[#>]' % re.escape(psql_dbname)).encode('utf-8'), re.M)

paramiko.util.log_to_file(__file__ + '.log')

def wait_for_prompt(shell, pattern):
    while True:
        while shell.recv_ready():
            data = shell.recv(1024)
            print(repr(data), file=sys.stderr, flush=True)
            if pattern.search(data):
                return
        time.sleep(1)

def wait_for_cmd_prompt(shell):
    wait_for_prompt(shell, cmd_prompt_re)

def wait_for_psql_prompt(shell):
    wait_for_prompt(shell, psql_prompt_re)

with paramiko.SSHClient() as cli:
    cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    cli.connect(ssh_host, username=ssh_user, password=ssh_pass)
    with cli.invoke_shell() as shell:
        wait_for_cmd_prompt(shell)
        shell.send(f'{psql_bin} --version\r\n'.encode('utf-8'))
        wait_for_cmd_prompt(shell)
        shell.send(f'set PGPASSWORD={psql_pass}\r\n'.encode('utf-8'))
        wait_for_cmd_prompt(shell)
        shell.send(f'{psql_bin} --host={psql_host} --username={psql_user} {psql_dbname}\r\n'.encode('utf-8'))
        wait_for_psql_prompt(shell)
        shell.send(b'SELECT version();\r\n')
        wait_for_psql_prompt(shell)

Results

C:>call C:\usr\src\paramiko.venv\Scripts\python.exe paramiko-windows.py
b'\x1b[2J\x1b[m\x1b[HMicrosoft Windows [Version 10.0.19045.4474]\x1b]0;C:\\WINDOWS\\system32\\conhost.exe\x07\x1b[?25h\x1b[25l\r\n(c) Microsoft Corporation. All rights reserved.\r\n\x1b[47X\r\nusername@COMPUTER-NAME C:\\Users\\username>    \x1b[4;44H\x1b]0;Administrator: C:\\WINDOWS\\system32\\conhost.exe\x07\x1b[?25h'
b'\rusername@COMPUTER-NAME C:\\Users\\username>"C:\\Program Files\\PostgreSQL\\16\\bin\\p\r\nsql.exe" --version\x1b[K\r\npsql (PostgreSQL) 16.3\x1b[21X\r\n\x1b[43X\r\nusername@COMPUTER-NAME C:\\Users\\username>'
b'set PGPASSWORD=password\r\n\r\nusername@COMPUTER-NAME C:\\Users\\username>'
b'\rusername@COMPUTER-NAME C:\\Users\\username>"C:\\Program Files\\PostgreSQL\\16\\bin\\p\r\nsql.exe" --host=192.168.11.122 --username=username dbname\x1b[K\r\n\x1b]0;Administrator: C:\\WINDOWS\\system32\\conhost.exe - "C:\\Program Files\\PostgreSQL\\16\\bin\\psql.exe"  --host=192.168.11.122 --username=username dbname\x07p\rpsql (16.3, server 9.5.25)\x1b[K\r\nSSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, compress\r\nion: off)\x1b[K\r\nType "help" for help.\x1b[K\r\n\x1b[K\r\ndbname=>\x1b[K\x1b[1C'
b'SELECT version();\r\n                                                      version\x1b[K\r\n\x1b[K\r\n--------------------------------------------------------------------------------\r\n-----------------------------------\x1b[K\r\n PostgreSQL 9.5.25 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu\r\n1~16.04.12) 5.4.0 20160609, 64-bit\x1b[K\r\n(1 row)\x1b[K\r\n\n\ndbname=>\x1b[1C'

from paramiko.

basu45 avatar basu45 commented on July 26, 2024

Ok let me check I haven't encode the send command let me check with that

from paramiko.

jun66j5 avatar jun66j5 commented on July 26, 2024

I don't think you should close an issue without explaining what caused it, especially when you've received help.

from paramiko.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.