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