Write a Script to respond to prompts with specified text - macos

I am writing a script which will automatically capture a screenshot and trigger iSight to take a still photo when the script is run, then upload it to FTP. Everything is perfect other then one thing, my FTP requires a username and password to be entered when prompted (after both images are captured)
heres what I have:
echo “GOLDENEYE ACTIVATED….”
screencapture -x screen.jpg
imagesnap Mugshot.jpg
ftp <YourServerHere>
Terminal Responds:
Connected to <YourServerHere>
Now, Username must be entered when this appears:
220 Welcome to <ServerDomain>, FTP server standing by ...
Name (<ServerName>.<DomainName>.:<Name>): <Type UserName Here - Hit Enter>
And, Password will be entered upon successful entry of a valid Username:
331 Hello <UserName>, your FTP account password is required:
Password: <Type Your Password Here - Hit Enter>
After the proper credentials are entered, Terminal will respond:
230-Login successful, your current directory is /
230 0 Kbytes used (0%) - authorized: 7340032 Kb
Remote system type is UNIX.
Using binary mode to transfer files.
And, to upload the images:
ftp> put /Mugshot.jpeg
ftp> put /Screen.jpeg
I am attempting to Automate the username and password entry after the prompts are given, in order for the script to be successful
the text would be, "username" and "password" to be entered after the each prompt is given.
i am completely new to terminal and scripts, just playing around in my spare time, please excuse anything which is incorrect here, thanks for ANY help and education!

A general solution is the expect command.
But for your task, it's an overkill.
The ftp reads commands from a standard input.
So just do:
ftp -n < command.txt
The -n prevents the automatic prompts for username/password. We provide these explicitly using the user command below.
The commands.txt file will look like:
open host
user user password
put /path
bye

Related

Windows FTP login using "user" command in script fails

I have a simple ftp script file to upload a file to a server.
Script.txt contains:
open ftp.host.com
user Fred PASSword
send c:\hotsheet.dat
disconnect
quit
The cmd line is
Ftp -s:Script.txt
It starts processing correctly but the login fails. I think it is using all lowercase for my password.
The Windows ftp has two login modes. In the default one (the one you are using), it tries to login automatically once the open command is issued and reads the credentials from the script. So the script would have to look like:
open example.com
username
password
send c:\hotsheet.dat
disconnect
quit
So what is happening with your script is that the user Fred PASSword is used as a username as a whole (and similarly the send c:\hotsheet.dat is used as a password). You can tell that, if you add -d (debug) switch:
---> USER user Fred PASSword
331 Please, specify the password.
---> PASS send c:\hotsheet.dat
530 Login incorrect.
Login failed.
Or you can prevent the automatic login with -n switch:
ftp -s:Script.txt -n
And then explicitly login using the user command, just as your script is doing.

Login Bluehost FTP using Shell Script on Amazon AWS

Facing error in script while login FTP on Bluehost server using Shell Script on Amazon AWS.
I am able to login FTP using SSH successfully but when use Shell script to automate the FTP login it shows error LOGIN FAILED.
#!/bin/sh
HOST='HOST IP'
USER='username#domainname.com'
PASSWD='password'
ftp -inv $HOST << EOT
user $USER $PASSWD
EOT
exit 0
Below is the result:
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 50 allowed.
220-Local time is now 04:11. Server port: 21.
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.
Remote system type is UNIX.
Using binary mode to transfer files.
331 User username#domainname.com_ OK. Password required
530 Login authentication failed
Login failed.
221-Goodbye. You uploaded 0 and downloaded 0 kbytes.
221 Logout.
Use "quote USER" and "quote PASS" in your script
#!/bin/sh
HOST='HOST IP'
USER='username#domainname.com'
PASSWD='password'
ftp -inv $HOST << EOT
quote USER $LOGIN
quote PASS $PASSWORD
EOT
exit 0
i hope help you
This issue may be unique to BlueHost.
For years I had been successfully using a BASH script with a number of hosting providers for logging in and uploading changes to my SpamAssassin config file. After moving to BlueHost, the script failed, stalled at the password entry prompt, and eventually timed out with a login failure.
The script failed every time even though I was able to successfully login by manually initiating an FTP session and typing/answering the login prompts from a CLI terminal window.
I found the BlueHost password prompt did not like my script password which included a "$" dollar sign as part of the password. The "$" dollar sign embedded in the password was not being passed to the FTP password prompt even when the $PASSWORD variable was quoted as "$PASSWORD". Changing the password to replace the "$" dollar sign character with another character allowed the script to successfully login and upload.

How to connect to a remote system using ftp command?

I have to transfer files from machine to another(both windows platform) and for that purpose i am trying out with ftp commands. So, i am opening command prompt and typing the following:
C:\WINDOWS\system32> ftp
ftp> open sample.com
ftp> username
Now when i enter username it throws me error as "Not connected".I am not getting what am i doing wrong. The user name is absolutely correct.Please suggest how do i connect to remote system using ftp commands.
It seems that the "open" command failed and you are not really connected to the ftp server.
Here is what a good connection looks like:
C:\>ftp
ftp> open ftp.microsoft.com
Connected to ftp.microsoft.akadns.net.
220 Microsoft FTP Service
User (ftp.microsoft.akadns.net:(none)): anonymous
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230-Welcome to FTP.MICROSOFT.COM. Also visit http://www.microsoft.com/downloads.
230 User logged in.
ftp>
Here the username is "anonymous" and I provided that at the "User" prompt.
You can do this in another way:
C:\>ftp
ftp> open ftp.microsoft.com
Connected to ftp.microsoft.akadns.net.
220 Microsoft FTP Service
User (ftp.microsoft.akadns.net:(none)):
501 Invalid number of parameters.
Login failed.
ftp> user anonymous
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230-Welcome to FTP.MICROSOFT.COM. Also visit http://www.microsoft.com/downloads.
230 User logged in.
ftp>
Here I did not enter the username but used the "user" command provided by the ftp client to provide the username "anonymous"
None of these look like what you are experiencing. Could this be because you have removed some of the information in the interest of confidentiality? If so, that makes it difficult to help.
Also, it would help to read up on some help on using FTP command.
Here is one I found off google that I think would walk you through FTP commands: FTP and SFTP Beginners Guide with 10 Examples

FTP from terminal if username contains #?

How do you connect to an FTP server from the command line if your username is your email address? Usually I login in like this:
ftp username#ftp.server.com
But if my username is an email address, it doesn't work:
ftp myname#mysite.com#ftp.server.com
I've tried using an escape character \#, and putting the username in quotes. Neither work. I looked in the man pages and searched Google to no avail.
Take a look to man ftp and you shoud use $HOME/.netrc file containing the username. See:
user User [Password] [Account] Identifies the local user (User)
to the remote FTP server. If the Password or Account parameter is not specified and the remote server requires it, the ftp command prompts for the password or account locally. If the Account parameter is required, the ftp command sends it to the remote server after the remote login process completes.

How to convert Windows FTP script to WinSCP?

I need to use WinSCP in my legacy vb6 code. I always used my script like this:
open ftp.myserver.com
myusername
mypassword
passive on
cd myfolder\
ascii
put C:\temp\test.xml test.xml
close
quit
Similar script (with little change) always worked for sslftp, psftp, etc.
Now I need to create script to make this WinSCP work but it keeps throwing "host not found error". I'm not even trying the script. I'm trying it on the command window.
winscp> open ftp.myserver.com
Searching for host...
Network error: Connection timed out.
same ftp works on regular command line ftp:
ftp> open ftp.myserver.com
Connected to myserver.
220 Gene6 FTP Server v3.10.0
User (...): myuser
331 Password required for myuser
Password:
230 User manager logged in.
How do I run WinSCP? The documentation doesn't show any such example.
WinSCP defaults to the SFTP protocol on the port 22. If you want to use the FTP, you need to specify that explicitly.
Also username and password come in session URL as part of the open command, not on separate lines. The passive mode is specified using the -passive=on switch.
open ftp://myusername:mypassword#ftp.myserver.com -passive=on
The ascii mode is specified using the -transfer=ascii switch of the put command (though the separate ascii command is also understood for compatibility):
put -transfer=ascii C:\temp\test.xml test.xml
It's the exit, not the quit.
See the complete guide for converting Windows FTP script to WinSCP.
You should also read the guide to automating file transfers to FTP server.

Resources