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 07-21-2016, 03:37 PM
rleon rleon is offline
Registered User
 
Join Date: Jan 2008
Posts: 112
loop in python

Hi everyone

I just switched over to mac and missing some of my basic vb scripts.

I had a nice vb script that would open a file .. read each host and run commands and waits on each host.

How would i duplicate this in phyton for mac?

thanks
Reply With Quote
  #2  
Old 07-21-2016, 03:45 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,316
Hi rleon,

See this post for the specific example script you mentioned.

There may be Python examples of additional scripts in this post.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730

Last edited by bgagnon; 07-21-2016 at 03:49 PM.
Reply With Quote
  #3  
Old 07-21-2016, 03:57 PM
rleon rleon is offline
Registered User
 
Join Date: Jan 2008
Posts: 112
I saw that but it was allot and a little fancier than I really needed.

I basically need

for host in `cat hosts`
do
cmd1
wait for x
cmd2
wait for x
cmd 3
wait for x
done
Reply With Quote
  #4  
Old 07-22-2016, 09:30 AM
rleon rleon is offline
Registered User
 
Join Date: Jan 2008
Posts: 112
This is the script I have been using for some years and worked great.

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

' This script demonstrates how to open a text file and it line by
' line to a server.

' Constants used by OpenTextFile()
'
Const ForReading = 1
Const ForWriting = 2

	pass = crt.Dialog.Prompt("Enter your password:", "Logon Script", "", True)
	If pass = "" Then
	' error handling
	End If


Sub Main

  Dim fso, file, str
  Set fso = CreateObject("Scripting.FileSystemObject")

  ' Note: A runtime exception will be generated if 'input.txt' doesn't exist.
  '
  Set file = fso.OpenTextFile("C:\Users\03339718\Documents\Program Files\crt_vbscripts\mass_work\host_lists.txt", ForReading, False)

  crt.Screen.Synchronous = True

  Do While file.AtEndOfStream <> True

    str = file.Readline

    ' Send the line with an appended CR
    '
	crt.Screen.Send “ssh " & str & Chr(13)

	crt.Screen.WaitForString "#"

	crt.Screen.Send "vi /etc/cron.allow" & vbCr

	crt.Screen.WaitForString "#"


    ' Wait for my prompt before sending the next line
    '
    crt.Screen.WaitForString ">"
  Loop

  crt.Screen.Synchronous = False

End Sub


Im stealing bits and pieces from the other scripts to recreate it in python but if anyone has something already done please share
Reply With Quote
  #5  
Old 07-22-2016, 09:44 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,316
Hi rleon,

We can help with issues specific to SecureCRT, but we don't write custom scripts, so you will need to rely on the forum community for help.

The SecureCRT objects, methods and properties do not differ that much between VBScript and Python. It's mostly just the syntax/structure required by Python that you will probably need to familiarize yourself with.

There are some resources mentioned in this thread that you might find useful.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #6  
Old 07-22-2016, 09:57 AM
rleon rleon is offline
Registered User
 
Join Date: Jan 2008
Posts: 112
im working on it .. i just cant get it to loop.

Code:
# $language = "python"
# $interface = "1.0"

def main():
        crt.Screen.Synchronous = True

        for g_strHost in open("/Users/rleon16/hosts", "r"):

         crt.Screen.Send("s " + line + '\r')

        crt.Screen.WaitForString("word: ")
        crt.Screen.Send("changeme" + chr(13))
        crt.Screen.WaitForString("-> ")
        crt.Screen.Send("exit" + chr(13))

        crt.Screen.WaitForString("# ")

        crt.Screen.Synchronous = False


main()
Reply With Quote
  #7  
Old 07-26-2016, 09:05 AM
rleon rleon is offline
Registered User
 
Join Date: Jan 2008
Posts: 112
Ok .. this works no errors .. but it only gets the first host in the list.

seems like its waiting for something but I cant figure out what it is waiting for. Any suggestions?

I would also like to add a timeout .. incase a host doesnt respond.

Code:
def main():
        crt.Screen.Synchronous = False

with open('/crt_scripts/hosts') as hosts_:
        for host in hosts_:
                crt.Screen.Send("ssh " + host + '\r')
                crt.Screen.WaitForString("word: ")
                crt.Screen.Send("mypass" + chr(13))
                crt.Screen.WaitForString("$ ")
                crt.Screen.Send("exit" + chr(13))
        Loop

main()
Reply With Quote
  #8  
Old 07-26-2016, 11:20 AM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 998
Loop is not something that Python knows about. That's a VBScript leftover and will need to be removed from your Python code.

Timeouts are possible. But, it's better to instead use WaitForStrings() to wait for those informational text snippets that appear in the success or failure cases, and handle them each accordingly.

Here's an example that shows you how to handle jump host connection scenarios:

Example Script: Jump Host - Handle Secondary Host Connection Attempts

You can find numerous examples of python scripts that perform looping, reading through files, etc. on the Script Examples "sticky" in the Scripting forum.

FYI.
--Jake
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
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 01:56 AM.