sftp problem!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sa6113

    sftp problem!


    I want to use sftp from paramiko to copy a file from a windows machine to a
    Linux in the network, I use this code :

    host = "LinuxComputerN ame" (or its Ip)
    port = 22
    transport = paramiko.Transp ort((host, port))
    password = "LinuxComputerP assword"
    username = "LinuxComputerU serName"

    transport.conne ct(username = username, password = password)
    sftp = paramiko.SFTPCl ient.from_trans port(transport)
    filepath = '/home/test.jpg'
    localpath = '/home/test.jpg'
    sftp.get(filepa th, localpath)

    sftp.close()
    transport.close ()

    and after runing this code I get this error:
    socket.error: (10061, 'Connection refused')

    What is wrong with this code?
    please help me.



    --
    View this message in context: http://www.nabble.com/sftp-problem%2...p19821106.html
    Sent from the Python - python-list mailing list archive at Nabble.com.

  • Lawrence D'Oliveiro

    #2
    Re: sftp problem!

    In message <mailman.2006.1 223188361.3487. python-list@python.org >, sa6113
    wrote:
    host = "LinuxComputerN ame" (or its Ip)
    port = 22
    >
    and after runing this code I get this error:
    socket.error: (10061, 'Connection refused')
    Did you try

    sftp LinuxComputerNa me

    just to confirm it's got a listener on port 22?

    Comment

    • sa6113

      #3
      Re: sftp problem!


      what do you mean?


      Lawrence D'Oliveiro wrote:
      >
      In message <mailman.2006.1 223188361.3487. python-list@python.org >, sa6113
      wrote:
      >
      >host = "LinuxComputerN ame" (or its Ip)
      >port = 22
      >>
      >and after runing this code I get this error:
      >socket.error : (10061, 'Connection refused')
      >
      Did you try
      >
      sftp LinuxComputerNa me
      >
      just to confirm it's got a listener on port 22?
      --

      >
      >
      --
      View this message in context: http://www.nabble.com/sftp-problem%2...p19823566.html
      Sent from the Python - python-list mailing list archive at Nabble.com.

      Comment

      • Mike Hjorleifsson

        #4
        Re: sftp problem!

        here is a snippet that works, you need to replace the default data or
        create a database etc.. and the fields to store it
        you also need to generate and share your public key to the receiving
        server to avoid password prompt


        # Standard library imports
        import os
        import sys
        import time
        import traceback
        import paramiko


        # my defaultdata is stored in a sqllite table called merchant.lib
        # you can just assign values to defaultdata or recreate a database of
        your own
        from merchant.lib import defaultdata


        # set up default values
        hostname = defaultdata.def aults['hostname']
        username = defaultdata.def aults['username']
        password = defaultdata.def aults['password']

        # paramiko required data
        port = defaultdata.def aults['port']
        fname = defaultdata.def aults['fname']
        pathupload = defaultdata.def aults['pathupload']
        pathdownload = defaultdata.def aults['pathdownload']
        input_dir = defaultdata.def aults['input_dir']
        output_dir = defaultdata.def aults['output_dir']
        curdir = os.path.abspath (os.curdir)

        # load ssh pkey you need to precreate the key and ensure the server
        side
        # has it in its authorized_keys
        pkey_path = os.path.expandu ser('~/.ssh/id_dsa')
        pkey = paramiko.DSSKey .from_private_k ey_file(pkey_pa th)

        paramiko.util.l og_to_file('tra nsfer.log')
        logger = paramiko.util.g et_logger('para miko')

        try:
        # set up connection
        t = paramiko.Transp ort((hostname, port))
        t.connect(usern ame=username, pkey=pkey)
        sftp = paramiko.SFTPCl ient.from_trans port(t)

        # upload files
        files = [file for file in os.listdir(outp ut_dir)
        if file.endswith(' .txt')]
        logger.info("UP LOAD FILES %s" % files)
        os.chdir(output _dir)
        for file in files:
        sftp.put(file, os.path.join(pa thupload, file))
        os.rename(file, file + '.old')

        # download files
        response_files = [file for file in sftp.listdir(pa thdownload)
        if file.startswith ('DISCOVER_RESP ONSE_')]

        logger.info("RE SPOSE FILES: %s" % response_files)
        os.chdir(os.pat h.join(curdir, input_dir))
        for file in response_files:
        file_path = os.path.join(pa thdownload, file)
        sftp.get(file_p ath, file)
        sftp.remove(fil e_path)


        # close connection
        t.close()

        Comment

        • sa6113

          #5
          Re: sftp problem!


          Dear Mike,
          Thanks for your help, but I am new in paramiko,would you please help me
          more?

          May I connect to server by password insted of private key? How?
          I used this code:
          ....
          t.auth_password (username, password, event)
          ....
          but I received this error :
          paramiko.SSHExc eption : No existing session

          I think my problem is in authentication, Do you think I have to use pycrypto
          module or something like that??

          If not would you please tell me how should I precreate the key and ensure
          the server
          side has it in its authorized_keys ? How do I share that as I mentioned it
          before I am using two different platforms?

          Thanks.



          Mike Hjorleifsson wrote:
          >
          here is a snippet that works, you need to replace the default data or
          create a database etc.. and the fields to store it
          you also need to generate and share your public key to the receiving
          server to avoid password prompt
          >
          >
          # Standard library imports
          import os
          import sys
          import time
          import traceback
          import paramiko
          >
          >
          # my defaultdata is stored in a sqllite table called merchant.lib
          # you can just assign values to defaultdata or recreate a database of
          your own
          from merchant.lib import defaultdata
          >
          >
          # set up default values
          hostname = defaultdata.def aults['hostname']
          username = defaultdata.def aults['username']
          password = defaultdata.def aults['password']
          >
          # paramiko required data
          port = defaultdata.def aults['port']
          fname = defaultdata.def aults['fname']
          pathupload = defaultdata.def aults['pathupload']
          pathdownload = defaultdata.def aults['pathdownload']
          input_dir = defaultdata.def aults['input_dir']
          output_dir = defaultdata.def aults['output_dir']
          curdir = os.path.abspath (os.curdir)
          >
          # load ssh pkey you need to precreate the key and ensure the server
          side
          # has it in its authorized_keys
          pkey_path = os.path.expandu ser('~/.ssh/id_dsa')
          pkey = paramiko.DSSKey .from_private_k ey_file(pkey_pa th)
          >
          paramiko.util.l og_to_file('tra nsfer.log')
          logger = paramiko.util.g et_logger('para miko')
          >
          try:
          # set up connection
          t = paramiko.Transp ort((hostname, port))
          t.connect(usern ame=username, pkey=pkey)
          sftp = paramiko.SFTPCl ient.from_trans port(t)
          >
          # upload files
          files = [file for file in os.listdir(outp ut_dir)
          if file.endswith(' .txt')]
          logger.info("UP LOAD FILES %s" % files)
          os.chdir(output _dir)
          for file in files:
          sftp.put(file, os.path.join(pa thupload, file))
          os.rename(file, file + '.old')
          >
          # download files
          response_files = [file for file in sftp.listdir(pa thdownload)
          if file.startswith ('DISCOVER_RESP ONSE_')]
          >
          logger.info("RE SPOSE FILES: %s" % response_files)
          os.chdir(os.pat h.join(curdir, input_dir))
          for file in response_files:
          file_path = os.path.join(pa thdownload, file)
          sftp.get(file_p ath, file)
          sftp.remove(fil e_path)
          >
          >
          # close connection
          t.close()
          --

          >
          >
          --
          View this message in context: http://www.nabble.com/sftp-problem%2...p19833497.html
          Sent from the Python - python-list mailing list archive at Nabble.com.

          Comment

          Working...