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 03-16-2012, 03:50 PM
_raindrop18 _raindrop18 is offline
Registered User
 
Join Date: Apr 2011
Posts: 32
open a file and write a text on specfic location

I have this text file and I want to insert a snmp community string on a file located on multiple devices
Code:
           : (system.sysName.0
                        :value (Unknown)
                )

  )
        :snmp_community (
                :read (public)
                :write ()
        )
so I want to insert " public" in the middle of parenthesis ()

is that possible to do with ScrureCRT, any insight highly appreciated.

Last edited by _raindrop18; 03-16-2012 at 04:17 PM.
Reply With Quote
  #2  
Old 03-16-2012, 04:46 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Hello,

The way you've described your project it sounds like you have a local file and want to script the modification of files on remote devices based on information you find in this file, but perhaps I'm misunderstanding.

Can you provide a little more information about your project or goal?

Are you wanting to modify a file locally and then upload that file to the devices, or are you wanting to script an interaction with the devices?

What steps do you currently take to add public inside parenthesis and get the remote device to accept the information?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 03-17-2012, 12:51 PM
_raindrop18 _raindrop18 is offline
Registered User
 
Join Date: Apr 2011
Posts: 32
thanks for the response, let me give you some easy example and if i can figure out this i can do the rest. let say I have a file name. user.txt on remote devices and i want to add(append) more users on that file. so the new users list are on my local pc as (new_user.txt) so i want the script read this new users list and append to remote device users.txt file. if i can figure how how to do that I will try to figure out the rest. I just want some basics .
again my objective is to " read the new_user list and append them on current user.txt located on remote device"
content of new_user.txt example -
# Mary Doe
# John South
# Bob Smith
thank you so very much for you assistance.

Last edited by _raindrop18; 03-17-2012 at 01:10 PM.
Reply With Quote
  #4  
Old 03-19-2012, 10:25 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Reading the file locally should be pretty straight forward. There is an example in the SecureCRT Scripting Guide, in a section titled Read Data from Separate Files: Hosts, Commands,
which shows how to read data from local files and send data to the remote.

I think you'll probably need a combination of the ReadDataFromFile() function shown in that example, and you'll need to create a function or subroutine which appends the data to the text file.

How do you currently append the data to the file?

For example, can you use a command like:
"cat 'some data' >> file.txt"
or
"sed 's/()/(public)/g' file.txt"?
Or, do you need to manually edit a text file on the remote and have a script drive the editor to make the change?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #5  
Old 03-19-2012, 10:59 AM
_raindrop18 _raindrop18 is offline
Registered User
 
Join Date: Apr 2011
Posts: 32
Quote:
"sed 's/()/(public)/g' file.txt
will work to replace the string (public) since the remote devices are Unix based. but I can't do cat on the remote file since the file need to append is on my local machine which is XP. so ithat is the reason I want use SECcrt to be a middle man to work between my pc and remote devices. as I said i can't put any file on remote device. so the new_user.txt always stay on my pc. so my objective is SECcrt read from my pc local file to push the list to remote li unix based device.
is that make sense what i am saying? thanks much again
Reply With Quote
  #6  
Old 03-19-2012, 11:43 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Quote:
but I can't do cat on the remote file since the file need to append is on my local machine which is XP.
Would echo work?

You'll read in the file locally, but finding the easiest solution on the remote is the question. If you really only ever needed to replace () with (public) then the sed command might be enough. It sounds like you really need to append data. If you were to read in a line of data from the local file, then you could issue a command to echo that data and append it to the file.

The following section of code (from the SecureCRT Scripting Manual, pp. 92-93), shows how you would send data to the remote:

Code:
' Send each command to the remote system.
For Each strCommand in vCommands
' Exit the For..Next loop if our current command is empty... ' otherwise, we'll just be pressing the Enter key pointlessly. If Trim(strCommand) = "" Then Exit For crt.Screen.Send strCommand & vbcr ' Wait for the command to complete before moving on to ' the next command crt.Screen.WaitForString strPrompt
Next
If you can use "echo" to append data then I think you would could modify the strCommand variable to be something something like this:
Code:
'... Same code as snippet above crt.Screen.Send "echo " strCommand & " >> " & "file.txt" && vbcr ' Wait for the command to complete before moving on to ' the next command crt.Screen.WaitForString strPrompt
Next
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #7  
Old 03-19-2012, 09:08 PM
_raindrop18 _raindrop18 is offline
Registered User
 
Join Date: Apr 2011
Posts: 32
i have use echo like this on the script
Quote:
crt.Screen.Send "{echo Mary Doe ; echo John South; } >> user.txt" & vbCr
and work just fine but since i have 50 lines of users list at the time, do you think any way I can list something like
Quote:
crt.Screen.Send "{
echo Mary Doe ;
echo John South;
} >> user.txt" & vbCr
writing the user list horizontally painful..I am looking to write inside on the script as above if it's possible. the way I did on above didn't work..so i am curious if there is a trick.any suggestion?
thanks .

Last edited by _raindrop18; 03-19-2012 at 09:11 PM.
Reply With Quote
  #8  
Old 03-20-2012, 10:39 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
In VBScript use an underscore to continue the line.
Code:
strCmdToSend = "echo Mary Doe ; " & _
               "echo John South;" 

crt.Screen.Send "{ " & strCmdToSend & " } >> user.txt" & vbcr
The SecureCRT Scripting Manual has quite a few examples, for example in the WaitForStrings() section.
Code:
Dim vWaitFors
vWaitFors = Array("File not found", _
                  "Access denied", _
                  "Permission denied")
Since you have local files / lists that you want to build up into echo statements, you could use some of the code I mentioned previously for reading in a file. Specifically, the script would read a file, looping through each line and appending the line of data to strCmdToSend. That would be easier and more typo-safe than manually typing each line.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #9  
Old 03-20-2012, 12:18 PM
_raindrop18 _raindrop18 is offline
Registered User
 
Join Date: Apr 2011
Posts: 32
thank you so very much, you are very help full. I used your suggestion and work just fine. as you said i prefer the script read the file instead of typing inside the file but spent hours I can't came up with that solution. my script look like this where I can modify to read the append list from file.
Quote:
#$language = "VBScript"
#$interface = "1.0"

Sub Main

Set g_shell = CreateObject("WScript.Shell")

nForReading = 1
strScriptPath = "C:\CRTscript"

' create an object file to be read - This is input file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDeviceFile = objFSO.GetFile(strScriptPath & "\device.txt")
Set objTS = objDeviceFile.OpenAsTextStream
strDevices = objTS.Read(objDeviceFile.Size)
' this line use to split line and the script read a text file one line at the time
vDevices = (split(strDevices, vbCrLf))
objTS.Close
strUser = "user"

' password will prompt for user input
strPasswd = crt.Dialog.Prompt("Enter password for " & strUser, "Login", "", True)

For i=0 to Ubound(vDevices)
vFields = split(vDevices(i), vbTab)

' which filed to be read
strDeviceNumber = vFields(0)
strDevice = strDeviceNumber

' Connect with the device using given user name and device name
'
cmd = "/SSH2 /L " & strUser & " /PASSWORD " & strPasswd & " /C 3DES /M MD5 " & strDevice
crt.Session.Connect cmd

' Start Sync with console
crt.Screen.Synchronous = True
crt.Screen.IgnoreEscape = True
' the list to be append on the file
strCmdToSend = "echo marry ; " & _
"echo john ; "
'send commnad

crt.Screen.WaitForString "#"
crt.Screen.Send "{ " & strCmdToSend & " } >> /disk2/users.txt" & vbcr
crt.Screen.WaitForString "#"


' send exit command to exit from the device
crt.Screen.Send "exit" & chr(13)

' stop Syn with the secreen
crt.Screen.Synchronous = False
'Disconnect session
crt.Session.Disconnect

'Close the out file after writting
Next

End Sub
so i have added the follwing section to read from the file , but the script doesn't like "echo"

Quote:
' Send each command to the remote system.
' create an object file to be read - This is input file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objstrCommand = objFSO.GetFile(strScriptPath & "\add.txt")
Set objTS = objstrCommand.OpenAsTextStream
strCommand = objTS.Read(objstrCommand.Size)
objTS.Close
For Each strCommand in vCommands ' Exit the For..Next loop if our current command is empty...
' otherwise, we'll just be pressing the Enter key pointlessly.
If Trim(strCommand) = "" Then Exit For
crt.Screen.Send strCommand & vbcr
Next
' Wait for the command to complete before moving on to
' the next command
crt.Screen.WaitForString strPromptNext
'1st commnad
crt.Screen.Send "echo " strCommand & " >> " & /disk2/users.txt" && vbcr
crt.Screen.WaitForString strPrompt
Next
did you see any error on the code
Reply With Quote
  #10  
Old 03-20-2012, 01:14 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Quote:
crt.Screen.Send "echo " strCommand & " >> " & /disk2/users.txt" && vbcr
It looks like you're missing an '&' ampersand between "echo " and strCommand. There are two ampersands before vbcr. There's a missing quotation mark in "/disk2/users.txt", or an extra one after >>. Does the following work better for you?
crt.Screen.Send "echo " & strCommand & " >> /disk2/users.txt" & vbcr
I've tried to make the items in between the ampersands stand out with bold so that it's a little easier to see where each unit is that you're concatenating together with the ampersands.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]

Last edited by miked; 03-20-2012 at 01:31 PM.
Reply With Quote
  #11  
Old 03-20-2012, 04:09 PM
_raindrop18 _raindrop18 is offline
Registered User
 
Join Date: Apr 2011
Posts: 32
the error gone but the command sent to the device looks like this
Quote:
device[/] # echo mary doe

>> /disk2/fw.txt
so the command should on one line but here is on 2 separet lines, so the echo result not saved in user.txt instead just dispaly "mary doe" on console, so how I can modify the script write the command on one line.
Reply With Quote
  #12  
Old 03-20-2012, 05:12 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
The questions seem to be less about SecureCRT scripting and more about general VBScript statement construction. You may want to use MsgBox before sending strCommand just to make sure you know what strCommand looks like. I just tested a short script which looks very similar to yours, and it seems to work properly:
Code:
strCmdToSend = "echo marry ; " & _
               "echo john ; " 

crt.Screen.WaitForString "$" 

crt.Screen.Send "{ " & strCmdToSend & " } >> ./users.txt" & vbcr
Here's the output from the script:
Quote:
~/tmp $ { echo marry ; echo john ; } >> ./users.txt
~/tmp $ cat users.txt
marry
john
This is outside the scope of SecureCRT, but looking at your code I wondered about the following lines:

Code:
For Each strCommand in vCommands ' Exit the For..Next loop if our current command is empty...
' otherwise, we'll just be pressing the Enter key pointlessly.
If Trim(strCommand) = "" Then Exit For
crt.Screen.Send strCommand & vbcr
Next
' Wait for the command to complete before moving on to
' the next command
crt.Screen.WaitForString strPromptNext 
'1st commnad
crt.Screen.Send "echo " strCommand & " >> " & /disk2/users.txt" && vbcrcrt.Screen.WaitForString strPrompt
Next 
The first bolded line where you're sending strCommand with nothing else looks a little strange, as does the first bolded Next which follows.

Are you wanting to build up the complete command here instead of sending data?
Code:
If Trim(strCommand) = "" Then Exit For
crt.Screen.Send strCommand & vbcr  <---- This looks wrong, to me.  
Next
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #13  
Old 03-20-2012, 05:22 PM
_raindrop18 _raindrop18 is offline
Registered User
 
Join Date: Apr 2011
Posts: 32
thanks i have corrected based on your suggestion.
Quote:
#$language = "VBScript"
#$interface = "1.0"

Sub Main

Set g_shell = CreateObject("WScript.Shell")
nForReading = 1
strScriptPath = "C:\CRTscript"

' create an object file to be read - This is input file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDeviceFile = objFSO.GetFile(strScriptPath & "\user.txt")
Set objTS = objDeviceFile.OpenAsTextStream
strDevices = objTS.Read(objDeviceFile.Size)
' this line use to split line and the script read a text file one line at the time
vDevices = (split(strDevices, vbCrLf))
objTS.Close
strUser = "user"
' password will prompt for user input
strPasswd = crt.Dialog.Prompt("Enter password for " & strUser, "Login", "", True)
For i=0 to Ubound(vDevices)
vFields = split(vDevices(i), vbTab)
' which filed to be read
strDeviceNumber = vFields(0)
strDevice = strDeviceNumber

' Connect with the device using given user name and device name
cmd = "/SSH2 /L " & strUser & " /PASSWORD " & strPasswd & " /C 3DES /M MD5 " & strDevice
On Error Resume Next
crt.Session.Connect cmd
' Start Sync with console
crt.Screen.Synchronous = True
crt.Screen.IgnoreEscape = True

' create an object file to be read - This is input file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objstrCommand = objFSO.GetFile(strScriptPath & "\new_users.txt")
Set objTS = objstrCommand.OpenAsTextStream
strCommand = objTS.Read(objstrCommand.Size)
vCommands = (split(strCommand, vbCrLf))
Next
For Each strCommand in vCommands
If Trim(strCommands) = "" Then Exit For
Next
'1st command
crt.Screen.WaitForString "#"
'crt.Screen.Send "echo " & strCommand & " >> /disk2/fw.txt" & vbcr
crt.Screen.Send "echo " & strCommand & ">> " & "/disk2/users.txt " & vbcr
crt.Screen.WaitForString "#"
crt.Screen.WaitForString "prompt$"


' send exit command to exit from the device
crt.Screen.Send "exit" & chr(13)

' stop Syn with the secreen
crt.Screen.Synchronous = False
'Disconnect session
crt.Session.Disconnect
objTS.Close
End Sub
the problem appers to be ok now, the only issue remain..the script only read only the first line from new_user.txt file. I know I missed something but not figure out so far. I belive some where here is the error
Quote:


create an object file to be read - This is input file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objstrCommand = objFSO.GetFile(strScriptPath & "\add.txt")
Set objTS = objstrCommand.OpenAsTextStream
strCommand = objTS.Read(objstrCommand.Size)
vCommands = (split(strCommand, vbCrLf))
Next
For Each strCommand in vCommands
If Trim(strCommands) = "" Then Exit For
Next
Reply With Quote
  #14  
Old 03-20-2012, 06:20 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
I think this question is mostly about the VBScript FileSystemObject. Are you sure you want to use the Read method, or do you want to use ReadAll?

I've bolded and italicized your code where I think the two problems may be.

Alternatively, you might want to write your own ReadDataFromFile function (you can copy it out of the SecureCRT Scripting Manual, page 93).
Code:
#$language = "VBScript"
#$interface = "1.0"

Sub Main

    Set g_shell = CreateObject("WScript.Shell")
    nForReading = 1
    strScriptPath = "C:\CRTscript"

    ' create an object file to be read - This is input file
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objDeviceFile = objFSO.GetFile(strScriptPath & "\user.txt")
    Set objTS = objDeviceFile.OpenAsTextStream

    strDevices = objTS.Read(objDeviceFile.Size)    ' this line use to split line and the script read a text file one line at the time

    vDevices = (split(strDevices, vbCrLf))
    objTS.Close
    strUser = "user"
    ' password will prompt for user input
    strPasswd = crt.Dialog.Prompt("Enter password for " & strUser, "Login", "", True)
    For i=0 to Ubound(vDevices)
        vFields = split(vDevices(i), vbTab)
        ' which filed to be read
        strDeviceNumber = vFields(0)
        strDevice = strDeviceNumber 

        ' Connect with the device using given user name and device name
        cmd = "/SSH2 /L " & strUser & " /PASSWORD " & strPasswd & " /C 3DES /M MD5 " & strDevice
        On Error Resume Next
        crt.Session.Connect cmd
        ' Start Sync with console 
        crt.Screen.Synchronous = True
        crt.Screen.IgnoreEscape = True 

        ' create an object file to be read - This is input file
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objstrCommand = objFSO.GetFile(strScriptPath & "\new_users.txt")
        Set objTS = objstrCommand.OpenAsTextStream

        strCommand = objTS.Read(objstrCommand.Size)

        vCommands = (split(strCommand, vbCrLf))
    Next
    
    For Each strCommand in vCommands
        If Trim(strCommands) = "" Then Exit For
    Next
    
    '1st command
    crt.Screen.WaitForString "#"
    'crt.Screen.Send "echo " & strCommand & " >> /disk2/fw.txt" & vbcr
    crt.Screen.Send "echo " & strCommand & ">> " & "/disk2/users.txt " & vbcr
    crt.Screen.WaitForString "#"
    crt.Screen.WaitForString "prompt$"


    ' send exit command to exit from the device 
    crt.Screen.Send "exit" & chr(13)

    ' stop Syn with the secreen
    crt.Screen.Synchronous = False
    'Disconnect session 
    crt.Session.Disconnect
    objTS.Close 
End Sub
Also, I'm a little concerned that there's an On Error Resume Next without a corresponding On Error Goto 0.

In general, code should have both or neither. Put On Error Goto 0 into the code as soon as possible after the On Error Resume Next.

Disabling error reporting with On Error Resume Next is helpful at times, because it hides error notification. This is done so that you can handle the error yourself rather than having the script stop execution when there's an error. You want to do this in cases where you think "failure" is a normal event. In the code above, adding On Error Resume Next before a connection attempt is great because it will allow your code to determine whether or not the connection attempt was successful and proceed appropriately. Use On Error Goto 0 as soon as you know the result.

This idea is discussed in more detail, and more eloquently, in the SecureCRT Scripting Manual (page 32).
Code:
' Instruct the script host we want to handle errors ourselves, now:
On Error Resume Next

' Attempt to connect to the remote machine:
crt.Session.Connect "/SSH2 /L user /PASSWORD vuln3r4ble 192.168.0.2"

' Capture error code and description (if any)
nError = Err.Number
strErr = Err.Description

' Now, tell the script host that it should handle errors as usual now:
On Error Goto 0

If nError <> 0 Then
    ' Handle the error...
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]

Last edited by miked; 03-20-2012 at 06:38 PM.
Reply With Quote
  #15  
Old 03-21-2012, 12:35 PM
_raindrop18 _raindrop18 is offline
Registered User
 
Join Date: Apr 2011
Posts: 32
ok I got it finally, thank you so very much for your time and patence. I am really apperciated.
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 10:40 PM.