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 06-23-2009, 03:30 AM
metramo metramo is offline
Registered User
 
Join Date: Jun 2009
Posts: 10
Script a tunneling configuration

I have a session with a tunneling configuration.
But i needed some scripting logic for the start of the script.

How can I define the tunneling configuration using scripts (I need to define the same stuff as in the connection / port forwading / add
Reply With Quote
  #2  
Old 06-23-2009, 10:50 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Hello Metramo,

You can define port forwarding details using the /SSH2 command-line options /LOCAL or /REMOTE. There's information in SecureCRT Help / Contents tab / Using the Software / Command-Line Options / Table of Protocol-Specific Command-Line Options.

Quote:
/SSH2 [ssh_options] hostname

[ /LOCAL port:hostname:hostport ]
Forwards connection requests from port (on localhost) to hostport on hostname. This is known as port forwarding.

[ /REMOTE port:hostname:hostport ]
Requests the SSH2 server to accept connection requests on port (on the remote machine) and forward the requests to hostport on hostname. This is known as "remote forwarding".
For example, here is a command that connects to a machine named redhat and forwards traffic from local port 3456 to remote port 22 on a server named greenhat.
securecrt /ssh2 /local 3456:greenhat:22 redhat
Does this information help with what you're wanting to do?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 06-23-2009, 11:08 AM
metramo metramo is offline
Registered User
 
Join Date: Jun 2009
Posts: 10
This starts answering, but can I do that from a script ?

I have a script that does some stuff to figure out which ports to use a s a function of the unix session. So I need to use a port name calculated by
the script. Only at that time do I know the port numbers.
Reply With Quote
  #4  
Old 06-23-2009, 01:13 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Yes, you can run SecureCRT and setup port forward connections from a script.

You may want to take a look at our new Scripting Manual for some scripting ideas. Chapter 3 contains information showing how you could connect if you're running the script from SecureCRT. You might want to use something like the following:
crt.Session.Connect "/SSH2 /LOCAL " 3456:greenhat:22 redhat"
Code:
dim nLocalPort
nLocalPort = 3456

dim nRemotePort
nRemotePort = 22

crt.Session.Connect "/SSH2 /LOCAL " & nLocalPort & ":greenhat:" & nRemotePort & " redhat"
If you're running the script outside of SecureCRT then you will probably need to create a shell object then run SecureCRT using the shell object's .Run method.
Code:
dim nLocalPort
nLocalPort = 3456

dim nRemotePort
nRemotePort = 22

dim g_shell
Set g_shell = CreateObject("WScript.Shell")

szCmd = """C:\Program Files\VanDyke Software\Clients\SecureCRT.exe""" & " /SSH2 /LOCAL " & nLocalPort & ":192.168.0.131:" & nRemotePort & " redhat8"

g_shell.run szCmd
How do you currently start your script?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #5  
Old 06-24-2009, 01:26 AM
metramo metramo is offline
Registered User
 
Join Date: Jun 2009
Posts: 10
To be tried

Thanks. I'll try this. I run the script by chosing it in securecrt. Later, when it works I'll invoke it on the command line.
Reply With Quote
  #6  
Old 06-24-2009, 01:00 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Sounds good - I'm glad that helped provide a couple things to try.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #7  
Old 06-29-2009, 06:30 AM
metramo metramo is offline
Registered User
 
Join Date: Jun 2009
Posts: 10
A little more complexity

I don't have the available ports when I connect so I have to do it as another step, something like this

(1) Connect to ip using ssh
(2) Send a command to the screen (show userports)
parse the number
(3) Add the port forwarding
(4) continue connecting to the target IP address

So the question is if I can add the port forwarding after (not in the connect statement)
Reply With Quote
  #8  
Old 06-29-2009, 09:38 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Hi metramo,

I saw your other post about needing to parse ports and am hoping that we'll be able to provide the information you need in order to put everything together.

Can you show us what you have so far, and an example of the output you're seeing?

In your steps you indicate wanting to parse the port then "continue connecting" to the target. By the time you issue a command and parse data you are already connected and authenticated. Adding a port forward to this connection may be possible with scripting but I'll need to investigate this.

If you can parse the information then start another SecureCRT instance or tab with the port forward, would that meet your goal?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #9  
Old 07-01-2009, 01:16 AM
metramo metramo is offline
Registered User
 
Join Date: Jun 2009
Posts: 10
Progressing

Hi Again,

I'm messing around with this. Here's my script

- I tried a tip about finding the current tab's inthe beginning,
this is irrelevant at this point
- I connect to 150.132.191.11
- I figure out the available ports on 150.132.191.11 as uportl to uportf
and I declare a port forwarding of port 10022 using uportl
after this I know uportl is equal to 23729 and I could issue
" /ssh2 /local 10022:150.132.191.11:23729 150.132.191.11

this is where I need to add fort forwarding although the session is
allready open

- the other post: i have uportl and want to add 1 (convert to integer and
add 1) ->
- As a further development I'm adding a second fort forwarding on
port 20022, this would presumeably be done by cloning



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

Dim tabOP1, tabOP2
Dim tabOP1i, tabOP2i
Dim rsg1, rsg2

MsgBox "This script will connect to RSG, find available ports," + vbcrlf + "define a portforwarding and go on to HPOVXL2"+vbcrlf+"It will not define a portforwarding in SecureCRT"

crt.Screen.Synchronous = True
rsg1="150.132.191.11"
rsg2="153.88.11.201"

' This automatically generated script may need to be
' edited in order to work correctly.

Sub Main

For nIndex = 1 to crt.GetTabCount
Set objCurrentTab = crt.GetTab(nIndex)
objCurrentTab.Activate
' Close tabs that aren't connected
if objCurrentTab.Session.Connected = True then
msgbox "Tab "+cstr(nIndex)+" "+objCurrentTab.Caption
else
objCurrentTab.close
end if
Next

'crt.session.Connect("/ssh2 150.132.191.11")
set tabOP1 = crt.session.ConnectIntab("/ssh2 150.132.191.11")
'crt.session.Connect("/ssh2 153.88.11.201")
tabOP1.Activate
tabOP1.Caption = "Orange via "+tabOP1.Caption

tabOP1.Screen.WaitForString "ecgw1-lu > "
tabOP1.Screen.Send "show userports" & chr(13)
'ecgw1-lu > show userports
'Your portrange is 1998,23723-23732
crt.Screen.WaitForString( "Your portrange is " )
crt.Screen.WaitForString "ecgw1-lu > "
screenrow = crt.screen.CurrentRow - 1
Dim xtrauportsmsg, xtrauports
xtrauportsmsg = Trim(crt.Screen.Get(screenrow, 1, screenrow, 80 ))
'MsgbBox "1: "
If InStr(xtrauportsmsg,",")<>0 Then
xtrauports=Right(xtrauportsmsg,Len(xtrauportsmsg)-InStr(xtrauportsmsg,","))
'MsgBox "2: "+xtrauportsmsg+Chr(13)+Chr(10)+xtrauports
If InStr(xtrauportsmsg,"-")<>0 Then
uportf=Left(xtrauports,InStr(xtrauports,"-")-1)
'MsgBox "3: " +uportf+Chr(13)+Chr(10)+uportl
uportl=Right(xtrauports,Len(xtrauports)-InStr(xtrauports,"-"))
MsgBox "4: " +uportf+Chr(13)+Chr(10)+uportl
uporth=Cnum(uportl)
Msgbox uporth
Else
MsgBox "5: "
End If
Else
xtrauports=Right(xtrauportsmsg,Len(xtrauportsmsg)-InStr(xtrauportsmsg,"is ")-2)
'MsgBox "9: "+ +xtrauports
End If
'MsgBox "10: "+ xtrauports+Chr(13)+Chr(10)+uportf+Chr(13)+Chr(10)+uportl

'set tabOP1 = crt.Session.
tabOP1.Caption = "Orange"
tabOP1i = tabOP1.index

msgbox "LocalAddress " + tabOP1.session.LocalAddress
msgbox "RemoteAddress " + tabOP1.session.RemoteAddress
msgbox "RemotePort " + tabOP1.session.RemotePort

' Add portforwarding

'crt.Screen.Send "add "+uportf+" 10.163.198.215 22" & chr(13)
crt.Screen.Send "add "+uportl+" 10.163.198.215 22" & chr(13)
crt.Screen.WaitForString " > "

crt.Screen.Send "ssh orangefr-mms" & chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "ericssonmms" & chr(13)
crt.Screen.WaitForString "orangemms1 > "
crt.Screen.Send "show hosts" & chr(13)
crt.Screen.WaitForString "orangemms1 > "
crt.Screen.Send "ssh 10.163.198.215 eric" & chr(13)
crt.Screen.WaitForString "eric@10.163.198.215's password: "
crt.Screen.Send "........." & chr(13)
crt.Screen.WaitForString "> "
crt.Screen.Send "bash" & chr(13)
crt.Screen.Send "echo;while true; do echo `date` tunnel on `hostname`;echo;sleep 60;done" & chr(13)

set tabOP2 = tabOP1.clone
tabOP2.Caption = "SRR"
tabOP1i = tabOP1.index
tabOP2i = tabOP1.index

uportl2=uporth
MsgBox uportl+Chr(13)+Chr(10)+uportl2+1+vbcrlf
crt.Screen.Send "add "+uportl2+" 10.163.198.215 22" & chr(13)
crt.Screen.WaitForString " > "

crt.Screen.Send "ssh orangefr-mms" & chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "ericssonmms" & chr(13)
crt.Screen.WaitForString "orangemms1 > "
crt.Screen.Send "show hosts" & chr(13)
crt.Screen.WaitForString "orangemms1 > "
crt.Screen.Send "ssh 10.163.198.215 eric" & chr(13)
crt.Screen.WaitForString "eric@10.163.198.215's password: "
crt.Screen.Send "........" & chr(13)
crt.Screen.WaitForString "> "
crt.Screen.Send "bash" & chr(13)
crt.Screen.Send "echo;while true; do echo `date` tunnel on `hostname`;echo;sleep 60;done" & chr(13)

End Sub
Reply With Quote
  #10  
Old 07-01-2009, 11:18 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
After you establish what ports you're going to forward to, could you disconnect then reconnect with to the same server with the ports you want to forward, as the following?
Code:
set tabOP1 = crt.session.connectintab("/ssh2 192.168.0.131")

' 
' Find ports to forward from/to
' ...
'

tabOP1.session.disconnect

set tabOP1 = crt.session.connectintab("/ssh2 /local 10022:192.168.0.131:23729 192.168.0.131")
Starting a new instance of SecureCRT with the command line will accomplish the same thing.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]

Last edited by miked; 07-28-2009 at 01:25 PM.
Reply With Quote
  #11  
Old 08-25-2010, 04:22 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,020
The ability to use "SetOption" to modify the Port Forward Table has been fixed in a later build of SecureCRT. Any users who would like to receive this build, please send an e-mail to support@vandyke.com and reference "Attn Brenda - Forum Thread #3651" in the subject.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
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 08:20 PM.