Comments (18)
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.
Hi @jun66j5 ,
Can I get help on this issue
It's kind of urgent here
from paramiko.
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.
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.
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.
I'd suggest using
Channel.exec_command
instead of sending command viaChannel.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.
Use PGPASSWORD enviroment (or PGPASSFILE) via exec_command rather than interactive mode.
from paramiko.
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.
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.
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.
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.
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.
Can u plz try it once if you have the windows setup
That would give a clear picture to the issue
from paramiko.
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.
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.
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.
Ok let me check I haven't encode the send command let me check with that
from paramiko.
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)
- [SUPPORT] - Getting RSA key from Azure Vault into paramiko - Not working
- [BUG] - SSHClient.open_sftp().open(...) cause file downloaded being modified HOT 2
- invoke_shell Dynamically adjust window size HOT 2
- [BUG] - After establishing an SSH connection, when a List[SomeClass] exists, the interpreter does not perform memory recycling (execute __del__).
- [SUPPORT] - <title>resume transfer
- [BUG] - Key discovery fails on root
- Locking remote ssh host with paramiko session/channel id HOT 5
- [BUG] - _parse_service_accept: Unexpected packet types are ignored (code suggests an exception was intended)
- Accept Path objects for remote file name in sftp.put HOT 2
- [SUPPORT] - Use Paramiko inside docker container
- [SUPPORT] - SSHClient.close() or SSHClient.exec_command("exit") doesn't close ssh session on macOS
- [BUG] - Paramiko doesnt work with OpenSSH 7.8 HOT 1
- [BUG] - Sends duplicate userauth-request - fails with AsyncSSH 2.14.1 and multiple SSH keys
- [BUG] - Transport::join_lingering_threads() not stopping all threads and join thread never get executed in Transport::stop_thread() HOT 1
- [BUG] - Wrong agreed keys (preferred_keys)
- [BUG] - Invoke_Shell deadlocks on remote reboot.
- [FEAT] - add demo of non-interactive ssh client
- [BUG] - SFTP disables further prefetching on single prefetching failure
- [SUPPORT] - Deprecation Warning for TripleDES Algorithm in Paramiko HOT 13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from paramiko.