Karan Shah
Karan Shah

Reputation: 1992

How to make scp in Shell Script ask me for password

I am making a script to securely transfer data between my two machines through scp.

But in the script, it shows an error due to no password. So how can I make my shell script to ask me for password after executing scp command?

Here is my csh script.

# ssh shahk@sj-shahk
# ls -al
echo "Source Location of Remote Server - $1"
echo "Destination Location of Local Server - $2"
echo "File/Folder to be Transferred from Remote Server - $3"

echo "File Transfer Starts"
scp -rv $1/$3 <username>@<hostname>:$2
echo "File Transfer Completed"

# exit

Now I am using the above script with ssh in following way.

ssh <username>@<hostname> "<script name> <args>"

When I use in the above manner, it does not prompt for password while executing scp command.

Upvotes: 1

Views: 14150

Answers (3)

Kenster
Kenster

Reputation: 25380

ssh <username>@<hostname> "<script name> <args>"

scp will only read a password from a TTY, and it doesn't have a TTY in this case. When you run ssh and specify a command to be executed on the remote system (as you're doing here), ssh by default doesn't allocate a PTY (pseudo-tty) on the remote system. Your script and all of the commands launched from it--including scp--end up running without a TTY.

You can use the ssh -t option to make it allocate a tty on the remote system:

ssh -t <username>@<hostname> "<script name> <args>"

If you get a message like "Pseudo-terminal will not be allocated because stdin is not a terminal", then add another -t:

ssh -tt <username>@<hostname> "<script name> <args>"

Upvotes: 0

Aserre
Aserre

Reputation: 5062

As mentioned by the other answer, sshpass will do the job perfectly. In the case where you can not install new packages on your local computer, you can also use expect (installed by default on most distros) to automate your interactive session.

The basic syntax of expect is to wait for the program to display a specific string (expect mystring), which triggers a specific behaviour (send command)

The following script shows the basic structure to implement what you need :

#!/usr/bin/expect -f
# syntax to specify which command to monitor
spawn scp myfile [email protected]:/dest_folder

# this syntax means we expect the spawned program to display "password: "
# expect can understand regex and glob as well. read the man page for more details
expect "password: "

# the \r is needed to submit the command
send "PASSWORD\r"

# expect "$ " means we wait for anything to be written.
# change if you want to handle incorrect passwords
expect "$ "
send "other_command_to_execute_on_remote\r"
expect "$ "
send "exit\r"

As a side note, you can also set up passwordless authorizations through ssh keys.

#1) create a new ssh key on your local computer
> ssh-keygen -t rsa
#2) copy your public key to your remote server
# you will need to login, but only once. Once the key is on the remote server, you'll be able to connect without password.
> ssh-copy-id -i ~/.ssh/id_rsa.pub user@ip_machine
# OR
> cat ~/.ssh/id_rsa.pub | ssh user@ip_machine "cat - >> ~/.ssh/authorized_keys"

This tutorial explains how to use the keychain tool to manage several ssh keys and users.

Upvotes: 1

Hemant Manwani
Hemant Manwani

Reputation: 128

You can use sshpass https://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/

I have used it once to directly scp or ssh without prompting password.

For example :

 sshpass -p 'password' scp file.tar.gz [email protected]:/backup

Upvotes: 3

Related Questions