Why heredoc does not work for sftp password? [duplicate] - bash

This question already has answers here:
How to run the sftp command with a password from Bash script?
(12 answers)
Closed 2 years ago.
I want to input the password to sftp using heredoc:
sftp root#example.com <<EOF
password
EOF
But it does not work. sftp still asks me to type password. I know I can use sshpass to input the password, but why cannot I just use the heredoc to input the password. I think heredoc will be read as the standard input for sftp. Am I misunderstanding something?

sftp will read the password from /dev/tty or some other program when running under graphical environment. When you run sftp from the terminal, stdin is /dev/tty. The heredoc replaces stdin, but sftp will always read /dev/tty for password.

I suggest to use sshpass:
SSHPASS='password' sshpass -e sftp root#example.com
From man sshpass:
-e: The password is taken from the environment variable "SSHPASS".

Related

Automatic ssh script without expect and without key (with user/password) [duplicate]

This question already has answers here:
Shell script to automate SSH login using password
(2 answers)
Closed 5 years ago.
I am trying to make a script in bash that connects via ssh to another machine.
I only have user access to this machines so
I can't use expect (It is not installed and I can't install it)
I can't install ssh keys
So I have to log in via username and password.
Is there a way to make my script send my password just using bash?
You can do that with sshpass, however, it's insecure.
sshpass -p YOUR_PW ssh ...
That will connect without asking for password.
You might need to add the flag -oStrictHostKeyChecking=no to ssh for auto-accepting keys.

SFTP with Password VIA Shell Script [duplicate]

This question already has answers here:
How to run the sftp command with a password from Bash script?
(12 answers)
Closed 8 years ago.
Is it possible to pass SFTP USER/PASS to a server in an automated script that will log in and retrieve a file?
I know that KEY PAIRS are the recommended method but assume thats not possible in this case.
In the simplest case you use a key based authorization so you don't need to enter any credentials.
For doing that create a key:
ssh-keygen -t rsa
And copy it to the target system:
ssh-copy-id -i ~/.ssh/id_rsa.pub user#remote-system
Now you can login to the system without a password.
If your problem is the missing ssh-copy-id command try this here:
cat ~/.ssh/*.pub | ssh user#remote-system 'umask 077; cat >>.ssh/authorized_keys'

Sftp using a remote user without shell [duplicate]

This question already has answers here:
How to run the sftp command with a password from Bash script?
(12 answers)
Closed 8 years ago.
I need to make a shell script that will transfer files to a remote server however, the account given to me has no shell mainly because they want to restrict the access to sftp only. I already have a shell script my only problem is that I cannot automate it. The script stops on the password prompt. I read on most of the passwordless sftp tutorials that I need to generate a keypair but like i've said I cannot do this as the remote account has no shell access. What are the alternatives for passwordless sftp considering an account without shell? The remote server has no 'expect' installed.
Thank you very much.
EDIT: Please also note that I cannot install anything in the local server. I MAY be able to do something on the remote server but not on the local.
On Debian and Ubuntu (maybe other systems also) you can use sshpass
apt-get install sshpass
sshpass -p 'YourPassword' ssh user#host
OR
sshpass -p 'YourPassword' sftp user#host
But the you will have your password printed in your bash_history..
So you might want to pass the password from a file or an environment-variable.
Greets, Eric

Execute a shell script in another unix machine using SSH [duplicate]

This question already has answers here:
SSH in shell script with password
(3 answers)
Closed 9 years ago.
I am trying to execute a script which is present in another unix machine from my unix box using below command:
HOST=myhostname
USER=myuser
ssh -o StrictHostKeyChecking=no -l $USER $HOST "/tmp/myscript.sh"
But the command prompts me to enter the password, but I don't want the command to prompt for password, instead of that I want to pass the password as parameter for my command. But I am not able to find an option to pass the password as parameter to SSH command.
Please help me on how to send password as part of command, instead of the command to prompt it. I am using BASH shell script.
Use EXPECT
Save this as mylogin.exp (or some other name you like), and change the names and password:
#!/usr/bin/expect -f
spawn ssh myname#some_server.com
expect {
password: {send "mypassword\n"}
}
interact
Then just run the command:
./mylogin.exp
That will just get you logged in. If you want to run a command instead, you can just put that at the end of the ssh command.

Automatically input a password when a bash script is run [duplicate]

This question already has answers here:
How to provide password to a command that prompts for one in bash?
(8 answers)
Closed 8 years ago.
For example, say if I have a script saying:
#!/bin/bash
sudo setpci -s 00:02.0 F4.B=00
How do I put the root password into the script so that it accepts it as the password when it reads and executes the sudo line (so I don't have to type it manually)?
Spawning an expect session within your bash script is typically how you automate interactive prompts.
e.g.
expect -c "
spawn sudo setpci -s 00:02.0 F4.B=00
expect -nocase \"password:\" {send \"$PASS\r\"; interact}
"
Note that this isn't the recommended approach in this case as it is a huge security hole to store your root password in a bash script.
The correct solution would be to edit your /etc/sudoers/ to not prompt you for a password for that binary.
#in /etc/sudoers
neohexane ALL=(ALL) NOPASSWD : /usr/bin/setpci

Resources