0

I'm trying to automate uploading and downloading files from sftp server. I've come up with the idea of creating an SSIS package with Python script doing the heavylifting and then running the SSIS package in SQL server job. I need to establish the connection to sftp server going through proxy server. I've come across a paramiko - Python implementation of SSH. I'm racking my brain how to use it to connect to my sftp server.

Here's my code:

import paramiko

hostname = "my_sftp_host"
port=22
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
proxy = paramiko.proxy.ProxyCommand(
    '/usr/bin/nc --proxy my_server_proxy %s %d' \
    % (hostname, port) )
ssh = paramiko.Transport(sock=proxy)
ssh.connect(username='***', password='***')

sftp = paramiko.SFTPClient.from_transport(ssh)

And the errors I'm getting:

Traceback (most recent call last):
  File "C:\Python\PythON-Wtajemniczenie\LibrisSFTP.py", line 7, in <module>
    proxy = paramiko.proxy.ProxyCommand(
  File "C:\Users\arkadiusz.drezek\AppData\Local\Programs\Python\Python310\lib\site-packages\paramiko\proxy.py", line 62, in __init__
    self.process = subprocess.Popen(
  File "C:\Users\arkadiusz.drezek\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 969, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\arkadiusz.drezek\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1438, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] Nie można odnaleźć określonego pliku

The error-message translated:

FileNotFoundError: [WinError 2] The system cannot find the file specified

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Arkadiusz
  • 369
  • 5
  • 18
  • 1
    I think the problem is that the "/usr/bin/nc" program doesn't exist in your filesystem. You are using Windows after all. – ccre Dec 03 '22 at 20:18
  • I totally agree with @ccre since the script is running on Windows, and this OS certainly does NOT have a `/usr/bin/nc` binary available. – accdias Dec 03 '22 at 20:23
  • That makes perfect sence, should I point out to the directory where paramiko package is installed? – Arkadiusz Dec 03 '22 at 20:33
  • 1
    If you don't have an executable named `nc` (usually NetCat is a Linux command) on your Windows path, then might find a Windows alternative like [`ncat`](https://stackoverflow.com/a/41021069/5730279) or `connect` as in [Windows SSH ProxyCommand /usr/bin/bash: line 0: exec: nc: not found on git bash - Stack Overflow](https://stackoverflow.com/questions/70603564/windows-ssh-proxycommand-usr-bin-bash-line-0-exec-nc-not-found-on-git-bash) – hc_dev Dec 03 '22 at 20:38
  • Here is another link you may find useful: https://nmap.org/ncat/. – ccre Dec 03 '22 at 20:41
  • Thanks guys, it's kind of tricky for me but I'm trying to get my head around it. – Arkadiusz Dec 03 '22 at 21:04
  • 1
    You can install the `Nmap` (https://nmap.org/download#windows), which is a very popular network scanner, and then you will find the `ncat.exe` binary in the installation folder. In the next step you need to replace `/usr/bin/nc` with the path to this binary. I think that is a quick solution to check if your script works. On my system the path to this binary is `C:\Program Files (x86)\Nmap\ncat.exe`. – ccre Dec 03 '22 at 21:41

0 Answers0