1

I am trying to ssh through .tcl script from ActiveState TCL 'tclsh' window. Having WINDOWS OS system.

#!/bin/sh    
# \    
exec tclsh "$0" ${1+"$@"}    
package require Expect


set user [lindex $argv 0]    
set password [lindex $argv 1]    
set DeviceIpAddr [lindex $argv 2]    
set DeviceHostName [lindex $argv 3]    

foreach DeviceIp $DeviceIpAddr HostName $DeviceHostName {    

    spawn ssh $DeviceIp     
     expect "login as:"    

    send "$user\r"    
    expect "Password:"    

    send "$password\r"    
    expect "$HostName ~]#"    
}    

I see below error while execute in tclsh(ActiveTCL)

% tclsh Test.tcl root 321 17.250.217.151 lb02va    
The system cannot find the file specified.         
    while executing    
"spawn ssh  root@$DeviceIp"       
    ("foreach" body line 3)  
    invoked from within    
"foreach DeviceIp $DeviceIpAddr HostName $DeviceHostName {     

        spawn ssh  root@$DeviceIp           
        expect "login as:"     

        send "$user\r"    
        expect "Password:"     

        send..."    
    (file "Test.tcl" line 12)    
child process exited abnormally    

Kindly assist me resolving this. Thank you.

Praveen
  • 21
  • 3
  • 2
    What operating system is this? Where is the `ssh` program installed? What is your command path? In other words, what is the value of the PATH environment variable? Please edit your question to include this information. – Kenster May 08 '15 at 14:30
  • I'd say the error message suggests `spawn` failed to spawn `ssh` because the OS failed to locate a program named "ssh" in the directories listed in the current user's `PATH` environment variable -- just what @Kenster said. Hence, the first thing to try out is to just run `ssh` at the same prompt you run your `tclsh` binary from -- it it does not work, the problem has nothing to do with Tcl but with basic knowledge of your OS. – kostix May 08 '15 at 15:43
  • I am new to this. How to find/set the value of PATH environment variable? I am trying to run from WINDOWS OS system. – Praveen May 11 '15 at 12:24

1 Answers1

0

First, make sure that you have ssh installed. From the bash prompt (Mac, Linux, Cygwin) or cmd prompt (Windows), type:

ssh

If you see an error, try to fix it. The most likely cause is ssh not installed, or not in the path.

Next, in your script, you did not use the $user variable, instead you use the hard-coded root. Fix that:

spawn ssh $user@$DeviceIp

The final problem: you already specified the user name from the command line, the ssh program will not ask for user again, so you must delete these two lines:

expect "login as:"    
send "$user\r"    

After that, hopefully everything will go as planned.

Hai Vu
  • 37,849
  • 11
  • 66
  • 93