View Single Post
  #1  
Old 09-16-2019, 07: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