Special Character issue in Expect Utility (Tcl)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Special Character issue in Expect Utility (Tcl)
# 1  
Old 09-25-2014
Special Character issue in Expect Utility (Tcl)

Hi,

I have written a unix expect utility "ssh-login.exp" which connects (ssh) to remote host and execute some shell script. I am calling this "ssh-login.exp" utility from another shell script.

"ssh-login.exp" takes username, password, host and shell script path to execute on remote host. All works fine for alphanumeric passwords. If password contains special characters e.g. "$", "ssh-login.exp" doesn't work. I have already added logic to add escape character right before any special characters in password string.

e.g. for password string "welcome$123" and I send "welcome\$123".

The issue is when expect utility "ssh-login.exp" sends password to remote host, it prepends '{' and appends '}' to password string if password contains any special characters. if password doesn't have any special characters then it sends password without '{' and '}' and works fine.

e.g. for password "welcome$123" expect program sends "{welcome\$123}", in fact it should send "welcome\$123" (without '{' and '}' ). If password is "{welcome\$123}" remote machine don't allow the entry because of wrong password as it is '{' and '}'.

So I am trying to find out a way to get rid of '{' and '}' from password string before sending it to remote host if password contains any special characters.

Here is my expect script : "ssh-login.exp":

Code:
#!/usr/bin/expect -f

set username [lrange $argv 0 0]
set password [lrange $argv 1 1]
set host [lrange $argv 2 2]
set script [lrange $argv 3 3]

set prompt "*$*"

set scriptIO  [open ./$script r]
set scriptContents [read -nonewline $scriptIO]
close $scriptIO

spawn ssh -q -o StrictHostKeyChecking=no $username@$host

#match_max 100000

while (1) {
        expect {
                "no)? " {
                        send -- "yes\r"
                        send -- "\r"
                }
                "*?assword:*" {
                        send -- "$password\r"
                        send -- "\r"
                }
                "$prompt" {
                        send -- "pwd\r"
                        send -- "\r"
                        break;
                }
        }
}

send -- "\r"

send -- "$scriptContents\r"

send -- "exit\r"

expect eof

Please let me know if someone has already faced this issue and were able to resolve it.

Thanks,

Last edited by Mahesh Desai; 09-25-2014 at 02:26 PM..
# 2  
Old 09-25-2014
Try:

Code:
send -- "[join $password]\r"

or
Code:
set password [join [lrange $argv 1 1]]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Issue in installing expect and Tcl

Hi I need to install expect in redhat. through net I came to know that I must install tcl too in order to make expect work. I have downloaded both packages but not able to install # ls -lrt total 3720 18:33 tcl8.4.20-src.tar.gz 18:33 expect5.45.3.tar.gz 18:40 expect5.45.3... (7 Replies)
Discussion started by: scriptor
7 Replies

2. Shell Programming and Scripting

A special character issue.

Hi Gurus, I have a file contains special character. when using: grep 'xxxxx' filename, it returns whole line. when using: grep -evt 'xxxxx' filename it returns whole line with special charactor. when running command: nl filename |grep 'xxxxxx' the line break by the special... (1 Reply)
Discussion started by: ken6503
1 Replies

3. Shell Programming and Scripting

expect TCL script

Hello, I write a TCL script for Expect/ Telnet. I want to send command to the telnet server. But I want to close after the command is sent. Anybody know which command can flush the expect so I can sure the command is sent to the telnet server??? EX: send "./command1\r" close... (0 Replies)
Discussion started by: linboco
0 Replies

4. UNIX for Dummies Questions & Answers

Expect/Tcl help

hi, I am new in Expect. I have a question about expect timeout. suppose I have a structure of expect { ".."{ send"............"} timeout{ ............... } } The silly question is if I reach timeout, how can I store the error message showing on the screen to... (2 Replies)
Discussion started by: allenxiao7
2 Replies

5. Shell Programming and Scripting

tcl/expect

Can someone identify what is the problem here?. no children while executing "exp_wait -nowait -i -1" (procedure "logOptions" line 45) invoked from within "logOptions" (procedure "doExecute" line 98) invoked from within "doExecute" (procedure "main" line 32) ... (7 Replies)
Discussion started by: calsum
7 Replies

6. Shell Programming and Scripting

Tcl expect HELP

In the following "for" loop I assume the the script will expect "anyway", "first" NOT in any paticular order and send "yes" when there found, breaking out of the loop when "$prompt" is found. The way it is working is like 3 individual expect lines, and they MUST be in cronological order. ANY help... (0 Replies)
Discussion started by: dave_m
0 Replies

7. Shell Programming and Scripting

tcl: regexp matching special character

Hello Experts, Can someone help me here: I have a variable which contains a string with "". set var1 {a} set str1 {a is the element i want to match} Now "regexp $var1 $str1" does not work? ("regexp {a\} $str1" works, but var1 gets it's value automatically from another script) Is... (6 Replies)
Discussion started by: sumitgarg
6 Replies

8. UNIX for Dummies Questions & Answers

Expect/Tcl help?

Does anyone know of an expect/tcl forum that is as helpful as this one is for shell scripting? Or if anyone has any expect knowledge, can you please provide some guidance on how to write to a local error log based on output from a ssh session? I have something like this: foreach host... (2 Replies)
Discussion started by: earnstaf
2 Replies

9. UNIX for Advanced & Expert Users

Issue with dispaly of special character like 'TM'

Hi, I've an xml file with special characetr 'TM' (trade mark sign). The issue is that I am not able to get the symbol 'TM'(trade mark sign) on unix. It displays '?' instead. I've searched the solution on net. Wht I've found is that the symbol 'TM' does not come under ISO-8859-1 char set. So I... (0 Replies)
Discussion started by: vbehal
0 Replies

10. Shell Programming and Scripting

Expect with tcl/tk

hai all, i have an tcl script in which i have been reading the DUT Command prompt of an cisco switch as DUT Command Prompt : cisco* and running the test case of stp now the problem is if i have given any blank space in between the cisco or at the startup then the Expect is not identifying the... (0 Replies)
Discussion started by: sanjustudy
0 Replies
Login or Register to Ask a Question