Reputation: 730
I have saw this post on stackoverflow sftp server implementaion with Python
I have tried setting up https://pypi.python.org/pypi/sftpserver , but i cannot seem to get it to work properly.
Like the stackoverflow post above, i am trying to develop a method to connect ssh to switches which i am able to do using paramiko, but then i need to be able to copy files/configs from the switchs back to the windows host where i am running my python script. i need to get a functioning python based sftp server running on my windows host that will accept connections from any sftp client like the switches. so i currently used the following code below to run sftp server in python on my windows host. i tested the connection from another windows host i am able to telnet to port 22 fine so i know the sftp server for python is running, when i run winscp from the other window host, it makes a connection but then times out. i can setup an ftp python server and connect using ftp client fine so i know i don't have any type of network issue.
not sure what i am doing wrong when i am running the sftp server below in python? i don't know if my key is wrong , i followed the steps from the sftpserver link above and ran "openssl req -out CSR.csr -new -newkey rsa:2048 -nodes -keyout /tmp/test_rsa.key" to create a key from an ubuntu host i had. i don't see that any username or password is required from the client side when i use winscp to connect.. here is netstat -aon output from windows so it is listening on 22 and telnet to it works from another host.
Active Connections
Proto Local Address Foreign Address State PID TCP 0.0.0.0:22 0.0.0.0:0 LISTENING 7012
the code i am currently using i taken from the following: https://gist.github.com/Girgitt/2df036f9e26dba1baaddf4c5845a20a2 so in short i need a functioning sftp server running on windows in python script, then use any non-python component like winscp or a switch to connect to the sftp server running on windows in python script?
import time
import socket
import optparse
import sys
import textwrap
import os
import paramiko
from sftpserver.stub_sftp import StubServer, StubSFTPServer
import threading
HOST, PORT = 'localhost', 22
BACKLOG = 10
class ConnHandlerThd(threading.Thread):
def __init__(self, conn, keyfile):
threading.Thread.__init__(self)
self._conn = conn
self._keyfile = keyfile
def run(self):
host_key = paramiko.RSAKey.from_private_key_file(self._keyfile)
transport = paramiko.Transport(self._conn)
transport.add_server_key(host_key)
transport.set_subsystem_handler(
'sftp', paramiko.SFTPServer, StubSFTPServer)
server = StubServer()
transport.start_server(server=server)
channel = transport.accept()
while transport.is_active():
time.sleep(1)
def start_server(host, port, keyfile, level):
paramiko_level = getattr(paramiko.common, level)
paramiko.common.logging.basicConfig(level=paramiko_level)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
server_socket.bind((host, port))
server_socket.listen(BACKLOG)
while True:
conn, addr = server_socket.accept()
srv_thd = ConnHandlerThd(conn, keyfile)
srv_thd.setDaemon(True)
srv_thd.start()
def main():
usage = """\
usage: sftpserver [options]
-k/--keyfile should be specified
"""
parser = optparse.OptionParser(usage=textwrap.dedent(usage))
parser.add_option(
'--host', dest='host', default=HOST,
help='listen on HOST [default: %default]')
parser.add_option(
'-p', '--port', dest='port', type='int', default=PORT,
help='listen on PORT [default: %default]'
)
parser.add_option(
'-l', '--level', dest='level', default='INFO',
help='Debug level: WARNING, INFO, DEBUG [default: %default]'
)
parser.add_option(
'-k', '--keyfile', dest='keyfile', metavar='FILE',
help='Path to private key, for example /tmp/test_rsa.key'
)
options, args = parser.parse_args()
options.keyfile = "C:\\test_rsa.key"
if options.keyfile is None:
parser.print_help()
sys.exit(-1)
start_server(options.host, options.port, options.keyfile, options.level)
if __name__ == '__main__':
main()
Upvotes: 4
Views: 9600
Reputation: 730
i figured it out. my key file
had
-----BEGIN PRIVATE KEY----- MIEdksjdlfaj;slkdfj......... ..... -----END PRIVATE KEY----- in it.
i had to change both to
-----BEGIN RSA PRIVATE KEY----- MIEdksjdlfaj;slkdfj......... ..... -----END RSA PRIVATE KEY-----
then winscp prompted for it fine
Upvotes: 6