LinuxQuestions.org
Help answer threads with 0 replies.
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 07-18-2008, 06:18 AM   #1
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Rep: Reputation: 17
Any expect script experts


I have a problem

Quote:
#!/projects/john/bin/expect
set var "f in `ls -tr /projects/johndir/john/expect/backupdir/testtest/*.txt`"

send "ssh -p 22 john@server.com for $var;do mv $f /projects/johndir/john/expect/backupdir;done\n"

expect "h>"

I cant able to do this, it gives an error

Quote:
can't read "f": no such variable
while executing
"send "ssh -p 22 john@server.com for $var;do mv $f..."
(file "./mytesting.exp" line 19)

but when i execute the

Quote:
for $var;do mv $f /projects/johndir/john/expect/backupdir;done
it works, ...whats the problem in expect script..please help
 
Old 07-18-2008, 06:51 AM   #2
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 38
Instead of using the set command, why don't you use a foreach?
Code:
foreach file [ls] {
#do something
}

Last edited by indeliblestamp; 07-18-2008 at 06:52 AM. Reason: bad formatting
 
Old 07-18-2008, 06:51 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
If you want to execute a non-interactive command on a remote server with ssh, you simply don't need to use expect. Set up public key authentication (so you don't need a password to log in), and then use a shell script, like this:

Code:
#!/bin/bash

ssh john@server.com 'mv /projects/johndir/john/expect/backupdir/testtest/*.txt /john/expect/backupdir/'
You only need to use expect if the command you wish to execute prompts you for input with the keyboard. For security reasons, you should not use expect to respond to password prompts (because your expect script would have to contain a copy of your unencrypted password).
 
Old 07-20-2008, 09:26 PM   #4
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
if normally in an expect script, u do this means in works

Quote:
spawn /bin/ksh

send "ssh -p 22 username@server.com cd /any/directory;ls -la file.txt\n"
expect "h>"

#note..already set up the server in such a way so the password is disabled.
the code aboe works, spawn a shell and then send commands to the waiting shell..

but y this doesnt work

Quote:

send "ssh -p 22 username@server.com for f in `ls -tr /projects/ilinterf/john/expect/backupdir/testtest/*.txt`;do mv $f /projects/johndir/john/expect/backupdir;done\n"

expect "h>"
it shows the error

Quote:

can't read "f": no such variable
while executing
"send "ssh -p 22 username@server.com for f in `ls -tr /projects/ilinterf/john/expect/backupdir/testtest/*.txt`;do mv $f..."
(file "./mytesting.exp" line 19)
because if i execute the script below on a shell means, it works. Y its not working on the expect script, which suppose to execute the shell script commands....

Quote:
for f in `ls -tr /projects/ilinterf/john/expect/backupdir/testtest/*.txt`;do mv $f /projects/johndir/john/expect/backupdir;done
pls help
 
Old 07-21-2008, 02:27 AM   #5
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
is this correct

Quote:

for f in `ls -ltra /projects/ilinterf/john/expect/backupdir/testtest/*.txt`; do scp $f username@server.com:/projects/ilinterf/john/expect/backupdir; done

 
Old 07-21-2008, 05:21 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
You expect script doesn't work because you have not spawned a process to expect output from or send input to.

In the vase of this:
Code:
for f in `ls -ltra /projects/ilinterf/john/expect/backupdir/testtest/*.txt`; do 
  scp $f username@server.com:/projects/ilinterf/john/expect/backupdir
done
A few comments:
  • You would be better off not calling ls with backtick expansion. The shell is already doing the expansion of the file pattern /projects/ilinterf/john/expect/backupdir/testtest/*.txt into a list of files - calling ls on that list has no use, and can cause a problem when there are spaces in one or more of the file names.
  • You should get into the habit of quoting your expanded variables in the shell. The case where there is a space in a file name will cause the scp line to malfunction the way you did it.

These things considered, a better version would be:
Code:
for f in /projects/ilinterf/john/expect/backupdir/testtest/*.txt; do 
  scp "$f" username@server.com:/projects/ilinterf/john/expect/backupdir/
done
Even better, scp can take a list of files as arguments, so there's no need for the loop at all.
Code:
scp /projects/ilinterf/john/expect/backupdir/testtest/*.txt username@server.com:/projects/ilinterf/john/expect/backupdir/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Expect Script coolest Programming 7 02-16-2009 03:59 PM
expect script output saltydog4791 Programming 1 05-27-2008 08:01 AM
Troubleshooting script that uses expect kaplan71 Linux - Software 4 02-29-2008 05:23 PM
SMTP/MX Check Expect Script rk4k Programming 2 02-06-2007 09:36 PM
Script... variables with expect? Manana Linux - Networking 1 01-19-2007 04:41 AM


All times are GMT -5. The time now is 08:21 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration