【问题标题】:How to make scp in Shell Script ask me for password如何在 Shell 脚本中制作 scp 询问我的密码
【发布时间】:2017-04-23 11:12:53
【问题描述】:

我正在编写一个脚本来通过scp 在我的两台机器之间安全地传输数据。

但在脚本中,由于没有密码而显示错误。那么如何让我的 shell 脚本在执行scp 命令后向我询问密码呢?

这是我的csh 脚本。

# 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

现在我正在以下列方式将上述脚本与ssh 一起使用。

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

当我以上述方式使用时,执行scp命令时不提示输入密码。

【问题讨论】:

  • scp 从脚本运行时应该要求输入密码,就像手动运行时一样。运行脚本时是否重定向输入?
  • 在我的情况下它不需要任何密码。实际上它只是终止命令。
  • 即使您以交互方式使用它?您是否配置了私钥? scp -v 显示什么?
  • 不,如果我在 shell 命令行中使用scp,那么它会要求输入密码。但如果该命令在脚本中,则不会。
  • 程序在脚本中的运行方式并没有什么不同,除非脚本执行重定向输入之类的操作。发布您的脚本。

标签: linux shell ssh scp csh


【解决方案1】:

你可以使用 sshpass https://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/

我用过一次直接scp或者ssh不提示密码。

例如:

 sshpass -p 'password' scp file.tar.gz root@xxx.xxx.xxx.194:/backup

【讨论】:

  • @Aserre 谢谢 sshpass -p 'password' scp file.tar.gz root@xxx.xxx.xxx.194:/backup
【解决方案2】:

正如另一个答案所提到的,sshpass 将完美地完成这项工作。如果您无法在本地计算机上安装新软件包,您还可以使用expect(大多数发行版上默认安装)来自动化您的交互式会话。

expect的基本语法是等待程序显示特定的字符串(expect mystring),从而触发特定的行为(send command

以下脚本显示了实现所需内容的基本结构:

#!/usr/bin/expect -f
# syntax to specify which command to monitor
spawn scp myfile user@remote.host:/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"

附带说明,您还可以通过 ssh 密钥设置无密码授权。

#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 解释了如何使用keychain 工具来管理多个 ssh 密钥和用户。

【讨论】:

  • 我尝试使用ssh-keygen -t rsa,但不知何故对我不起作用
  • @KaranShah 它做了什么?你收到错误信息了吗? ls ~/.ssh 的输出是什么?
  • 出现错误:debug1:可以继续的身份验证:publickey,gssapi-keyex,gssapi-with-mic,password 权限被拒绝,请重试。
  • @KaranShah 检查您的远程计算机上的/etc/ssh/ssh_config。确保它具有以下几行:RSAAuthentication yesPubkeyAuthentication yes。另外,请确保您对远程 .ssh 文件夹具有适当的授权:chmod 700 ~/.ssh/*
【解决方案3】:
ssh <username>@<hostname> "<script name> <args>"

scp 只会从 TTY 读取密码,在这种情况下它没有 TTY。当您运行ssh 并指定要在远程系统上执行的命令时(就像您在此处所做的那样),ssh 默认情况下不会在远程系统上分配 PTY(伪 tty)。您的脚本和从它启动的所有命令(包括 scp)最终会在没有 TTY 的情况下运行。

您可以使用 ssh -t 选项使其在远程系统上分配一个 tty:

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

如果你收到类似“Pseudo-terminal will not be assigned because stdin is not a terminal”的消息,那么再添加一个-t:

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

【讨论】:

    猜你喜欢
    • 2017-03-14
    • 2010-11-30
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2014-01-04
    • 2011-12-28
    相关资源
    最近更新 更多