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 08-04-2016, 03:22 AM
Cisco_ronin123 Cisco_ronin123 is offline
Registered User
 
Join Date: Aug 2016
Posts: 5
Connecting to multiple sessions: same IP address , different ports

I have deployed UnetLab very recently on my PC which is basically GNS3 + IOL . Oversimplication but should suffice for this post.

I want to create several sessions to different routers which basically exist over the same IP but different port numbers.
This should have been pretty straightforward and I wrote the following program,

def main():
hostIP = "###.###.###.###" [redacted]

crt.Screen.Synchronous = True

# CONNECT TO REMOTE DEVICE
for i in range(32769,32781):
crt.Session.Connect("/telnet "+hostIP + " " + str(i))
if not crt.Session.connected :
print "Unable to connect to remote host"

main()

However, as feared , it is unable to create more than one session over the same IP. After some reading as to understand how SCRT works, i think i have zeroed down to the fact that the session.ini file being created for each login is the same and hence it is creating a problem. Am I correct ? If yes, how do i solve this ?
If no, can you help me solve this problem ?

Last edited by bgagnon; 08-04-2016 at 07:21 AM.
Reply With Quote
  #2  
Old 08-04-2016, 08:35 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,997
Hi Cisco_ronin123,

You are correct. The Default.ini file (Default Session) is the template for ad hoc connections.

So do you want the 12 connections all open at the same time? Do you care if you save these connections as sessions?

If you don't want them permanently, you could delete them after. That's pretty much the only solution I can come up with, others in the community might have different ideas.

For example:

Code:
    for i in range(32769,32781):
        # Append port to hostIP to create unique session name
        strSessName = hostIP + "_" + str(i)
        # Open Default Session config
        objConfig = crt.OpenSessionConfiguration("Default")
        # Set the protocol
        objConfig.SetOption("Protocol Name", "Telnet")
        # Save since the options available are dependent on protocol
        objConfig.Save(strSessName)
        # Open session config
        objConfig = crt.OpenSessionConfiguration(strSessName)
        # Set Hostname and Port and Save
        objConfig.SetOption("Hostname", hostIP)
        objConfig.SetOption("Port", i)
        objConfig.Save()
        # Connect Session
        objTab = crt.Session.ConnectInTab("/S " + strSessName)
        ...
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 08-04-2016, 11:09 AM
Cisco_ronin123 Cisco_ronin123 is offline
Registered User
 
Join Date: Aug 2016
Posts: 5
I do want all the connections to be open at the same time. It's extremely important since I am in love with the Send chat to all tabs SCRT command and for that I need the connections to be active.
Reply With Quote
  #4  
Old 08-05-2016, 01:41 AM
Cisco_ronin123 Cisco_ronin123 is offline
Registered User
 
Join Date: Aug 2016
Posts: 5
Thanks for the advice, the code works. However, I do not have any exception handling here.
if crt.Session.ConnectInTab("/S " + strSessName):
objTab = crt.Session.ConnectInTab("/S " + strSessName)
else:
continue

This doesn't work. Any advice on this too ?
Reply With Quote
  #5  
Old 08-05-2016, 08:21 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,997
Hi Cisco_ronin123,

You are referring to working within the for loop, correct?

Do you want it to move on to the next IP/port if one fails?

If so, I would think the "if not connected: continue" would work for that scenario. Maybe you want to log that information to a file. If you don't want to continue, use return.

Code:
    if not crt.Session.Connected:
        crt.Dialog.MessageBox("Must be connected!")
        return
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #6  
Old 08-08-2016, 05:27 AM
Cisco_ronin123 Cisco_ronin123 is offline
Registered User
 
Join Date: Aug 2016
Posts: 5
Here is my entire script:
for i in range(32769,32790):
# Append port to hostIP to create unique session name
strSessName = hostIP + "_" + str(i)
# Open Default Session config
objConfig = crt.OpenSessionConfiguration("Default")
# Set the protocol
objConfig.SetOption("Protocol Name", "Telnet")
# Save since the options available are dependent on protocol
objConfig.Save(strSessName)
# Open session config
objConfig = crt.OpenSessionConfiguration(strSessName)
# Set Hostname and Port and Save
objConfig.SetOption("Hostname", hostIP)
objConfig.SetOption("Port", i)
objConfig.Save()
# Connect Session
objTab = crt.Session.ConnectInTab("/S " + strSessName)
if not crt.Session.connected :
print "Unable to connect to remote host"
continue


and the crt.Session.connected statement doesn't work. The parser gives an error at the crt.Session.ConnectedInTab itself as "Connection Failed".

Does this function(connectintab) return 0 and non zero values for a negative and positive connection respectively ? or it already has inbuilt exception mechanism ?


[IMG]file:///C:/Users/surahuja/Desktop/error1.PNG[/IMG]
Reply With Quote
  #7  
Old 08-08-2016, 08:29 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,997
Hi Cisco_ronin123,

Quote:
Does this function(connectintab) return 0 and non zero values for a negative and positive connection respectively ? or it already has inbuilt exception mechanism ?
No, per SecureCRT's documentation, it returns a tab object:

Quote:
ConnectInTab
Description
Connects to a session in a tab or tiled session window.
Remarks
Takes the same arguments as Connect. This also returns a Tab object.
Perhaps that's *not* the way to handle exceptions in this case.

I will have to defer to our script guru if he has time to comment later on (I'm not very Python savvy), or the community to help. I have also seen exceptions handled via a "try block" in Python:

Code:
        try:
            crt.Session.ConnectInTab("/S " + SessName)
            
        except Exception as objException:
            crt.Dialog.MessageBox(str(objException))
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #8  
Old 08-09-2016, 04:08 AM
Cisco_ronin123 Cisco_ronin123 is offline
Registered User
 
Join Date: Aug 2016
Posts: 5
Thanks for all your advice Brenda. You are right, I am making a mistake in the logic itself.
The problem is somewhat circular.
The logic should be something like this:
if Connected == True : then ConnectInTab
else
DoNotOpenTab

What is happening right now is that even if use a try catch block : there will be some events where other errors are handled. Things like "systems refusing connection" . In such cases what I need is that SCRT throws up a dialog but then does not proceed to open that connection in a tab .
Reply With Quote
  #9  
Old 08-09-2016, 11:22 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,997
Hi Cisco_ronin123,

Quote:
What is happening right now is that even if use a try catch block : there will be some events where other errors are handled. Things like "systems refusing connection" . In such cases what I need is that SCRT throws up a dialog but then does not proceed to open that connection in a tab .
For that, I believe you are going to need to setup an array of possible responses and then act on them accordingly. The example script here illustrates a way of doing that. (Specifically, the example script ReadDataFromHostFile-SendCommandsFromCommandsFile-LogResultsToIndividualFiles.py.txt, starting around line 165.)
__________________
Thanks,
--Brenda

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

Last edited by jdev; 11-09-2017 at 11:27 AM.
Reply With Quote
Reply

Tags
iol , python , securecrt 5.0


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 03:21 AM.