VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   File Transfer (https://forums.vandyke.com/forumdisplay.php?f=18)
-   -   Import pysftp module error (https://forums.vandyke.com/showthread.php?t=13585)

vikram1840 06-10-2019 09:10 AM

Import pysftp module error
 
Hello Experts,
I have to perform two task(basically have to auto schedule both task as one)

1) Connect OSS server(remote server) through secureCRT and run some script on secureCRT window(same as OSS terminal window)
2)After completion of Script have to transfer the file from remote PC to my local computer.

1st part is working fine but when I am trying to call function to compete 2nd part(sftp transfer) it showing error.
for sftp transfer I am using pysftp module which is showing error as "no module name sftp".

So could you please help to get some solution.

code to access SecureCRT and run the script:
Code:

# $language = "python"
# $interface = "1.0"

# Connect to an SSH server using the SSH2 protocol. Specify the
# username and password and hostname on the command line as well as
# some SSH2 protocol specific options.

import datetime
from datetime import date, timedelta




def main():
    host = "x.x.x.x"
    user_name = "xx"
    password = "xx"
    session_name = "xx"
    cmd = "/SSH2 /L " + session_name + " /PASSWORD " + password + " " + host
    crt.Session.Connect(cmd)
    # crt.Screen.WaitForString("Prompt from dialog that is now in terminal window")
    # crt.Screen.Send("Response")
    if crt.Session.Connected:
        crt.Screen.Synchronous = True
    row = crt.Screen.CurrentRow
    prompt = crt.Screen.Get(row, 0, row, crt.Screen.CurrentColumn - 1)
    prompt = prompt.strip()
    crt.Screen.Send("cd EM_NO\n")
    crt.Screen.WaitForString(prompt)
    logs_date = date.today() - timedelta(days=1)
    starttime = logs_date.strftime("%Y%m%d" + '.' + "0000")
    endtime = logs_date.strftime("%Y%m%d" + '.' + "2345")
    str = "sed '$ s/$/ -s " + starttime + " -e " + endtime + " -a/' Command_input.txt > Command_Input_Sudo.txt && mv Command_Input_Sudo.txt Command.txt"
    crt.Screen.Send(str + '\r')
    crt.Screen.WaitForString("Command.txt")
    crt.Screen.Send('echo "rnc" >> Command.txt' + '\r')
    crt.Screen.WaitForString("Command.txt")
    crt.Screen.Send('echo "q" >> Command.txt' + '\r')
    crt.Screen.WaitForString("Command.txt")
    crt.Screen.WaitForString(prompt)
    crt.Screen.Send("./em.sh\n")
    crt.Screen.Send("1\n")
    crt.Screen.WaitForString(">")


main()

now for 2nd part:
Code:

import pysftp as sftp
import os
import sys

def sftpfile():
    with sftp.Connection(host = 'xxxx',port=xx,username='xx',password='xx') as s:
        s.cwd('/home/xx/xxx/final/')
        directory_structure = s.listdir_attr()
        for attr in directory_structure:
            name = attr.filename
            if name.startswith('Utrancell'):
                file_name = name
        remotepath = '/home/xx/EM_NO/final/'+ file_name
        localpath = "C:\\temp\\2019\\June\\Daily_Logfile\\xx\\" + file_name
        s.get(remotepath,localpath)


sftpfile()

if I try to call function sftpfile() from main then showing error.

So please suggest if any module available to support sftp file transfer after completion of 1st task.

Regards
Vikram
+357-96346120

bgagnon 06-10-2019 10:30 AM

Hi Vikram,

What version of SecureCRT are you using?

On what OS/platform?

If on Windows, how did you import the custom module? (It's not one that's included.)

bgagnon 06-10-2019 11:10 AM

Hi Vikram,

As a note, SecureCRT does have its own sftp mechanism, the Sftp tab. See example script here.


But I was able to get pysftp working on SecureCRT/Linux. For Windows, that module may depend on too many other missing modules.

vikram1840 06-10-2019 11:31 AM

Quote:

Originally Posted by bgagnon (Post 51695)
Hi Vikram,

What version of SecureCRT are you using?

On what OS/platform?

If on Windows, how did you import the custom module? (It's not one that's included.)

I am using SecureCRT8.5 and running on windows OS.
Basically i am running my code in SecureCRT and from there I want to perfrom sftp file transfer

vikram1840 06-10-2019 11:41 AM

Quote:

Originally Posted by bgagnon (Post 51698)
Hi Vikram,

As a note, SecureCRT does have its own sftp mechanism, the Sftp tab. See example script here.


But I was able to get pysftp working on SecureCRT/Linux. For Windows, that module may depend on too many other missing modules.

if possible could you please explain how you have accessed pysftp with secureCRT.It would be quite helpful for me.

Regards
Vikram

bgagnon 06-10-2019 11:59 AM

Hi Vikram,

To clarify, I have not been able to use pysftp in SecureCRT/Windows.

On my Ubuntu Linux VM, I installed pysftp using pip and then just ran a simple script I found online, altering the print statement to a MessageBox instead:

Code:

import pysftp

srv = pysftp.Connection(host="my_host", username="my_username", password="my_pwd")

# Get the directory and file listing
data = srv.listdir()

# Closes the connection
srv.close()

# Prints out the directories and files, line by line
for i in data:
    crt.Dialog.MessageBox(i)


vikram1840 06-11-2019 03:30 AM

Quote:

Originally Posted by bgagnon (Post 51698)
Hi Vikram,

As a note, SecureCRT does have its own sftp mechanism, the Sftp tab. See example script here.


But I was able to get pysftp working on SecureCRT/Linux. For Windows, that module may depend on too many other missing modules.

I am trying to use this script with already connected SSH2 tab but unable to get stfp key on screen.
Even I have tried with right click and connect sftp connection and the same output. Still waiting for sftp> keyword.
I am using secureCRT8.5 and I think this support SFTP connection as well.

So is there anything I am missing here. Plz suggest

Regards
Vikram

bgagnon 06-11-2019 08:36 AM

Hello Vikram,
Quote:

I am trying to use this script with already connected SSH2 tab but unable to get stfp key on screen.
What is this script?

The example sftp tab script with no alterations?

Or did you incorporate that example script into your own script?

If not the example script as-is, I will need to see a copy of your script. You can post it here if it does not include sensitive data. If there is sensitive data, email it to support@vandyke.com and reference Attn Brenda - Forum Thread #13585 in the subject line.

vikram1840 06-11-2019 08:54 AM

Quote:

Originally Posted by bgagnon (Post 51706)
Hello Vikram,

What is this script?

The example sftp tab script with no alterations?

Or did you incorporate that example script into your own script?

If not the example script as-is, I will need to see a copy of your script. You can post it here if it does not include sensitive data. If there is sensitive data, email it to support@vandyke.com and reference Attn Brenda - Forum Thread #13585 in the subject line.

I am using the same script without any alteration.Just updated remote and local path.
Apart from it even without using script while I am trying to connect"coonect SFTP connection" of running SSH2 connection it also not reflecting any sftp keyword.

Regards
Vikram

bgagnon 06-11-2019 10:10 AM

Hi Vikram,

Please list the explicit steps you take for your manual (without script) results.
  • Are you connected with SSH protocol to a server that supports the SFTP subsystem?

  • Does this issue occur with all your sessions?

  • Or just some sessions?

vikram1840 06-11-2019 10:28 AM

Quote:

Originally Posted by bgagnon (Post 51708)
Hi Vikram,

Please list the explicit steps you take for your manual (without script) results.
  • Are you connected with SSH protocol to a server that supports the SFTP subsystem?

  • Does this issue occur with all your sessions?

  • Or just some sessions?

Hi One thing I have noticed is that port no. for SSH connection and SFTP connection are different.
We have port 8022 for SSH and 8024 for SFTP.
So may be due to this I am unable to get SFTP through SSH connection.

So is there any solution in this cases?

bgagnon 06-11-2019 10:39 AM

Hi Vikram,

No, not that I know of at this time.

A colleague did discover the major missing dependency that would prevent pysftp working in SecureCRT/Windows is that we don't include the _socket module.

I have added this thread to a feature request in our product enhancement database to add native support for _socket Python module on Windows. Should a future release of SecureCRT include this feature, notification will be posted here.

If you prefer direct email notification, send an email to support@vandyke.com and include "Feature Request - Forum Thread #13585" in the subject line or use this form from the support page of our website.


All times are GMT -6. The time now is 08:25 PM.