I wish to upload all files in C:\Users\myuser\Downloads\SFTP folder on my local Windows to remote SFTP server myfolder directory.
I have multiple issues running sftp on Windows and achieving the above.
Below is my command:
C:\putty\psftp.exe -b C:\putty\sftp_commands.txt -l myuser -pw mypass 10.8.44.86
Here is my C:\putty\sftp_commands.txt file:
mkdir myfolder
cd myfolder
lcd "C:\Users\myuser\Downloads\SFTP"
put "C:\Users\myuser\Downloads\SFTP\*.*"
I get "unable to open" error despite the file exists on my local Windows.
This works when I change C:\Users\myuser\Downloads\SFTP\*.* to C:\Users\myuser\Downloads\SFTP\file1.txt.
C:\Users\myuser\Desktop>C:\putty\psftp.exe -b C:\putty\sftp_commands.txt -l myuser -pw mypass 10.8.44.86
Using username "myuser".
Pre-authentication banner message from server:
| EFT Server Enterprise 7.3.2.8
End of banner message from server
Keyboard-interactive authentication prompts from server:
End of keyboard-interactive prompts from server
Remote working directory is /
mkdir /myfolder: OK
Remote directory is now /myfolder
New local directory is C:\Users\myuser\Downloads\SFTP
local: unable to open C:\Users\myuser\Downloads\SFTP\*.*
Can you please suggest what is wrong with my code?
put does not support wildcards, you have to use mput:
lcd "C:\Users\myuser\Downloads\SFTP"
mput *.*
I wrote a bash script for uploading backup files from a server to a ftp server. But I always get an error.
Name (myftpserver:root): Permission denied.
Login failed.
Login with USER first.
Please login with USER and PASS.
Local directory now /backup01
Please login with USER and PASS.
Passive mode refused.
That's my script:
#!/bin/bash
DATE=`date +%Y-%m-%d_%H%M`
LOCAL_BACKUP_DIR="/backup01"
DB_NAME="databasename"
DB_USER="root"
FTP_SERVER="randomIP"
FTP_USERNAME="myname"
FTP_PASSWORT="supersecret"
FTP_UPLOAD_DIR="/home/mydirectory/ftp/upload"
LOG_FILE=/backup01/backup-$DATE.log
mysqldump -u $DB_USER $DB_NAME | gzip > $LOCAL_BACKUP_DIR/$DATE-$DB_NAME.sql.gz
ftp $FTP_SERVER << END_FTP
quote USER $FTP_USERNAME
quote PASS $FTP_PASSWORD
cd $FTP_UPLOAD_DIR
lcd $LOCAL_BACKUP_DIR
put "$DATE-$DB_NAME.sql.gz"
bye
END_FTP
if test $? = 0
then
echo "Database successfully uploaded to the FTP Server!"
echo "Database successfully created and uploaded to the FTP Server!" | mail -s "Backup from $DATE" my.email#whereever.com
else
echo "Error in database upload to Ftp Server" > $LOG_FILE
fi
Maybe someone can help me, because I've tried everything I've found on the internet.
I've made a .netrc file. I configured the vsftpd.conf, enabled passiv mode, enabled user list and I've made a lot of other stuff...
But now I'm having no idea what else I have to do to make this script working the way it should. And I have no idea why it's trying to connect via root...
Maybe there is someone out there who can help.
Thanks in advance.
I use:
ftp -v -n >> /tmp/ftpb.log <<EOF
open $URL
user $USER $PASS
binary
put $FILE
quit
EOF
and works
It's a common staple to use something like this:
$: ftp -vn <<!
> open localhost
> user foo
> put someFile
> quit
> !
> ftp: connect :Connection refused
Not connected.
Not connected.
Not connected.
$: echo $?
0
Unfortunately ftp considers that it successfully reported all problems, so it exits with a happy zero.
Use scp:
if scp "$lcldir/$filename" "$usr/$pw#$svr:$dir/"
then echo "file delivered"
else echo "delivery failed"
fi
If you can't use scp try something like expect, or write something in Perl - some way you can interactively test and confirm each step.
As a last resort, make the here-doc send the file and then pull it back to a tempfile that doesn't already exist locally. If you can successfully cmp file1 file2 afterwards, the send must have worked ok.
I want to transfer a file using LFTP.
Connection Type: Passive connection Implicit SSL
Protocol: FTPS
Steps followed by me:
-bash-4.1$ set ftp:ssl-protect-data true
-bash-4.1$ set ftps:initial-prot
-bash-4.1$ set ftp:ssl-force true
-bash-4.1$ set ftp:ssl-protect-data true
-bash-4.1$ set ssl:verify-certificate off
-bash-4.1$ lftp
lftp :~> open ftps.host.com
lftp ftps.host.com:~> user u001
Password:
lftp u001#ftps.host.com:~> pwd
ftp://u001#ftps.host.com
lftp u001#ftps.host.com:~> ls
Interrupt
lftp u001#ftps.host.com:~> cd folderName
Interrupt
lftp u001#ftps.host.com:~> ls -ltr
`ls -ltr' at 0 [Connecting...]
Telnet is connecting & working but LFTP seems to hang. Can anybody please provide a solution to this?
lftp seems to be unable to connect to the server. You can use debug command to enable verbose messages and see what happens in detail. One of possible reasons is an incorrect port number to connect to. You can specify a port number explicitly by -p NNN option to open command, or just use open ftp://u001#ftps.host.com:NNN where NNN is the port number.
BTW, the set commands should be given inside lftp.
OS: OS X 10.9.3
lftp version:
LFTP | Version 4.5.1 | Copyright (c) 1996-2014 Alexander V. Lukyanov
After upgraded to lftp 4.5.1, I got this error when mput files to the same ftp.
Is there any advice? Thanks
try this:
lftp -e 'set ssl:verify-certificate false' -u username,password -p 1234 ftp.example.com
or add config in ~/.lftprc
set ssl:verify-certificate false
I need to transfer a log file to a remote host using sftp from a Linux host. I have been provided credentials for the same from my operations group. However, since I don't have control over other host, I cannot generate and share RSA keys with the other host.
So is there a way to run the sftp command (with the username/password provided) from inside the Bash script through a cron job?
I found a similar Stack Overflow question, Specify password to sftp in a Bash script, but there was no satisfactory answer to my problem.
You have a few options other than using public key authentication:
Use keychain
Use sshpass (less secured but probably that meets your requirement)
Use expect (least secured and more coding needed)
If you decide to give sshpass a chance here is a working script snippet to do so:
export SSHPASS=your-password-here
sshpass -e sftp -oBatchMode=no -b - sftp-user#remote-host << !
cd incoming
put your-log-file.log
bye
!
Another way would be to use lftp:
lftp sftp://user:password#host -e "put local-file.name; bye"
The disadvantage of this method is that other users on the computer can read the password from tools like ps and that the password can become part of your shell history.
A more secure alternative which is available since LFTP 4.5.0 is setting the LFTP_PASSWORD environment variable and executing lftp with --env-password. Here's a full example:
export LFTP_PASSWORD="just_an_example"
lftp --env-password sftp://user#host -e "put local-file.name; bye"
# Destroy password after use
export LFTP_PASSWORD=""
LFTP also includes a cool mirroring feature (can include delete after confirmed transfer --Remove-source-files):
lftp -e 'mirror -R /local/log/path/ /remote/path/' --env-password -u user sftp.foo.com
EXPECT is a great program to use.
On Ubuntu install it with:
sudo apt-get install expect
On a CentOS Machine install it with:
yum install expect
Lets say you want to make a connection to a sftp server and then upload a local file from your local machine to the remote sftp server
#!/usr/bin/expect
spawn sftp username#hostname.com
expect "password:"
send "yourpasswordhere\n"
expect "sftp>"
send "cd logdirectory\n"
expect "sftp>"
send "put /var/log/file.log\n"
expect "sftp>"
send "exit\n"
interact
This opens a sftp connection with your password to the server.
Then it goes to the directory where you want to upload your file, in this case "logdirectory"
This uploads a log file from the local directory found at /var/log/ with the files name being file.log to the "logdirectory" on the remote server
You can use lftp interactively in a shell script so the password not saved in .bash_history or similar by doing the following:
vi test_script.sh
Add the following to your file:
#!/bin/sh
HOST=<yourhostname>
USER=<someusername>
PASSWD=<yourpasswd>
cd <base directory for your put file>
lftp<<END_SCRIPT
open sftp://$HOST
user $USER $PASSWD
put local-file.name
bye
END_SCRIPT
And write/quit the vi editor after you edit the host, user, pass, and directory for your put file typing :wq .Then make your script executable chmod +x test_script.sh and execute it ./test_script.sh.
I was recently asked to switch over from ftp to sftp, in order to secure the file transmission between servers. We are using Tectia SSH package, which has an option --password to pass the password on the command line.
example : sftp --password="password" "userid"#"servername"
Batch example :
(
echo "
ascii
cd pub
lcd dir_name
put filename
close
quit
"
) | sftp --password="password" "userid"#"servername"
I thought I should share this information, since I was looking at various websites, before running the help command (sftp -h), and was i surprised to see the password option.
You can override by enabling Password less authentication. But you should install keys (pub, priv) before going for that.
Execute the following commands at local server.
Local $> ssh-keygen -t rsa
Press ENTER for all options prompted. No values need to be typed.
Local $> cd .ssh
Local $> scp .ssh/id_rsa.pub user#targetmachine:
Prompts for pwd$> ENTERPASSWORD
Connect to remote server using the following command
Local $> ssh user#targetmachine
Prompts for pwd$> ENTERPASSWORD
Execute the following commands at remote server
Remote $> mkdir .ssh
Remote $> chmod 700 .ssh
Remote $> cat id_rsa.pub >> .ssh/authorized_keys
Remote $> chmod 600 .ssh/authorized_keys
Remote $> exit
Execute the following command at local server to test password-less authentication.
It should be connected without password.
$> ssh user#targetmachine
The easiest way I found to accomplish this, without installing any third-party library like Expect, SSHPASS...etc, is by using a combination of CURL, and SFTP. Those two are almost in every Linux machine.
This is the command you should execute, after changing the values.
curl -k "sftp://SERVER_IP:SERVER_PORT/FULL_PATH_OF_THE_FILE" --user "SERVER_USER:SERVER_PASSOWRD" -o "THE_NAME_OF_THE_FILE_AFTER_DOWNLOADING_IT"
Example:
curl -k "sftp://10.10.10.10:77/home/admin/test.txt" --user "admin:123456" -o "test.txt"
Explanation:
We are connecting to the server 10.10.10.10:77 using the username admin and password 123456, to move the file /home/admin/test.txt from that server to the server you are using currently to execute the above command.
Combine sshpass with a locked-down credentials file and, in practice, it's as secure as anything - if you've got root on the box to read the credentials file, all bets are off anyway.
Bash program to wait for sftp to ask for a password then send it along:
#!/bin/bash
expect -c "
spawn sftp username#your_host
expect \"Password\"
send \"your_password_here\r\"
interact "
You may need to install expect, change the wording of 'Password' to lowercase 'p' to match what your prompt receives. The problems here is that it exposes your password in plain text in the file as well as in the command history. Which nearly defeats the purpose of having a password in the first place.
You can use sshpass for it. Below are the steps
Install sshpass For Ubuntu - sudo apt-get install sshpass
Add the Remote IP to your known-host file if it is first time
For Ubuntu -> ssh user#IP -> enter 'yes'
give a combined command of scp and sshpass for it.
Below is a sample code for war coping to remote tomcat
sshpass -p '#Password_For_remote_machine' scp /home/ubuntu/latest_build/abc.war #user##RemoteIP:/var/lib/tomcat7/webapps
You can use a Python script with scp and os library to make a system call.
ssh-keygen -t rsa -b 2048 (local machine)
ssh-copy-id user#remote_server_address
create a Python script like:
import os
cmd = 'scp user#remote_server_address:remote_file_path local_file_path'
os.system(cmd)
create a rule in crontab to automate your script
done
A few people have mentioned sshpass but not many clear coding examples...
This is how we are doing it with bash scripts for rsync backups:
sshpass -p "${RSYNC_PASSWORD}" sftp "${RSYNC_USER}"#"${RSYNC_REMOTE_HOST}"
Keep in mind you will have to sudo apt install sshpass before this works properly.