Transferring the Contents of a Folder Using smbclient - shell

I have written a Shell script that moves into a directory with some binaries files present.
What I am looking to do is transfer all the files present inside this directory.
cd /home/user/binaries
smbclient //ip.address/directory$ password -W domain -U username << ENDOFMYSMBCOMMANDS
prompt
put *
exit
ENDOFMYSMBCOMMANDS
I tried to use put * to transfer all files - but this is not accepted.
The only other option I know of is to go one folder up, and use the command mput binaries - but this copies everything including the folder.
How can I modify my script to only transfer the contents of the directory?

I'm going to format the self-answer of Dustin a bit differently into a real one-liner. It is also possible to add the prepended "cd" command into the smbclient command, like so:
smbclient //ip.address/directory -W domain -U username \
-c 'prompt OFF; recurse ON; cd remote/target/directory; lcd /local/source/directory; mput *'

I had the answer with me all along!! I was under the impression that mput could only be used to transfer a directory, turns out that using mput * inside a directory will copy all the files located within that directory!
cd /home/user/binaries
smbclient //ip.address/directory$ password -W domain -U username << ENDOFMYSMBCOMMANDS
prompt
put *
exit
ENDOFMYSMBCOMMANDS
Going to leave this here for anyone else who gets stumped on this like me!

Related

E2E tests - removing FTP directory [duplicate]

I am connected to a Unix server and I am trying to, via FTP, delete the directory dir with several files in it. If I use
ftp> delete dir/*
I get
550 Wildcard is ambiguous.
When I use
ftp> prompt off
Interactive mode off.
ftp> mdelete dir/*
I still get
550 Wildcard is ambiguous.
When I try
ftp> glob
Globbing on.
ftp> mdelete dir
I'm prompted for every file.
How can I easily delete/empty-and-delete a directory without getting prompted for every file?
I got it to work in two steps, on a server with restricted access, no SFTP, only FTP through commandline.
Like this :
mdelete folder_name/*
rmdir folder_name
If you've hidden files or folders on your server (for example .folder), you have to set the lftp list-options to "-a".
So this worked for me:
$ lftp -u user,pass server
> set ftp:list-options -a
> cd /folder/to/be/empty/
/folder/to/be/empty/> glob -a rm -r *
Use lftp to log into your server, this supports the rm -r command.
lftp user, password server
then:
rm -r directory
the -r stands for "recursive".
info:
http://en.wikipedia.org/wiki/Lftp
http://en.wikipedia.org/wiki/Rm_%28Unix%29#Options
$ ftp -i ...
will turn off prompting on mdel, which is what you want. It can't be done inside ftp.
rmdir directoryName
this directory must be in the current directory however.
cheatsheet: http://www.cs.colostate.edu/helpdocs/ftp.html
I'm using Filezilla, and it deletes folders recursively. I believe the ftp does not have a command that recursively deletes folders.

How to run multi commands in one sh file?

I have a mac os and
my question is about 'How to run multi commands in one sh file?', like this :
#!/bin/bash
ftp
open domain.com
user
pass
cd /public_html/test_folder
lcd /Users/mac/downloads
mput file
a
This commands for :
open ftp
connect to my website
enter user name
enter password
cd << for open folder in my website
lcd << open folder in my computer
mput << upload file from my computer
a << for question in ftp for file type or something like this
I add && in end each line but no work he start and stop in command line number 2
I'm sorry for my bad language :(
Use lftp, very good in ftp scripting, when you require reconnect+continue file transfer, ssh/scp transfer, etc.
It should be similar to this:
#!/bin/bash
## note: mput: -c (retry+continue); run: lftp -c 'help mput' to see help
lftp -c 'mput -c -O ftp://name:passd#server.com/path/ file1.txt file2.txt'
Alternatively, you can write lftp scripts, example (note: mirror -R = upload):
#!/usr/bin/lftp -f
open ftp://ftp.server.com
user name passwd
cd /remote/path
lcd /local/path
mirror -R folder

"No such file or directory" but it's there. Shell script looping through array of folders

I'm trying to automate the download of a subdirectories in a directory. However, when executing the script, the last one or two directories cannot be found by the script - "No such file or directory". All others do fine and can be downloaded. This occurs for all directories I've tried this on which is strange to me. Why would it always not find the last two directories?
Can anyone help with this? Is it due to the loop? I've tried changing it to loop over the last ones only and this doesn't help. Or maybe it's due to the conversion of array=($l)?
Here's my script:
dirServer=/dir/to/location/in/server
dirLocal=/dir/to/location/in/local/pc
l=`ssh -t username#server 'ls' ${dirServer}`
#array of folders that should be copied to local machine
array=($l)
for folder in ${array[#]}
do
echo ${folder}
#if directory doesn't exist, creat it
mkdir -p ${dirLocal}${folder}
scp -r username#server:${dirServer}${folder}/analysis/ ${dirLocal}${folder}
done
Try change the following line including this "-o LogLevel=QUIET"
l=`ssh -o LogLevel=QUIET -t username#server 'ls' ${dirServer}`
Explanation:
That is coming from SSH. You see it because you gave the -t switch, which forces SSH to allocate a pseudo-terminal for the connection. Traditionally, SSH displays that message to make it clear that you are no longer interacting with the shell on the remote host, which is normally only a question when SSH has a pseudo-terminal allocated.

moving remote files inside ftp in shell script

I want to ftp file to my remote server and then move those files into another directory in the remote server
The ftp happens correctly but the movement is throwing an error like
550 RNFR command failed.
Can you please help?
My script is
#!/bin/sh
echo "Enter the version of the xml (eg:- v17.25)"
read version
HOST_FIRST='un01'
HOST_LAST='01'
USER='someuser'
PASSWD='somepassword'
HOST="$HOST_FIRST$FILE$HOST_LAST"
ftp -n $HOST <<-END_SCRIPT
quote USER $USER
quote PASS $PASSWD
cd /tmp
put myfile.xml
rename myfile.xml /tmp/test_ftp
quit
END_SCRIPT
exit 0
You have put myfile.xml to the tmp dir, why not just
edit
Change your script from
rename myfile.xml /tmp/test_ftp
TO
rename myfile.xml test_ftp
You have already put the file in the /tmp directory, which you have already done a cd /tmp .
That should work.
You can't specify a path in an ftp rename command and expect it to be moved as well.
And sorry, not what you want to here, but there is no move command in ftp. It is that way for security.
IHTH.

How to CD inside a SFTP connection where the connection is established using - Shell script

In my script - i create a sftp connection.
I read some directory value from user earlier and once the sftp connection is established, i try to cd to that dir which i got from the user.
But its not working, probably bec the prompt goes inside the server to which the SFTP connection was established.
In this case how to make it work ?
I also faced this problem and was able to find the solution. The solution is right there in the man page of sftp. In the man page, you will find where it is written the format of using sftp like this:
sftp [options] [user#]host[:dir[/]]
Actually, two formats are given there but this is the one I wanted to use and it worked.
So, what do you do? You simply supply the username#host as seen there, then, without any space followed by : and the path you want to change to in the client/remote server in your script and that's all. Here is a practical example:
sftp user#host:/path/
If your script does, as you state somewhere in this page,
sftp $user#$host cd $directory
and then tries to do something else, like:
sftp $user#$host FOO
That command FOO will not be executed in the same directory $directory since you're executing a new command, which will create a new connection to the SFTP server.
What you can do is use the "batchfile" option of sftp, i.e. construct a file which contains all the commands you'd like sftp to do over one connection, for example:
$ cat commands.txt
cd foo/bar
put foo.tgz
lcd /tmp/
get foo.tgz
Then, you will be able to tell sftp to execute those commands in one connection, by executing:
sftp -b commands.txt $user#$host
So, I propose your solution to be:
With user's input, create a temporary text file which contains all the commands to be executed over one SFTP connection, then
Execute sftp using that temporary text file as "batch file"
Your script would do something like:
echo "Directory in which to go:"
read directory
temp=$( mktemp /tmp/FOOXXX )
echo "" > $temp
echo "cd $directory" >> $temp
# other commands
sftp -b $temp $user#$host
rm $temp
If you are trying to change the directory of the machine, try lcd
In what way is it not working? To change directories on the remote server, you use the "cd" command. To change directories on the local server, you use the "lcd" command. Read "man sftp".

Resources