ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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).
#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
for f in `ls -ltra /projects/ilinterf/john/expect/backupdir/testtest/*.txt`; do scp $f username@server.com:/projects/ilinterf/john/expect/backupdir; done
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.