Welcome to the VanDyke Software Forums

Join the discussion today!

Go Back   VanDyke Software Forums > File Transfer

Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old 06-10-2019, 08:10 AM
vikram1840 vikram1840 is offline
Registered User
Join Date: May 2019
Posts: 21
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:
# $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.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")
    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.Send('echo "rnc" >> Command.txt' + '\r')
    crt.Screen.Send('echo "q" >> Command.txt' + '\r')

now for 2nd part:
import pysftp as sftp
import os
import sys

def sftpfile():
     with sftp.Connection(host = 'xxxx',port=xx,username='xx',password='xx') as s:
         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

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.

Reply With Quote

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -6. The time now is 12:32 PM.