Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 09-01-2016, 03:14 PM
HEMISixty HEMISixty is offline
Registered User
 
Join Date: Sep 2016
Posts: 7
Passing Session Name to Python Script

I log into a host and then into a destination server...typical jump box scenario. I want to run a script that uses the session name to determine the host to login to.

I currently have individual scripts for each remote server. I'd like to condense them down to one script.

Currently I use this:

objTab.Screen.Send("ssh user@server01" + "\r")

I'd like to use something that replaces "server01" with the session name so that I can use one script for all of my sessions.

Is there a variable I can use to do this? I've googled a bit and can't seem to locate an example to use...
Reply With Quote
  #2  
Old 09-02-2016, 12:10 AM
noelvd's Avatar
noelvd noelvd is offline
Registered User
 
Join Date: Jul 2004
Location: Belgium
Posts: 17
Ask for server

The simplest way to do this, is to ask in the script, which server to connect to:

Code:
str_SRV =  crt.Dialog.Prompt(txt,"Which server do you want to connect to ? ","Default_Server",False)
Reply With Quote
  #3  
Old 09-02-2016, 08:21 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
Hi HEMISixty,

That's an interesting project and it sounds like it would be a useful script that would benefit the larger community. However, there is not currently an object for iterating over sessions.

I have added this thread to a feature request in our product enhancement database to add Sessions object to scripting API for iterating over all sessions. Should a future release of SecureCRT include this feature, notification will be posted here.

If you prefer direct email notification, send an email to support@vandyke.com and include "Feature Request - Forum Thread #12464" in the subject line or use this form from the support page of our website.

I think there might be a workaround, but all the script examples I can think of are probably VBScript.

Is SecureCRT installed on Mac or Linux OS?

Or are you just choosing to use Python on Windows?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #4  
Old 09-02-2016, 09:29 AM
noelvd's Avatar
noelvd noelvd is offline
Registered User
 
Join Date: Jul 2004
Location: Belgium
Posts: 17
Connect via Jump Box

I've been using the 'Jump Box' approach for many (10+) years.
Just have the script connect to the JumpBox, then read (from a .txt file) the names of the servers to connect to. That works great !

Best regards,
Noel
Reply With Quote
  #5  
Old 09-02-2016, 11:24 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
Hi HEMISixty,

A colleague came up with a snippet of code that might help you to accomplish your objective:

Code:
def Main():
    # Get the session path... since on Windows, we might get a back-slash
    # char instead of a /, convert any '\' received into '/':
    strSessionPath = crt.Session.Path.replace("\\", "/")
    
    # Now, split on "/" and take the first one from the right ([-1]),
    # which will be our session name:
    strSessionName = strSessionPath.split("/")[-1]
    
    # Create a string, formatted to present info nicely:
    strInfo = "Session Path:\t{0}\r\nSession Name:\t{1}".format(
        strSessionPath, strSessionName)
    # Display the info using the nicely-created string
    crt.Dialog.MessageBox(strInfo)
    
Main()
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #6  
Old 09-02-2016, 12:52 PM
HEMISixty HEMISixty is offline
Registered User
 
Join Date: Sep 2016
Posts: 7
Hi Brenda,

I'll give the snippet a try. It's a python script on a Mac.

it's a short script:

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

def Main():
szPrompt = "\r\n" # "localhost:~ username$"
objTab = crt.GetScriptTab()
objTab.Screen.Synchronous = True
objTab.Screen.IgnoreEscape = True
objTab.Screen.WaitForString("username$ ")
objTab.Screen.Send("quit" + "\r")
szCommand = "Script done, output file is "
objTab.Screen.WaitForString(szCommand)
szResult = objTab.Screen.ReadString(szPrompt)
objTab.Screen.Send("rm " + szResult + "\r")
objTab.Screen.WaitForString("username$ ")
objTab.Screen.Send("ssh -o ServerAliveInterval=60 username@destnationserver" + "\r")
objTab.Screen.WaitForString("ssword: ")
passwd = crt.Dialog.Prompt("Passwd: ", "", "", True)
objTab.Screen.Send(passwd + "\r")
Main()
Reply With Quote
Reply

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 02:27 PM.