How to login and then run command in linux ftp client? - bash

I want to be able to connect to a ftp server automatically using a single file. This input file contains commands to run in ftp session.
I am able to connect and login to a ftp server via terminal using:
ftp <IP>
Connected to <IP>.
220 ProFTPD Server (ProFTPD) [<IP>]
Name (<IP>:current_user): USER
331 Password required for USER
Password:
I enter password, the output is:
230 User USER logged in
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
200 PORT command successful
150 Opening BINARY mode data connection for file list
drwxr-xr-x 4 USER psacln 4096 Aug 11 2020 backups
drwxr-xr-x 2 USER psacln 4096 Mar 2 2019 error_docs
drwxr-xr-x 53 USER psacln 4096 Jul 10 03:18 httpdocs
drwx------ 2 USER root 4096 Aug 4 06:32 logs
drwxrwxr-x 12 USER psacln 4096 Aug 10 2020 moodledata
226 Transfer complete
ftp>
Suppose a single file named cmd includes the command "ls" and also username & password:
#!/bin/bash
HOST=<IP>
USER=USER
PASSWORD=<PASS>
ftp $HOST <<EOF
user $USER $PASSWORD
Using above file as input to:
ftp <ip> < cmd
How to use this file to auto connect to this ftp server?

I've just solved it.
I just use encoded password in the bach script:
#!/bin/bash
HOST=<ip>
USER=USER
PASSWORD=$(echo <encoded password> | base64 --decode)
ftp -inv $HOST <<EOF
user $USER $PASSWORD
ls
bye
EOF
My password contains char &ampersand&, so it has error in connecting to ftp server.
Note that <encoded password> is obtained by:
echo '<PASS>' | base64
Just this!

Related

Curl download and delete file ftp

I need to download a file from a ftp server and delete it after is transferred (on remote server)
It's possibly in a single command line?
curl ftp://host/testfile.txt -X 'GET testfile.txt' --user user:password -o local.txt
curl ftp://host/testfile.txt -X 'DELE testfile.txt' --user user:password
Thanks
Yes, it is possible to do this in a single command:
curl -Q '-DELE testfile.txt' --user user:password -o local.txt ftp://host/testfile.txt
From the man page:
-Q, --quote
(FTP SFTP) Send an arbitrary command to the remote FTP or SFTP
server. Quote commands are sent BEFORE the transfer takes place
(just after the initial PWD command in an FTP transfer, to be
exact). To make commands take place after a successful transfer,
prefix them with a dash '-'. To make commands be sent after
curl has changed the working directory, just before the transfer
command(s), prefix the command with a '+' (this is only sup‐
ported for FTP). You may specify any number of commands.
WinSCP has an option -delete that you can combine either with put or get commands, to delete the source file after a succesfull change.
This option lead me to move from curl to WinSCP, because now I can write simpler "transactional" scripts, and I'm sure that when I delete the original file from the "transfer" folder, it is because it was successfully delivered.
https://winscp.net/
https://winscp.net/eng/docs/scriptcommand_get
https://winscp.net/eng/docs/scriptcommand_put
However, curl is quite powerful for other types of scripts and URL management, nowadays I use a mix of both utilities depending on the task to be done.
I recommend creating a script of your FTP commands, and piping them into the built-in ftp client. Here's an example (replace the ls command in the test.ftpscript with your GET/DEL commands) that worked on my machine:
[user#host ~] cat > test.ftpscript
user anonymous anonymous#gmail.com
ls
bye
[user#host ~] ftp -inv ftp.swfwmd.state.fl.us < test.ftpscript
Connected to ftp.swfwmd.state.fl.us (204.76.241.31).
220
331 Please specify the password.
230 Login successful.
227 Entering Passive Mode (204,76,241,31,191,167).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 7478 Dec 05 09:59 README.txt
drwx------ 2 0 0 16384 Dec 04 13:40 lost+found
drwxr-xr-x 20 0 0 4096 Dec 18 14:42 pub
lrwxrwxrwx 1 0 0 3 Dec 05 10:07 public -> pub
drwxr-xr-x 3 0 0 4096 Dec 04 15:26 pvt
226 Directory send OK.
221 Goodbye.
[user#host ~]

Scripting a file move on an FTP Server

I'm attempting to move multiple files on an FTP server to a different directory on the same server. So far, I've written a bash script that will login and retrieve any new files in the remote directory but the ftp command doesn't support a 'mv' command. Essentially the script would download the new file(s) and then once downloaded move the file(s) to a different directory on the same server. Please Note that the filenames will be different every time so the use of wildcards is important here.
Before you answer please note that this needs to be automated so using a GUI like Filezilla wouldn't help me as I would have to login to various ftp sites and move the files manually, also, keep in mind that I'm unable to ssh into any of the servers as they are managed by other company's and ftp access is all I'm able to get. Last thing, I won't know what the file names are so using a wildcard would be helpful.
Any help or guidance is truly appreciated.
Thank you!
Perhaps the rename command in ftp could work for you?
rename [from [to]]
Rename the file from on the remote machine, to the file to.
I gave it a bash with an old file I had sitting on a server and it seemed to do what you want:
ftp> ls tmp/test*
229 Entering Extended Passive Mode (|||55572|)
150 Accepted data connection
-rw-r--r-- 1 sinasohn sinasohn 21 Mar 31 16:37 tmp/testfile01
226-Options: -a -l
226 1 matches total
ftp> ls tmp2/test*
229 Entering Extended Passive Mode (|||64715|)
150 Accepted data connection
226-Options: -a -l
226 0 matches total
ftp> rename tmp/testfile01 tmp2/testfile01
350 RNFR accepted - file exists, ready for destination
250 File successfully renamed or moved
ftp> ls tmp/test*
229 Entering Extended Passive Mode (|||56698|)
150 Accepted data connection
226-Options: -a -l
226 0 matches total
ftp> ls tmp2/test*
229 Entering Extended Passive Mode (|||50239|)
150 Accepted data connection
-rw-r--r-- 1 sinasohn sinasohn 21 Mar 31 16:37 tmp2/testfile01
226-Options: -a -l
226 1 matches total
ftp>
I put blank lines in between commands here for clarity.
Hope this helps!
full script to achieve move more than one file
1. get file list from ftp server with mls command
2. generate to do list file
2.1 get file
2.2 rename (move file)
3. execute ftp command with to do list file
#!/bin/sh
clear
# change local directory
cd [local-directory]
#collect file names
ftp -ni ftp.abccompany.com <<EOF
user [user] [password]
cd /OUT
mls abc*.* list.txt
quit
EOF
# create ftp action list
echo >>todo.lst user [user] [password]
while read N
do
echo >>todo.lst cd /OUT
echo >>todo.lst get $N
echo >>todo.lst rename $N ARCHIVE/$N
done <list.txt
echo >>todo.lst quit
# ftp transfer process
ftp -nv ftp.abccompany.com <todo.lst
# cleanup
rm todo.lst

Windows 7 Batch Script - "Access Denied" error when using FTP

I wrote a batch script. I am on WINDOWS 7 and try to ftp file, which are on Linux
open MYIP
MYUSER
MYPASSWORD
hash
cd /home/mktftp/upload/coverimages
asc
get DONE.SVRIMG.14102014
quit
exit /b
and get output below:
ftp> Connected to 10.160.2.52.
open 10.160.2.52
220 10.160.2.52 FTP server ready
User (10.160.2.52:(none)):
331 Password required for user
530 Access was denied.
I don't understand, why it is not working.

is it possible to "ignore" your .ssh when accessing a server?

I have started working somewhere. There are servers I've been given access to using a public key generated from my computer. However, while the ssh login seems to be fine at first, I can't actually grab the repo using this rsync. It wants a password I shouldn't need. I emailed the previous dev back, and suggested my terminal is ignoring ssh? Is this even possible? How would I fix that?
Need to access this code :/
Thanks for your help!
Last login: Fri Jun 13 11:51:30 on ttys000
Naomis-MacBook-Air:summerspirit.github.io Naomi$ cd
Naomis-MacBook-Air:~ Naomi$ ssh naomi#svn.civicrm.ca
Last login: Fri Jun 13 09:56:56 2014 from 76-10-147-186.dsl.teksavvy.com
[naomi#emily ~]$ ls
community.socialinnovation.ca socialinnovation.ca
[naomi#emily ~]$ ls -alt
total 28
-rw-------. 1 naomi naomi 183 Jun 13 12:43 .bash_history
drwx------. 3 naomi naomi 4096 Jun 13 10:03 .
lrwxrwxrwx. 1 naomi naomi 35 Jun 13 10:03 socialinnovation.ca -> /var/www/vhosts/socialinnovation.ca
lrwxrwxrwx. 1 naomi naomi 45 Jun 13 10:02 community.socialinnovation.ca -> /var/www/vhosts/community.socialinnovation.ca
drwx------. 2 naomi naomi 4096 Jun 13 09:56 .ssh
drwxr-xr-x. 15 root root 4096 Jun 13 09:12 ..
-rw-r--r--. 1 naomi naomi 18 Jul 18 2013 .bash_logout
-rw-r--r--. 1 naomi naomi 176 Jul 18 2013 .bash_profile
-rw-r--r--. 1 naomi naomi 124 Jul 18 2013 .bashrc
[naomi#emily ~]$ mkdir yolo
[naomi#emily ~]$ cd yolo
[naomi#emily yolo]$ rsync -avz naomi#svn.civicrm.ca:/var/www/vhosts/socialinnovation.ca .
The authenticity of host 'svn.civicrm.ca (209.15.213.70)' can't be established.
RSA key fingerprint is 63:99:92:28:c3:dd:b8:eb:c0:ec:c5:3a:11:7a:0f:88.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'svn.civicrm.ca,209.15.213.70' (RSA) to the list of known hosts.
naomi#svn.civicrm.ca's password:
Permission denied, please try again.
naomi#svn.civicrm.ca's password:
Connection closed by UNKNOWN
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: unexplained error (code 255) at io.c(600) [receiver=3.0.6]
[naomi#emily yolo]$
[naomi#emily yolo]$ rsync -avz naomi#svn.civicrm.ca:/var/www/vhosts/socialinnovation.ca .
naomi#svn.civicrm.ca's password:
Permission denied, please try again.
naomi#svn.civicrm.ca's password:
Permission denied, please try again.
naomi#svn.civicrm.ca's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: unexplained error (code 255) at io.c(600) [receiver=3.0.6]
[naomi#emily yolo]$
[naomi#emily yolo]$ rsync -avz naomi#svn.civicrm.ca:/var/www/vhosts/community.socialinnovation.ca .
naomi#svn.civicrm.ca's password:
Permission denied, please try again.
naomi#svn.civicrm.ca's password:
Permission denied, please try again.
naomi#svn.civicrm.ca's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: unexplained error (code 255) at io.c(600) [receiver=3.0.6]
[naomi#emily yolo]$
from the dev 3:00 PM (19 hours ago)
to me, I'm thinking that your mac is somehow configured to ignore your .ssh keys when connecting.
Baffling, but reminds me why I'm not trying to run a public server ...
It looks like you're ssh'ing from Naomis-MacBook-Air to svn.civicrm.ca (aka emily) and then you're trying to ssh again (via rsync) to svn.civicrm.ca from there.
Three possible solutions:
1. rsync without the network
Because you are trying to rsync naomi#svn.civicrm.ca:/var/www/vhosts/socialinnovation.ca from svn.civicrm.ca you could just use:
rsync -avz /var/www/vhosts/socialinnovation.ca .
By-passing the need to use ssh over the network at all.
2. Make sure you are forwarding your agent
The reason that your .ssh on Naomis-MacBook-Air is being ignored is because you haven't allowed ssh to forward your agent. You can fix this by running:
echo "ForwardAgent yes" > ~/.ssh/config
On Naomis-MacBook-Air. This should allow you to continue using ssh and rsync on emily once you have ssh'd onto that remote server.
You can test that has worked by running ssh localhost repeatedly - it should never ask you for a password!
3. Copy your .ssh/id_rsa.pub and .ssh/id_rsa to the remote server and use them there
This is what you tried to do with pbcopy in the comment on the original question.
Once you've got /home/naomi/.ssh/id_rsa and /home/naomi/.ssh/id_rsa.pub on the remote server, and once you have chmod 400'd both files, you should be able to:
naomi#emily:~$ ssh-agent bash
naomi#emily:~$ ssh-add
naomi#emily:~$ rsync ...
You have to do the manual ssh-agent and ssh-add commands because there won't already be an agent running that you can use. This is not the case on your MacBook Air because MacOS X runs an agent in the background for you.
However I would recommend against this option because it means putting your private key on the remote server, which anybody with root access on that server would then be able to access.

equivalent ftp command for ftp rstatus on Solaris 10?

ftp rstatus $remotefile
is giving '?Invalid command' error on solaris. I came to know that there is no ftp command like rstatus on Solaris 10 unlike HP-UX. Basically on HP-UX, rstatus was used to get status of remote file. output like i.e.
-rw-r--r-- 1 dearshady users 20 Aug 2 10:28 remoteFile
On solaris ftp has ls command, but that is only for directory not file. Can anyone suggest a solution to get status of remote file on Solaris ftp?
You can use dir instead of rstatus
ftp> rstatus z
?Invalid command
ftp> dir z
200 PORT command successful.
150 Opening ASCII mode data connection for /bin/ls.
-rw-r--r-- 1 root root 34 mars 16 23:05 z
226 Transfer complete.
remote: z
58 bytes received in 0,00014 seconds (402,98 Kbytes/s)

Resources