Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 09-16-2019, 06:27 AM
vikram1840 vikram1840 is offline
Registered User
 
Join Date: May 2019
Posts: 13
Code not executed as per script

Hello Expert,

Please help to resolve issue I am facing.

My purpose
1) To autoconnect SSh2 and run some command and store the output
2) Transfer the stored output to my local PC through sftp connection.

In my cases I have written two function one is main() in which I have written code to autoconnect SSh2 and run some command at remote server and inside this main I am calling sftp_transfer function.
Means once command will finish then only call sftp_transfer function.

In my code it happens that without executing main function completely it call to transfer function.
so plz suggest.
I am doing some mistake with waitforstring I think so.


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

def main():
    host = "x.x.x.x"
    user_name = "xxxx"
    password = "xxxx"
    cmd = "/SSH2 /L " + user_name + " /PASSWORD " + password + " " + host
    crt.Session.Connect(cmd)
    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_LTE\n")
    crt.Screen.WaitForString(prompt)
    crt.Screen.Send("./em.sh\n")
    crt.Screen.Send("1\n")
    crt.Screen.WaitForString(prompt)
    sftp_transfer()

def sftp_transfer():
    objSFTPTab = crt.GetScriptTab().ConnectSftp()
    objSFTPTab.Screen.Synchronous = False
    while True:
        strScreenText = objSFTPTab.Screen.Get2(
            1,
            1,
            crt.Screen.Rows,
            crt.Screen.Columns)
        if "sftp> " in strScreenText:
            break
        crt.Sleep(10)
    objSFTPTab.Screen.Synchronous = True
    objSFTPTab.Screen.Send("ls -l\r")
    strFileListing = objSFTPTab.Screen.ReadString("sftp> ")
    objSFTPTab.Screen.Send("lcd C:\Macro\ENM\r")
    objSFTPTab.Screen.WaitForString("sftp> ")
    objSFTPTab.Screen.Send("cd /home/shared/f22220/EM_LTE/Output/mobatch_RNC\r")
    objSFTPTab.Screen.WaitForString("sftp> ")
    objSFTPTab.Screen.Send("get all.txt\r")
    strResults = objSFTPTab.Screen.ReadString("sftp> ")
    if "100%" in strResults:
        vDataLines = strResults.split("\r\n")
        crt.Dialog.MessageBox("File transfer successful: \r\r" + vDataLines[-2])
    else:
        crt.Dialog.MessageBox("Failed to get file!\r\r" + strResults)

    # Close the SFTP tab once we're done
    objSFTPTab.Close




main()
in above code without completion of autoconnect it call sftp_transfer function which is not desirable.
Reply With Quote
  #2  
Old 09-16-2019, 10:04 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,997
Hi vikram1840,

This may be the cause:
Code:
    crt.Screen.Send("./em.sh\n")
    crt.Screen.Send("1\n")
The "golden rule" for keeping your script in sync is to always have a WaitForString() following a Send().

And when you *send*, you should use \r as that is what simulates the ENTER key.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply


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

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 03:44 PM.