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-13-2019, 05:32 PM
vikram1840 vikram1840 is offline
Registered User
 
Join Date: May 2019
Posts: 13
Access file name in Directory of remote machine

Hello Expert,

Please help me to find out the solution.

Problem statement- I have written python script to run in secureCRT.
This script will provide access to remote server and run some command to remote server terminal.
Now I want to access the particular file name in some directory of remote server which ends with ".log" extension and I have to add some prefix in that .log file and then transfer to our local machine.

in this problem statement I am unable to access file name end with .log.

So please suggest some solution.

Presently I am manually providing file name which I do not want in future by keep in mind to automate this system.

The Code I am using presently:

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
import os
def main():
    host = "x.x.x.x:xxxx"
    user_name = "xxx"
    password = "xxxx"
    cmd = "/SSH2 /L " + user_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 /home/shared/xxx/EM_LTE/Output/mobatch_RNC\n")
        crt.Screen.WaitForString(prompt)
        crt.Screen.Send("rm *.*\n")
        crt.Screen.Send(r"amosbatch -p 20 /home/shared/xx/EM_LTE/EnodeB_List.txt /home/shared/xxx/EM_LTE/Output/scripts_Command/Event_Monitoring_Script.txt  /home/shared/xx/EM_LTE/Output/mobatch_RNC")
        crt.Screen.WaitForString(prompt)
        crt.Screen.Send("\n")
        crt.Screen.WaitForString(prompt)
        crt.Screen.Send("rm mobatch_result.txt\n")
        crt.Screen.WaitForString(prompt)
        # for attr in os.listdir("/home/shared/xx/EM_LTE/Output/mobatch_RNC/"):
        #      name = attr.filename
        #      if name.endwith('.log'):
        #          file_name = name
        crt.Screen.Send('cd /home/shared/xx/EM_LTE/Output/mobatch_RNC\n')
        crt.Screen.WaitForString(prompt)
        crt.Screen.Send("sed -ne 's/.*/TBTX1_PA.log: &/p' TBTX1_PA.log>output.txt")
        crt.Screen.Send("\n")
        crt.Screen.WaitForString(prompt)
        crt.Screen.Send("mv output.txt TBTX1_PA.log")
        crt.Screen.Send("\n")
        crt.Screen.WaitForString(prompt)
        crt.Screen.Send("egrep -i 'EUtranCellFDD=|.EUtranCellFDD=' *.log > outputNew.txt\n")
        crt.Screen.WaitForString(prompt)
        crt.Screen.Send("egrep -i 'InterfaceIPv4=1|IpAccessHostEt=1' *.log > iub.txt\n")
        crt.Screen.WaitForString(prompt)
        crt.Screen.Send("perl -ne 'print;' *.txt > all.txt\n")
        crt.Screen.WaitForString(prompt)
        crt.Screen.WaitForString("perl -ne 'print;' *.txt > all.txt")
        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/xxxx/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()
Reply With Quote
  #2  
Old 09-16-2019, 07:20 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,976
Hi vikram1840,

I don't see where you are 'presently manually providing file name' but it does not seem you are working with the file that you piped the ".log" filenames into:

crt.Screen.Send("egrep -i 'EUtranCellFDD=|.EUtranCellFDD=' *.log > outputNew.txt\n")

Does the above contain all the target files (prior to prepending data to the filenames)?

Quote:
in this problem statement I am unable to access file name end with .log.
What do you mean by that?

Are you getting an error? If so, what error?

It would seem to me it would be easier if you captured the *.log filenames into a variable, split it on the line ending and then prepended what you need to and run the "get" command as you are already mostly doing in the sftp_transfer() function.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 09-16-2019, 09:27 AM
vikram1840 vikram1840 is offline
Registered User
 
Join Date: May 2019
Posts: 13
Quote:
Originally Posted by bgagnon View Post
Hi vikram1840,

I don't see where you are 'presently manually providing file name' but it does not seem you are working with the file that you piped the ".log" filenames into:

crt.Screen.Send("egrep -i 'EUtranCellFDD=|.EUtranCellFDD=' *.log > outputNew.txt\n")

Does the above contain all the target files (prior to prepending data to the filenames)?


What do you mean by that?

Are you getting an error? If so, what error?

It would seem to me it would be easier if you captured the *.log filenames into a variable, split it on the line ending and then prepended what you need to and run the "get" command as you are already mostly doing in the sftp_transfer() function.
Hi ,

The file name manually i have used- TBTX1_PA.log in this line

crt.Screen.Send("sed -ne 's/.*/TBTX1_PA.log: &/p' TBTX1_PA.log>output.txt").

So basically if I read the file name by sending ls command and store in a varibale then I can use that variable in place of manual intervention.
So could you please suggest me where how can I store the ls command output in varibale in this code.

Regards
Vikram
Reply With Quote
  #4  
Old 09-16-2019, 11:16 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,976
Hi vikram1840,

By using ReadString() just as you did in the sftp_transfer() function():

Code:
    strResults = objSFTPTab.Screen.ReadString("sftp> ")
Except in this case, you will wait for prompt:

Code:
    strResults = crt.Screen.ReadString(prompt)
__________________
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 07:43 PM.