LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2008, 01:16 PM   #1
gustavolinux
Member
 
Registered: Aug 2008
Posts: 36

Rep: Reputation: 15
Tcl escape characteres


Folks, I'm having a problem with a tcl escape character: the backslash.

I'm trying to create a script that calls another script. One of the parameters that is sent to the other script is a password (to use with ssh). Well, if the password is a usual string, like 'carrie', it works fine. The problem occurs when the first letter of the password is a dollar sign, like $testpass. When this password is used, tcl seems to write the string inside braces, like {$testpass}. This new value created by tcl is sent to the second script, and then my system fails, because the correct password is $testpass, and not {$testpass}.

Please, somebody that knows tcl can help me?

A simple teste code shows my problem:

---------
#!/bin/sh

package require Expect

set timeout 15

set ssh_password \$testpass

spawn ./ssh.sem.passw.sh 5622 root $ssh_password 172.21.1.2 md5sum /root/ipt-firewall
interact

--------

Debugging, I found that the parameters given to spawn by tcl engine was:

./ssh.sem.passw.sh 5622 root {$testpass} 172.21.1.2 md5sum /root/ipt-firewall

Where the incorrect password appears... It should be $testpass.

Thanks by the attention dudes...
 
Old 10-20-2008, 09:44 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
I am not able to reproduce your error.

I have to change the code a bit in order to get a valid command for spawn to execute.

Code:
#!/usr/bin/expect

package require Expect

set timeout 15
exp_internal 1

set ssh_password \$testpass

spawn telnet 5622 root $ssh_password 172.21.1.2 md5sum /root/ipt-firewall
interact
Take good notice of the first line in the code. It cannot be /bin/sh like in the code you posted. Are you sure you posted the code verbatim?

This is the output I got:

Code:
donald_pc:~$ ./psswd.exp
spawn telnet 5622 root $testpass 172.21.1.2 md5sum /root/ipt-firewall
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {4289}
tty_raw_noecho: was raw = 0  echo = 1
However, I am not sure there is not shell expansion being used for $testpass. Have you tried to put a literal '\$testpass' in the spawn command line. I assume you get this by:
Code:
set ssh_passwd \\\$testpass
jlinkels
 
Old 10-21-2008, 06:04 AM   #3
gustavolinux
Member
 
Registered: Aug 2008
Posts: 36

Original Poster
Rep: Reputation: 15
thanks by the answer jlinkels..

I'll make the test more simples, than I can explain it perfectly...

There is two files: teste.sh and ssh.teste.sh

teste.sh code is:

#-------------------------------
#!/bin/sh

package require Expect

set timeout 15

set ssh_password \$teste

spawn ./ssh.teste.sh $ssh_password
interact
#-------------------------------

and ssh.teste.sh is:

#-------------------------------
#!/usr/bin/expect -f
# set Variables

set timeout -1

log_user 0

send_user "\nTeste:\n"
send_user $argv
send_user "\n"
#-------------------------------


I save both in the same folder and type the following command:

tclsh ./teste.sh

The output that I get is:
---------------------------------------
spawn ./ssh.teste.sh $teste

Teste:
{$teste}
---------------------------------------

And it should be

Teste:
$teste

Hope you can reproduce now... Thanks by the attention...

I don understand pretty much the difference between #!/usr/bin/expect and #!/bin/sh, but I'll play a little bit with this issue and give a feedback to the forum if it works...

thanx again
 
Old 10-21-2008, 06:19 AM   #4
gustavolinux
Member
 
Registered: Aug 2008
Posts: 36

Original Poster
Rep: Reputation: 15
Unhappy

Feedbacks:

using \\\$testpass

does not work...

output is:

-----------------------------------
Teste:
{\$ssh_password}
-----------------------------------

double quotes at the set directive does not work also.. makes no difference...

set ssh_password "\$teste"

and making both script as #!/usr/bin/expect does not work also... but if I put both as #!/bin/sh, then a total error happens.. so I'm using both as expect script now... thanks by the tip...


[ ] 's
 
Old 10-21-2008, 06:54 PM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
ok, now I see, sorry I did not realize this sooner.
Put this:
Code:
send_user [lindex $argv 0]
instead of:
send_user $argv
and you are fine. The problem is that $argv is a list. Although is have only one member it is still a list and therefore printed between {}

BTW, it is better programming style to name your files as .tcl or .exp when they contain tcl or expect code, and sh when it is a shell script. Also, be careful to put !#/usr/bin/tcl or !#/bin/sh as the first line matching whether your script is tcl or sh. And it MUST be on the first line. This:
Code:
#-------------------------------
#!/usr/bin/expect -f
does not work when spawning. It can only work when you call the expect program with this file as input.

Do you have special reasons to put the login part in a different file? It can be in one file, this way complicates matters a bit. (Like the passing of variables )

jlinkels
 
Old 10-22-2008, 06:08 AM   #6
gustavolinux
Member
 
Registered: Aug 2008
Posts: 36

Original Poster
Rep: Reputation: 15
it worked! thanks pretty much dude, great help..

Thanks by the tips about shell, I'm a newbie on the subject, as you can see.. I'll do that..


[]'s
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot find Tcl dll ( Perl's Tcl bridge ) Xyem Linux - Software 2 08-08-2006 09:45 AM
how do i escape a / ??? Fascistchicken Linux - General 10 09-12-2004 11:28 AM
Escape character ? juanb Linux - Newbie 2 08-31-2004 10:03 AM
Escape! Wind0wR3fuge Linux - Newbie 6 07-11-2004 03:00 AM
escape sequence help in C name_in_use450 Linux - General 6 07-01-2004 09:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration