Two commands or command pipe command - Spawn Expect - linux

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

Related

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.

Why spawn scp does not work correctly?

Below is my script containing spawn scp related script.
spawn scp -r /usr/local/clo/Jenkins.zip root#xu.domain.com:/usr/local/clo
expect "password:"
send "111111\r"
expect "*\r"
expect "\r"
But when I execute it, it output the following log and it can not transfer the file correctly.
**
spawn scp -r /usr/local/clo/Jenkins.zip root#xu.domain.com:/usr/local/clo
root#xu.domain.com's password:
Jenkins.zip 0% 0 0.0KB/s --:-- ETA
**
I was wondering how does the '0%' happen? If I manually execute the scp script from console, it can send the file completely.
I suspect this happens because the output of the spawned process includes \r (carriage return) characters when displaying the progress meter, try to add expect eof at the end of your Expect script.
But to deal with timeout problems, it should be better to detect the progress meter, using exp_continue to continue executing while the progress meter is displayed:
spawn scp -r /usr/local/clo/Jenkins.zip root#xu.domain.com:/usr/local/clo
expect "password:"
send "111111\r"
expect {
"ETA" {exp_continue}
"100%" {}
}
expect eof
You could also add exp_internal 1 at the beginning of the script (it reports internal activity of the expect command).

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.

expect auto login sucess, but can't exec any command

the shell code:
~ cat test_longin.sh
#!/usr/bin/expect
set timeout 120
spawn /usr/bin/ssh fuyou#target.server
expect "*assword*"
send "fuyou\n"
interact
When I type ./test_login.sh, it can login target servers successfully, but when I execute ls command, the console doesn't output any text. So I can't exec any commond on target server. Is the interaction problematic?
I think you should expect something after send, but not use command "interact" immediately.
xxx:/home/username/temp # cat t1.exp
#!/usr/bin/expect
spawn ssh serverx
expect "#"
send "ls -l\r"
expect "#"
interact

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