How to list a directory with expect via sftp connection - linux

I have this expect script but the list of files does not present me correctly.
expect.dat
set timeout 10000
spawn sftp -o ConnectTimeout=3 user#192.1.6.6
expect "*?assword*"
send "tech\r"
expect "sftp>"
send "cd /global/scripts/log\r"
expect "sftp>"
send "ls 20220703*\r"
expect "sftp>"
send "bye\r"
Expect command
expect expect.dat
Result
spawn sftp -o ConnectTimeout=3 user#192.1.6.6
Password:
Connected to user#192.1.6.6.
sftp> cd /global/scripts/log
sftp> ls 20220703*
20220703_A.log 20220703_A.xls
20220703_E.log 20220703_E_r.log
sftp> You have new mail in /var/spool/mail/dm
how to get this result?
20220703_A.log
20220703_A.xls
20220703_E.log
20220703_E_r.log

First, use the sftp command ls -1 20220703* to produce output in a single column.
Next, use log_user 0 and spawn -noecho sftp ... to hide all the excess output.
Last, and the trickiest part, capture the ls output so that can be printed out cleanly: we have to handle the fact that the command we just "typed" will be given back to us.
set ls_cmd "ls -1 20220703*"
send "$ls_cmd\r"
# capture the command's output
expect -re "$ls_cmd\r\n(.*)\r\nsftp>"
puts $expect_out(1,string)
send "bye\r"
expect eof # wait for sftp to exit cleanly
expect gives you the process's output using \r\n line endings.

Related

EOF in expect script spawning sftp

I have a script that is transferring files from a linux server to a windows server. I want to log the data related to the transfers but EOF is giving me error in the HEREDOC construct. Can anyone show me the way forward for this.
My script is:
#!/usr/bin/expect
spawn sftp XXXX#XXXXXX <<EOF>> log.file
expect "password:"
send "ABC\n"
expect "sftp>"
send "cd /FIRST\r"
expect "sftp>"
send "lcd /home\r"
expect "sftp>"
send "mput /home/*First*\r"
send "bye\r"
interact
Or
#!/usr/bin/expect
log_file -a log.file
spawn sftp XXXX#XXXXXX
# ... the rest is all the same.
If you're not actually interacting (as a human) with the sftp process, you could use this as the last line
expect eof
Use a shell script instead and call expect passing to it "-" to make it read from its standard input which will be the HEREDOC (i.e. <<EOF ... EOF):
#!/bin/sh
/usr/bin/expect - <<EOF >> /tmp/log
spawn sftp XXXX#XXXXXX
expect "password:"
send "ABC\n"
expect "sftp>"
send "cd /FIRST\r"
expect "sftp>"
send "lcd /home\r"
expect "sftp>"
send "mput /home/*First*\r"
send "bye\r"
EOF

Unable to mput all the files through sftp in the remote server using expect

I'm trying to mput all files present in the directory : /Test/XML/ into a remote sftp server with the help of expect utility.
I've around 320 files in the directory: /Test/XML/.
The size of each file is around 0.1 MB.
There's no error observed.
Here's my code:
cd /Test/XML/
/usr/bin/expect <<EOF
spawn /usr/bin/sftp ${user_name}#${HOSTNAME}
expect "password:"
send "${passwd}\r"
expect "sftp>"
send "cd /test\r"
expect "sftp>"
send "mkdir XML\r"
expect "sftp>"
send "cd /test/XML\r"
expect "sftp>"
send "mput *\r"
expect "sftp>"
send "bye\r"
EOF
But the problem here is, mput * is transferring only 4 files instead of transferring all the 320 files.
Not sure, why it's not able to transfer all the 320 files in the remote server.
Any help is most welcome.
Thanks #ThiruShetty for the hint of using set timeout -1 in the expect utility.
Actually, i had a lot of files(~320-350) to be transferred(sftp) to a remote server.
With the normal execution of sftp using expect utility, it was able to transfer only a few files, not all of them which i wanted.
After inserting set timeout -1 inside expect, it solved the problem of timeout.
Here's the final code:
cd /Test/XML/
/usr/bin/expect <<EOF
set timeout -1
spawn /usr/bin/sftp ${user_name}#${HOSTNAME}
expect "password:"
send "${passwd}\r"
expect "sftp>"
send "cd /test\r"
expect "sftp>"
send "mkdir XML\r"
expect "sftp>"
send "cd /test/XML\r"
expect "sftp>"
send "mput *\r"
expect "sftp>"
send "bye\r"
EOF

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).

how to acquire the return value of a command in ssh accessed by expect?

After i use expect access ssh, and run particular command in it. How could i get the actually return value of that command and set it to the return value of expect or an env variable(better)?
I am not so familiar with expect, so a few lines to show how the capture thing works would help a lot. Many thanks.
You do it the same way you would sitting at a terminal: echo $? (I assume your remote shell is sh/ksh/bash/...)
#!/usr/bin/expect -f
set host remote_host
set user remote_username
set prompt {\$ $}
set cmd {grep not-found /etc/passwd}
log_user 0
spawn ssh -l $user $host
expect -re $prompt
send "$cmd\r"
expect -re $prompt
send -- "echo \$?\r"
expect -re "\r\n(\\d+)\r\n.*$prompt"
set rc $expect_out(1,string)
send -- "exit\r"
expect eof
puts "return code from '$cmd' on $host = $rc"

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

Resources