SFTP scripting unable to change directories question - sftp

I'm relatively new to using sftp in scripting format (bash shell on Mac OSX High Sierra). I am having issues changing dirs once logged into the remote server. I want to cd to 'FTP PDF (Download) to CR'
Here is my script(edited):
#!/bin/bash
expect -c "
spawn sftp ClaimReturn#8.19.132.155
expect \"Password\"
send \"xxxxxxx\r\"
expect \"sftp>\"
send \"cd CR\ Reports\r\"
#DIR TO CD to "CR REPORTS"
expect \"sftp>\"
send \"bye\r\"
expect \"#\"
"

This is really just an formatted comment expanding on #meuh's comment.
You're having quoting trouble. You could use single quotes or a quoted heredoc to make your life easier
#!/bin/bash
expect <<'END_EXPECT'
spawn sftp ClaimReturn#8.19.132.155
expect "Password"
send "xxxxxxx\r"
expect "sftp>"
send "cd 'CR Reports'\r"
#DIR TO CD to "CR REPORTS"
expect "sftp>"
send "bye\r"
expect "#"
END_EXPECT
Or, just an expect script:
#!/usr/bin/expect -f
spawn sftp ClaimReturn#8.19.132.155
expect "Password"
send "xxxxxxx\r"
expect "sftp>"
send "cd 'CR Reports'\r"
#DIR TO CD to "CR REPORTS"
expect "sftp>"
send "bye\r"
expect "#"

Related

SFTP file to get and remove files from remote server

I'm trying to write an expect script to pull files from a remote server onto a local folder, and delete them from the remote as they're pulled in. The script I have that doesn't remove them is:
#!/usr/bin/expect
spawn sftp -oHostKeyAlgorithms=+ssh-dss sftp://<username>#<ftp server>
expect "<username>#<ftp server>'s password:"
send "<password>\n"
expect "sftp>"
send "cd <folder>\n"
expect "sftp>"
send "get *"
expect "sftp>"
send "exit\n"
interact
I could add "rm *" after the get command, but sometimes it loses connection to the server while getting the files and the script stops, so I'd like to remove files as I get them.
I tried setting up a for loop like this:
#!/usr/bin/expect
spawn sftp -oHostKeyAlgorithms=+ssh-dss sftp://<username>#<ftp server>
expect "<username>#<ftp server>'s password:"
send "<password>\n"
expect "sftp>"
send "cd <folder>\n"
expect "sftp>"
set filelist [expr { send "ls -1\n" } ]
foreach file $filelist {
send "get $file\n"
expect "sftp>"
send "rm $file\n"
expect "sftp>"
}
send "exit\n"
interact
But I get:
invalid bareword "send" in expression " send "ls -1\n" "; should be "$send" or "{send}" or "send(...)" or ...
Can anyone tell me what I'm doing wrong in that script, or if there's another way to achieve what I want?
The error message you get comes from the line
set filelist [expr { send "ls -1\n" } ]
This is because the expr command evaluates arithmetic and logical expressions (as documented at https://www.tcl-lang.org/man/tcl8.6/TclCmd/expr.htm) but send "ls -1\n" is not an expression it understands.
If you were trying to read a list of files from your local machine you could do this with
set filelist [exec ls -1]
However what you really want here is to read a list of files through the ssh connection to the remote machine. This is a little more complicated, you need to use expect to loop over the lines you get back until you see the prompt again, something like this (untested):
send "ls -1\r"
expect {
"sftp>" {}
-re "(.*)\n" {
lappend filelist $expect_out(1,string)
exp_continue
}
}
For more info see https://www.tcl-lang.org/man/expect5.31/expect.1.html and https://www.tcl-lang.org/man/tcl8.6/TclCmd/contents.htm .

SFTP BSD Zsh Batchfile with Expect Syntax

I have a zsh script in MacOS—trying to automate the -b (batchfile option) but the "~" doesn't seem to be expanding within the expect syntax. Expect can't find the batchfile location in that directory even though it's there. Do I need the full path or do globals like $HOME not work within expect?
deletedfiles=$(/usr/bin/expect <<SFTP
set timeout -1
spawn /usr/bin/sftp -b ~/Desktop/pgmfilesremoved.txt Port=${port} ${sftpuser}#${host}
expect "Password authentication"
expect "Password:"
send "${password}\r"
expect "sftp>"
send "exit\r"
SFTP
)
Because the heredoc is unquoted, the shell is expanding the variables before launching expect. Use $HOME: tilde has no special significance in Tcl/expect.

how to transfer in sftp with password ? put command not sending the full file

I am new to UNIX. Need help in finding the correct approach to send a file.
I have to send a big file 1gb and the time it takes to sftp manually is approx 10 mins, We have tried the below script because we have to login with password.
The problem here is before completely transferring the file. The script come out of the SFTP connection with no error.
Script:
` expect -c " `
spawn sftp ${remote_user}#${remote_host}
expect \"password\"
send ${remote_pswd}\r
expect sftp>
send \" cd ${remote_path}\r \"
expect sftp>
send \" lcd ${source_path}\r \"
expect sftp>
send \" put ${source_file} \r \"
expect sftp>
send \" echo $? \r \"
expect sftp>
send \"bye\" " ' `
Log:
` spawn sftp DataStageIM2#192.168.79.15
DataStageIM2#192.168.79.15's password:
Connected to 192.168.79.15.
sftp> cd /users/StoreStockManagement/ReferenceData/Inbound
sftp> lcd /staging/oretail/external/data/DSPRD/Output/Pricing/INT340
sftp> mput hhtstore_price.dat
Uploading hhtstore_price.dat to /users/StoreStockManagement/ReferenceData/Inbound/hhtstore_price.dat
hhtstore_price.dat 3% 189MB 18.1MB/s 04:31 ETA+ [[ 0 -ne 0 ]]`
--Here after transferring 3% of the file this script comes out and I cannot see the file there. But when i manually trying the sftp it is working. Only with script it is not copying.
Can some one help here
The default timeout value for the expect command is 10 seconds. So, after put, expect will wait for 10 seconds to see the prompt, then timeout, then continue on with the script.
Clearly you want to wait for however log as necessary to transfer the file, so add this to your script:
set timeout -1

scripting sftp with expect

I have to script an sftp-access with password-authentification. Unfortunately our customer can't create a key-authentification on the server we have to access.
All the sftp-Commands are generated and written in a temporary file (tmp.txt). Following command works fine on the command-line:
sftp OnlineRegTest_BS#ftp.b-n-c.ch < tmp.txt
The problems starts when I'd like to use "expect" because I have to script it and use a password.
EXPECTSCRIPT=`cat <<EOF
spawn sftp user#server < tmp.txt
expect "password"
send ${pass}
send "\r"
EOF`
expect -c "$EXPECTSCRIPT"
If I remove the "< tmp.txt" the connection will establish. But I need the commands from the tmp.txt. Here's the error-Message:
[root#xxx web]# ./transfer.sh
spawn sftp user#server < tmp.txt
usage: sftp [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]
[-o ssh_option] [-P sftp_server_path] [-R num_requests]
[-S program] [-s subsystem | sftp_server] host
sftp [[user#]host[:file [file]]]
sftp [[user#]host[:dir[/]]]
sftp -b batchfile [user#]host
send: spawn id exp6 not open
while executing
"send password"
Does someone have any ideas?
I also couldn't get it running with sftp -b... :-(
edit for #glenn jackman
EXPECTSCRIPT2=`cat <<EOF
spawn sh -c {sftp user#server < tmp.txt}
expect "password"
send ${pass}
send "\r"
expect "sftp>"
expect "sftp>"
.....
expect "sftp>"
expect "sftp>"
expect "sftp>"
EOF`
the tmp.txt looks like this:
cd d
put d/d1
cd ../e
put e/e1
put e/e2
cd ../f
put f/f1
Try spawning a shell to handle the redirection:
spawn sh -c {sftp user#example.com < tmp.txt}

SFTP prompting for password even though password is in script

I am trying to transfer a file from one server to a remote server using SFTP. Client is not ready for key setup so
I have gone through other questions on this forum related to SFTP and tried all. But still its not working in my case.
My Script :-
#!/bin/sh
# sample automatic ftp script to dump a file
USER="username"
PASSWORD="password"
HOST="hostname"
sftp $USER#$HOST << EOF
$PASSOWRD
cd test_path
put test_file.txt
quit
EOF
You have a misprint in your script - you are writing $PASSOWRD instead of $PASSWORD, so it substitutes empty string.
You can do this using expect. It's very easy and simple;
#!/usr/bin/expect
spawn sftp <userid>#<server>
expect "password:"
send "<password>\n"
expect "sftp>"
send "cd <remot dirctory>\r"
expect "sftp>"
send "mput * \r"
expect "sftp>"
send "quit \r"
Try the below steps,
lftp -u $user,$passwd sftp://$host << --EOF--
cd $directory
put $srcfile
quit
--EOF--

Resources