Shell script with SFTP and mget finishing before downloads are complete - bash

Here is my script:
#!/usr/bin/expect
spawn /usr/bin/sftp user#place
expect "password:"
send "mysecret\n"
expect "sftp>"
send "mget dir/*.csv\n";
expect "sftp>"
send "quit\n"
It starts to download but after a few seconds, it sends the quit command and exits the program before the downloads are complete.
The only way I've successfully made it stop is to throw in an "interact" command but because I want to run this as a cron job, I don't want to just spawn processes that end up sitting.
Is there a way to accomplish this?
edit:
Someone will ask why I don't have a key setup... this is the only method allowed to me by the vendor.

Adding the line set timeout -1 appears to have fixed this problem
The whole script should look as such:
#!/usr/bin/expect
set timeout -1
spawn /usr/bin/sftp user#place
expect "password:"
send "mysecret\n"
expect "sftp>"
send "mget dir/*.csv\n";
expect "sftp>"
send "quit\n"

Well, if you can use Perl:
use Net::SFTP::Foreign;
my $sftp = Net::SFTP::Foreign->new('user#place', password => 'mysecret');
$sftp->mget("dir/*.csv", '.',
on_error => sub {
warn "unable to copy '$_[1]{filename}':".$sftp->error."\n";
});
You will have to install Net::SFTP::Foreign and IO::Pty (also available as packages in several linux distributions).

Related

Expect script to configure PDU

I need a script to configure PDU (power distribution unit). The script will be called from another program. I created Expect script that gets parameters, connects to PDU by ssh and runs the command. The short commands, such as 'pwd' or 'lang' works correctly, but the long commands stuck waiting for user interaction. If I do nothing, the connection is closed after timeout, if I press Enter, the script continue and pass.
There is a script:
spawn ssh $user#$pdu
interact -o -nobuffer "assword:" return
send "$password\r"
interact -o -nobuffer "apc>" return
send "olStatus $host\r"
interact -o -nobuffer "apc>" return
Running it with debug flag gave the following output
apc>tty_set: raw = 0, echo = 1
send: sending "olStatus machine-123456\r" to { exp4 }
defining key apc>, action return
tty_raw_noecho: was raw = 0 echo = 1
spawn id exp4 sent <olStatus machine-123>
olStatus machine-123spawn id exp0 sent <\r>
spawn id exp4 sent <456\r\n23: machine-123456: On \r\nE000: Success\r\n\r\napc>\r\napc>>
456
23: machine-123456: On
E000: Success
Here I added the comments
How can I solve the problem?
This is what I'd suggest:
spawn ssh $user#$pdu
expect "assword:"
send "$password\r"
expect -re {apc>$}
send "olStatus $host\r"
expect -re {apc>$}
send "exit\r" # or "quit" or whatever
expect eof
But run it with debug enabled and let me know what you get.
Thanks to #GlennJackman
I ran autoexpect and this help me to understand how to solve the issue. Maybe it is a my system problem, but currently it is not matter.
What needed to be done was to send in two steps
send "olStatus "
expect "olStatus"
send "machine-123456\r"
expect -re {apc>$}

How to automate the process with expect tool?

I want to automate a process with expect :
1.login with a common user in ssh
2.su to root
3.make diretory /home/test
set user xxxx
set host yyyy
set password1 pass1
set password2 pass2
set timeout 60
spawn ssh $user#$host
expect "*assword:*"
send "$password1\r"
spawn su
expect "*assword:*"
send "$password2\r"
send "mkdir /home/test"
close
How to fix the codes to automate my process?
Basically, expect will work with two feasible commands such as send and expect. In this case, if send is used, then it is mandatory to have expect (in most of the cases) afterwards. (while the vice-versa is not required to be mandatory)
This is because without that we will be missing out what is happening in the spawned process as expect will assume that you simply need to send one string value and not expecting anything else from the session, making the script exits and causing the failure.
Apart from the commands being missed, I see one issue with su command. I hope your intention is to create directory with su command execution in the remote host, not in local. So, assuming that, you just have to send the command as su to the server. Your current code will spawn su in the local machine.
#!/usr/bin/expect
set host xxx.xxx.xxx.xxx
set user dinesh
set loginpwd welcome
set adminpwd root
set timeout 60
set prompt "#|>|\\\$"; # We escaped the `$` symbol with backslash to match literal '$'
spawn ssh $user#$host
expect -nocase "password:"
send "$loginpwd\r"
expect -re $prompt
send "su\r"
expect -nocase "password:"
send "$adminpwd\r"
expect -re $prompt
send "mkdir /home/test\r"
expect -re $prompt
send "logout\r"
expect eof
eof will wait for the closure of the ssh session.

bash script which downloads a file with sftp

I need to make a sftp connection with a password and download a file. There is an ip restriction so first of all i should make a ssh connection. I wrote a script but it stucks after connecting with ssh.
Note: i also tried doing it with an expect script but it also didn't work.
#!/usr/local/bin/
ssh test#test1.t.com
lftp sftp://test2:123456#test2.com
get "file.xls"
Edit: You can also see my expect code here.
#!/usr/local/bin/expect -f
expect -c "
spawn ssh test#test1.t.com
expect \"test\#test1\:\~\$\"
spawn sftp test2#test2.com
expect \"*assword:\"
send \"123456\r\"
expect \"sftp\>\"
send \"get file.xls\r\"
expect \"sftp\>\"
exit 1
";
I'm not sure exactly what you're trying to accomplish here. First, I'll address the problems in your expect script. Since your shebang line invokes expect, you don't need to wrap the expect body in a call to expect. That gets rid of all the backslashes. Next, you have 2 spawn calls, which raises questions about you're intent. I'm going to assume that you want to ssh to test1, then grab the file from test2 so the file exists on test1. This assumption changes the 2nd spawn to a plain send command.
#!/usr/local/bin/expect -f
set shell_prompt "test#test1:~$"
set sftp_prompt "sftp>"
spawn ssh test#test1
expect $shell_prompt
send "sftp test2#test2\r"
expect "*assword:"
send "123456\r"
expect $sftp_prompt
send "get file.xls\r"
expect $sftp_prompt
send "exit\r"
expect $shell_prompt
send "exit\r"
expect eof
Now, you can scp the file to your local machine. Let's put those 2 steps into one shell script:
#!/bin/sh
expect <<'EXPECT_SCRIPT'
set shell_prompt "test#test1:~$"
set sftp_prompt "sftp>"
spawn ssh test#test1
expect $shell_prompt
send "sftp test2#test2\r"
expect "*assword:"
send "123456\r"
expect $sftp_prompt
send "get file.xls\r"
expect $sftp_prompt
send "exit\r"
expect $shell_prompt
send "exit\r"
expect eof
EXPECT_SCRIPT
scp test#test1:file.xls .

BASH: Send directory files from server(non-sftp) to remote server that use sftp

I can send files one by one by changing the file name, the files are increasing in number so I tried using for loop but it did not work.
I need to send all the files in FILE variable to the remote machine USER#$HOST:/Semi/KFILE-ENG/ELD. Also, the remote machine is sftp enabled, while the sender is using ftp only.
This is what I only have.
HOST=ftpabc.abc.cn
PORT=22
USER=user
PASSWORD=pass
FILE=/u01/flatringDev/ToVendor/LC7/ELD/ELD.7
spawn /usr/bin/sftp $USER#$HOST:/Semi/KFILE-ENG
expect "password:"
send "$PASSWORD\r"
expect "sftp>"
send "mkdir ELD\r"
expect "sftp>"
send "cd ELD\r"
expect "sftp>"
send "put $FILE\r"
expect "sftp>"
send "bye\r"
If someone knows a solution, please let me know. Thank you
#!/bin/bash
HOST=ftpabc.abc.cn
PORT=22
USER=user
PASSWORD=pass
declare -a arry
arry=('file1' 'file2' 'file3' 'file4' '/tmp/kl.pl') # list files with absolute path eg: /tmp/kl.pl
#for FILE in $(ls -1 DIR_containing_files) # If you can all files in same dir, else create an array with elements as filenames with absoulte path and use 2nd for statement
for FILE in ${arry[*]}
do
spawn /usr/bin/sftp $USER#$HOST:/Semi/KFILE-ENG
expect "password:"
send "$PASSWORD\r"
expect "sftp>"
send "mkdir ELD\r"
expect "sftp>"
send "cd ELD\r"
expect "sftp>"
send "put $FILE\r"
expect "sftp>"
send "bye\r"
sleep 5
done
Check if this works, you can have a bash function doing all the expect thing and call it in for loop
or an expect script file and call that in bash for

Pipe stdin into "expect" script

I am uploading a file via ftp using expect. The file is piped into my bash script.
#!/bin/bash
HOST='example.com'
USER='XXX'
PASSWD='XXX'
expect << EOT
spawn ftp $HOST
expect "Name*:"
send "$USER\r"
expect "Password:"
send "$PASSWD\r"
expect "ftp>"
send "binary\r"
expect "ftp>"
send "prompt\r"
expect "ftp>"
send "put - $1\r" ####
expect "ftp>"
send "bye\r"
expect eof
EOT
On the highlighted line I want ftp to get access to the main script stdin.
Thank you
I believe the key to what you are looking for is Expect's interact command. You'd get a script like this:
#!/usr/bin/env expect
# Set these in the Tcl way, not the bash way
set HOST "example.com"
set USER "XXX"
set PASSWD "YYY"
# You should recognize this bit...
spawn ftp $HOST
expect "Name*:"
send "$USER\r"
expect "Password:"
send "$PASSWD\r"
expect "ftp>"
send "binary\r"
expect "ftp>"
send "prompt\r"
expect "ftp>"
# New stuff starts here
send "put - [lindex $argv 0]\r"
interact {
"ftp>" { return }
}
send "bye\r"
wait
exit
I've rewritten your script so it doesn't use a here document, because that would have interfered with the reading of the content (here-docs are presented as stdinā€¦) and switched it to use a few more idiomatic ways of doing things (idiomatic argument access being the main one).
That said, if I was doing this sort of thing for real, I'd look into using the ftp package from Tcllib as that talks the protocol directly instead of using a possibly-problematic subprocess. (Indeed, if you were going to be doing this on Windows, you'd have to do it that way because of quirks of how Expect and FTP.EXE work on that platform).

Resources