VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   SecureCRT vbs script Read files log in a lot of Unix (https://forums.vandyke.com/showthread.php?t=13345)

amy_itx_aps 12-31-2018 12:26 AM

SecureCRT vbs script Read files log in a lot of Unix
 
The vbs script reads the information in the file and logs in to Unix in bulk.



1. ip_uu_pass_path.txt four columns of information

ip user password path
192.168.80.123 oracle oracle /home/oracle/data
192.168.80.124 app app /home/app
192.168.80.3 han han /home/han

2.ssh2_auto_read-ip_user_pass.vbs

ssh2_auto_read-ip_user_pass.vbs Loop read ip_uu_pass_path.txt login Unix


ssh2_auto_read-ip_user_pass.vbs Script content:

#$language = "VBScript"
#$interface = "1.0"

'turn on synchronous mode so we don't miss any data
crt.Screen.Synchronous = True


Sub Main

Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("ip_uu_pass.txt")

crt.Window.Show 3


Do Until file.AtEndOfStream
line = file.ReadLine
commendAndLog line
crt.screen.send chr(13)


Loop



End Sub



Sub commendAndLog(line)
' turn off synchronous mode to restore normal input processing

crt.Screen.Synchronous = False

equipmentInfo = Split(line)
ip = equipmentInfo(0)
uname = equipmentInfo(1)
pwd = equipmentInfo(2)


'login unix
crt.session.ConnectInTab "/SSH2 /L "&uname&" /PASSWORD "&pwd&" "&ip

crt.screen.clear

crt.Screen.Send "pwd" & chr(13)

crt.Screen.Send "clear && pwd && whoami" & chr(13)


End Sub

ekoranyi 12-31-2018 10:33 AM

Hi amy_itx_aps,

What, if any, trouble are you having with the script? Is there anything I can help with?

amy_itx_aps 12-31-2018 10:01 PM

vbs scripts
 
Yes, I have had some trouble. I want my script to switch to the corresponding directory after reading the Unix in the text one by one. My script can only log in. I can't execute some commands after logging in.


file: ip_uu_pass.txt

192.168.80.123 oracle oracle /home/oracle/data
192.168.80.124 app app /home/app
192.168.80.3 han han /home/han

vbs scripts

#$language = "VBScript"
#$interface = "1.0"


'turn on synchronous mode so we don't miss any data
crt.Screen.Synchronous = True
'crt.Screen.Synchronous = True

Sub Main
Set fso = CreateObject("Scripting.FileSystemObject")

Set file = fso.OpenTextFile("ip_uu_pass.txt")
crt.Window.Show 3


Do Until file.AtEndOfStream
line = file.ReadLine
commendAndLog line
Loop

'crt.Quit()

End Sub



Sub commendAndLog(line)
' turn off synchronous mode to restore normal input processing

crt.Screen.Synchronous = False

equipmentInfo = Split(line)
ip = equipmentInfo(0)
uname = equipmentInfo(1)
pwd = equipmentInfo(2)

Call crt.Session.Log(False)


'crt.Screen.WaitForString("$")
crt.Screen.Synchronous = True
crt.session.ConnectInTab "/SSH2 /L "&uname&" /PASSWORD "&pwd&" "&ip

crt.Screen.Send "export TMOUT=86400" & chr(13)
crt.Screen.WaitForString "export TMOUT=86400"

End Sub

amy_itx_aps 12-31-2018 11:21 PM

3 Attachment(s)
I want a script that reads the ip user password path in the host.txt file. Use ssh2 to log in to the unix host and execute some commands on each host.

Host.txt :
192.168.80.123 oracle oracle /home/oracle/data
192.168.80.124 app app /home/app
192.168.80.3 han han /home/han

like this:
After logging in to 192.168.80.123, execute cd /home/oracle/data/
Login 192.168.80.124 After successful Execute cd /home/app
After logging in 192.168.80.3, execute cd /home/app


my vbs scripts

#$language = "VBScript"
#$interface = "1.0"

'turn on synchronous mode so we don't miss any data
crt.Screen.Synchronous = True
'crt.Screen.Synchronous = True

Sub Main

Set fso = CreateObject("Scripting.FileSystemObject")

Set file = fso.OpenTextFile("ip_uu_pass.txt")
crt.Window.Show 3

Do Until file.AtEndOfStream
line = file.ReadLine
commendAndLog line
Loop

'crt.Quit()

End Sub



Sub commendAndLog(line)
' turn off synchronous mode to restore normal input processing

crt.Screen.Synchronous = False

equipmentInfo = Split(line)
ip = equipmentInfo(0)
uname = equipmentInfo(1)
pwd = equipmentInfo(2)

Call crt.Session.Log(False)


'crt.Screen.WaitForString("$")
crt.Screen.Synchronous = True
crt.session.ConnectInTab "/SSH2 /L "&uname&" /PASSWORD "&pwd&" "&ip

crt.Screen.Send "export TMOUT=86400" & chr(13)
crt.Screen.WaitForString "export TMOUT=86400"

End Sub

ekoranyi 01-03-2019 09:47 AM

Hi amy_itx_aps,

After taking a look at the script I don't see that you're capturing or using the path.

I see that you're using a Split to get the IP, Username, and Password but do not capture the file path.

Code:

equipmentInfo = Split(line)
ip = equipmentInfo(0)
uname = equipmentInfo(1)
pwd = equipmentInfo(2)

You may be able to get the path using similar code.

Code:

path = equipmentInfo(3)
Towards the end of the script I see that you're connecting using ConnectInTab but the only other thing sent is an export TMOUT.

Code:

crt.session.ConnectInTab "/SSH2 /L "&uname&" /PASSWORD "&pwd&" "&ip

crt.Screen.Send "export TMOUT=86400" & chr(13)
crt.Screen.WaitForString "export TMOUT=86400"

Did you want to change directories before or after sending the TMOUT?

amy_itx_aps 01-06-2019 08:15 AM

vbs scripts
 
Yes, I need to execute some commands export TMOUT=86400 is a command I used to test to see if it can be executed. My ultimate goal is to read the value of path = equipmentInfo(3) element and execute the cd command to switch to the specified Under contents

Host.txt:
192.168.80.123 oracle oracle /home/oracle/data
192.168.80.124 app app /home/app
192.168.80.3 han han /home/han

equipmentInfo = Split(line)
Ip = equipmentInfo(0)
Uname = equipmentInfo(1)
Pwd = equipmentInfo(2)
#add path As you said, You may be able to get the path using similar code.
Path = equipmentInfo(3)


I started logging in and executed cd path = equipmentInfo(3) How to write?
crt.session.ConnectInTab "/SSH2 /L "&uname&" /PASSWORD "&pwd&" "&ip

ekoranyi 01-06-2019 10:52 AM

Hi amy_itx_aps,

It's still not clear to me if you need to run the CD command before or after the export command. In general, it would be sent to the remote much the same way that you're sending the export command.

Code:

crt.Screen.Send "cd " & path & chr(13)
You will typically want to wait for an indication that the remote system is ready to accept the command. This can be done by waiting for the prompt. Borrowing from your code, it may look something like:

Code:

crt.Screen.WaitForString("$")
crt.Screen.Send "cd " & path & chr(13)

You would then want to wait for an indication the command finished and the remote is ready for the next command before
issuing the next command.

amy_itx_aps 01-07-2019 09:41 AM

vbs scripts
 
1 Attachment(s)
#$language = "VBScript"
#$interface = "1.0"

'turn on synchronous mode so we don't miss any data
crt.Screen.Synchronous = True


Sub Main

Set fso = CreateObject("Scripting.FileSystemObject")

Set file = fso.OpenTextFile("ip_uu_pass.txt")

crt.Window.Show 3



Do Until file.AtEndOfStream
line = file.ReadLine
commendAndLog line
Loop

'crt.Quit()

End Sub


Sub commendAndLog(line)
' turn off synchronous mode to restore normal input processing

'crt.Screen.Synchronous = False
crt.Screen.Synchronous = True

equipmentInfo = Split(line)

ip = equipmentInfo(0)
uname = equipmentInfo(1)
pwd = equipmentInfo(2)
path = equipmentInfo(3)


crt.session.ConnectInTab "/SSH2 /L "&uname&" /PASSWORD "&pwd&" "&ip


crt.Screen.WaitForString("$")

crt.Screen.Send " cd " &path & chr(13)

End Sub




ip_uu_pass.txt:

192.168.80.123 oracle oracle /home/oracle/data
192.168.80.124 app app /home/app
192.168.80.3 han han /home/han

At the end of the code


crt.Screen.WaitForString("$")

crt.Screen.Send " cd " &path & chr(13)


Added two lines at the end of the code
crt.Screen.WaitForString("$")
crt.Screen.Send " cd " &path & chr(13)

The two lines failed to achieve the expected purpose, the first unix host executed cd /home/oracle/data cd /home/app cd /home/han Obviously this is not my expected effect, it should be one by one Corresponding to cd to the target directory

ekoranyi 01-07-2019 10:22 AM

Hi amy_itx_aps,

I apologize, I missed a small but important feature. Rather than sending the command using crt.Screen.Send, we need to be sure to specify which tab the command gets sent to. This can be done by modifying the ConnectInTab line and the Send/WaitForString lines.

Code:

#$language = "VBScript"
#$interface = "1.0"

'turn on synchronous mode so we don't miss any data
crt.Screen.Synchronous = True

Sub Main

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile("C:\Users\user\Desktop\test.txt")

    Do Until file.AtEndOfStream
    line = file.ReadLine
    commendAndLog line
    Loop

End Sub

Sub commendAndLog(line)

    equipmentInfo = Split(line)

    ip = equipmentInfo(0)
    uname = equipmentInfo(1)
    pwd = equipmentInfo(2)
    path = equipmentInfo(3)

    Set objTab = crt.session.ConnectInTab("/SSH2 /L " & uname & " /PASSWORD " & pwd & " " & ip)

    objTab.Screen.WaitForString("$")
    objTab.Screen.Send " cd " & path & chr(13)
    objTab.Screen.WaitForString("$")

End Sub

Does making these changes give you the desired behavior?

amy_itx_aps 01-09-2019 08:49 AM

vbs scripts
 
That's great! Your code is perfect :)

ekoranyi 01-09-2019 09:38 AM

amy_itx_aps,

I'm glad to hear it's working well for you.

Have a great rest of the day.


All times are GMT -6. The time now is 05:13 AM.