'invalid command' error in expect script that is fine in interactive mode - linux

I have a problem in running an expect script (very new in this).
Here is what I have now in an expect script autorun.exp:
#!/usr/bin/expect -f
cd /auto/rmscr/shared_resources/qiuyuguo/softwares/QuEST_2.4
./generate_QuEST_parameters.pl -sam_align_ChIP A.sam -sam_align_RX_noIP \
B.sam -rp mm10_bychr -ap ./output_directory -ChIP_name name
After running this script, what I got is this:
[qiuyuguo#hpc-uec 20150605]$ ./auto_run.exp
invalid command name "./generate_QuEST_parameters.pl"
while executing
"./generate_QuEST_parameters.pl -sam_align_ChIP A.sam -s..."
(file "./auto_run.exp" line 3)
Interestingly, the content in the script runs fine in the interactive mode of expect. Could I get a hint about what is going on?

expect script should have a spawn command, a expect command and a send command.
This is a example:
#!/usr/bin/env expect
spawn su -l test
expect "Password: " {
send "xxxxx\r"
}
expect "# " {
send "exit\r"
}
expect eof {
exit
}

Related

sftp via bash script in a cronjob: 'get' and 'rm' commands not found?

I'm trying to do something real, real simple: download a file from a sftp site at a set time. However, I'm getting errors like 'command: rm does not exist' or 'command get does not exist' which is garbage, because these commands obviously exist.
#!/usr/bin/expect
#remove current version of file
rm -f /home/user/downloads/data/newdata.zip
spawn sftp user#ftpsite.com
expect "password:"
send "PaSsWoRd\n"
expect "sftp>"
get /mycompany/myproject/data/newdata.zip /home/user/downloads/data
expect "sftp>"
send "exit\n"
interact
(base) root#ubuntu:~# ./shellscript.sh
invalid command name "rm"
while executing
"rm -f /home/user/downloads/data/newdata.zip"
or without the removal
(base) root#ubuntu:~# sudo ./shellscript.sh
spawn sftp user#sftpsite.com
EFT Server Login - %DATE% %TIME% - Please enter valid credentials to continueEnter password:
Connected to ftpsite.com
sftp> invalid command name "get"
while executing
"get /mycompany/myproject/data/newdata.zip /home/user/downloads/data"
What gives? Why can't I get real error messages - I'm sure get and rm a perfectly fine commands and I can verify this by running them directly from the command line, where they work fine.
You are writing TCL code in an Expect script. You are not writing Bash in a shell script.
You have to send the commands you want to write to the sftp tool:
send "get /mycompany/myproject/data/newdata.zip /home/user/downloads/data\n"
And exec any commands you want to run non-interactively:
exec rm -f /home/user/downloads/data/newdata.zip

Two commands or command pipe command - Spawn Expect

I'm trying to execute a expect script into bash script, but when I try to execute the shell without "| tee -a xxx.log" (where xxx.log is a file than I want to write to record the ssh session).
This code works fine:
comlog="ssh $USR#192.168.228.20"
expect -c"
spawn \"$comlog\"
expect \"Password:\"
send \"$PASS\r\"
interact
"
But when I try to add the "tee -a" command to save the ssh session the issue is
invalid command name "ssh"
while executing
This is the complete command where I obtain the error message
comlog="ssh $USR#192.168.228.20 | tee -a /home/xxx.log"
expect -c"
spawn \"$comlog\"
expect \"Password:\"
send \"$PASS\r\" #Already Obteined
interact
"
I tried to change the "comlog" var as this ways but doesn't work :(
cssh $USR#192.168.228.20 \| tee -a /home/xxx.log
Does anyone know another way to save the ssh session started from expect? Or how can I send those two commands in same spawn command.
You can capture the expect session output this way:
comlog="ssh $USR#192.168.228.20"
expect -c"
spawn \"$comlog\"
expect \"Password:\"
send \"$PASS\r\" #Already Obteined
interact
" >/home/xxx.log
If you want to put shell metacharacters like the pipe, you'll have to spawn a shell to handle them. Also using a here-doc can help a lot with quoting
comlog="ssh $USR#192.168.228.20 | tee -a /home/xxx.log"
expect <<"END_EXPECT"
spawn sh -c "$comlog"
... rest of expect script
END_EXPECT

How do I ssh into a machine, wait for a prompt, and then send a bunch of commands to it?

How do I do the following?
SSH to a machine, probably using expect since this is a script and I can't type the password for it
Wait for a prompt from the machine. The prompt is ->
Then send a bunch of commands from the original host to the machine I ssh'd to. Note that the output from these commands can contain the characters ->
Exit
Here are the contents of some-commands.txt:
first command
second command
third command
Here are the contents of the expect script:
#!/usr/bin/expect
set f [open "some-commands.txt"]
set cmds [split [read $f] "\n"]
close $f
eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no root#machine
interact -o -nobuffer -re "Password:" return
send "password\r"
# look for the prompt
set prompt "\n-> "
foreach cmd $cmds {
send "$cmd\r";
# The following works, except for the commands
# whose output include ->
interact -o -nobuffer -re "-> " return
}
The problem is that the interact command captures the -> from the command output instead of the prompt, which hasn't yet arrived at that point.
I'm used to accomplish the same thing by doing something like:
ssh -t -t -C user#host 'bash -s' < my_shell_script.sh param1 paramX
Where my_shell_script.sh is an simple shell script.
The trick here is use multiple -t to force pseudo-terminal over ssh and the -s option to bash witch makes it reads the commands from the standard input.

Expect script error send: spawn id exp4 not open while executing "send "password""

I'm trying to run an expect script but I have an error:
send: spawn id exp4 not open
while executing
"send "password"
My script is very simple:
#!/usr/bin/expect -f
#!/usr/bin/env expect
#!/usr/bin/env export
#!/bin/sh
#!/bin/bash
spawn sftp -o IdentityFile=/home/localUser/MyPrivKeys_open.ppk user#123.123.123.123
expect "sftp.gatewayEnter passphrase for key '/home2/localUser/MyPrivKeys_open.ppk':"
send "passphrase\r"
expect "sftp>"
send "cd /home/localUser/localPath \r"
expect "sftp>"
send "mget myfile.xml /home/localUser/localPath \r"
set timeout 5
interact
What I do is editing my expect script in notepad.exe but when i upload my script i have to run next commands:
dos2unix bash.sh -> converts dos file to a Unix file
chmod +x bash.sh -> gives execution permission
And finally i run my script:
./bash.sh
But after that it sends that error.
It is something really weird because i have another .sh file with the same structure and i am only changing paths where i get files
I think i need some help about it.
Thanks.
I have already fix it, it was an error in dos2unix command. I had to code it again over Ubuntu for avoiding to run dos2unix command.

send error while running expect code

I made an expect code:
#!/usr/bin/expect -f
spawn ls
expect "*]$*"
send "cd /to/some/path\n"
expect "*]$*"
send "sudo -u root ./program.sh"
expect "*:*"
send "i_am_password\n"
interact
while executing it I am getting the below error:
spawn ls
my_pgm.exp abc.sh axyz.zip all.zip test.exp
send: spawn id exp6 not open
while executing
"send "cd /to/some/path\n""
(file "./my_pgm.exp" line 5)
I am running this code on ubuntu. Please help.
I'm not sure to really understand why you need expect, but for a bash script, try this:
#!/usr/bin/expect -f
spawn bash -i
expect "*$ "
send "cd /to/some/path\n"
expect "*$ "
send "sudo -u root ./program.sh\n"
expect "*: "
send "i_am_password\n"
interact
Description
The spawn directive instruct expect which program are to be used to interact with. So if you want to interact with bash, you have to ask expectto spawn bash. The -i parameter to bash enforce them to start in interactive mode.
At all, this look like a very bad idea, but that's only my opinion ;)

Resources