简体   繁体   English

如何 scp 文件并运行 ssh 命令仅要求输入一次密码?

[英]How can I scp a file and run an ssh command asking for password only once?

Here's the context of the question:这是问题的上下文:

In order for me to be able to print documents at work, I have to copy the file over to a different computer and then print from that computer.为了让我能够在工作中打印文档,我必须将文件复制到另一台计算机,然后从该计算机打印。 (Don't ask. It's complicated and there is not another viable solution.) Both of the computers are Linux and I work in bash. (别问。这很复杂,没有其他可行的解决方案。)两台电脑都是 Linux,我在 bash 工作。 The way I currently do this is I scp the file over to the print computer and then ssh in and print from command line.我目前这样做的方法是将文件scp到打印计算机,然后ssh输入并从命令行打印。

Here's what I would like to do:这是我想做的事情:

In order to make my life a bit easier, I'd like to combine these two step into one.为了让我的生活更轻松,我想将这两个步骤合二为一。 I could easily write a function that did both these steps, but I would have to provide my password twice.我可以轻松编写完成这两个步骤的 function,但我必须提供两次密码。 Is there any way to combine the steps so that I only provide my password once?有什么方法可以组合这些步骤,以便我只提供一次密码?

Before somebody suggests it, key-based ssh-logins are not an option.在有人建议之前,基于密钥的 ssh 登录不是一种选择。 It has been specifically disabled by the Administrators for security reasons.出于安全原因,管理员已专门禁用它。

Solution:解决方案:

What I ended up doing was a modification of the second solution Wrikken provided.我最终做的是对 Wrikken 提供的第二个解决方案的修改。 Simply wrapping up his first suggestion in a function would have gotten the job done, but I liked the idea of being able to print multiple documents without having to type my password once per document.只需将他的第一个建议包含在 function 中就可以完成工作,但我喜欢能够打印多个文档而无需为每个文档输入一次密码的想法。 I have a rather long password and I'm a lazy typist:)我有一个相当长的密码,而且我是一个懒惰的打字员:)

So, what I did was take a sequence of commands and wrap them up in a python script.所以,我所做的是获取一系列命令并将它们包装在 python 脚本中。 I used python because I wanted to parameterize the script, and I find it easiest to do in python.我使用 python 因为我想参数化脚本,我发现在 python 中最容易做到。 I cheated and just ran bash commands from python through os.system.我作弊,只是通过 os.system 从 python 运行 bash 命令。 Python just handled parameterization and flow control. Python 刚刚处理了参数化和流量控制。 The logic was as follows:逻辑如下:

if socket does not exist:
    run bash command to create socket with timeout
copy file using the created socket
ssh command to print using socket

In addition to using a timeout, I also put have an option in my python script to manually close the socket should I wish to do so.除了使用超时,我还在我的 python 脚本中添加了一个选项,如果我愿意的话,可以手动关闭套接字。

If anyone wants the code, just let me know and I'll either paste-bin it or put it on my git repo.如果有人想要代码,请告诉我,我会将它粘贴到我的 git 存储库中。

ssh user@host 'cat - > /tmp/file.ext; do_something_with /tmp/file.ext;rm /tmp/file.ext' < file.ext 

Another option would be to just leave an ssh tunnel open:另一种选择是只打开 ssh 隧道:

In ~/.ssh/config:在 ~/.ssh/config 中:

Host *
        ControlMaster auto
        ControlPath ~/.ssh/sockets/ssh-socket-%r-%h-%p

. .

$ ssh -f -N -l user host
(socket is now open)

Subsequent ssh/scp requests will reuse the already existing tunnel.随后的 ssh/scp 请求将重用已经存在的隧道。

Here is bash script template, which follows @Wrikken's second method , but can be used as is - no need to edit user's SSH config file:这是 bash 脚本模板,它遵循@Wrikken 的第二种方法,但可以按原样使用 - 无需编辑用户的 SSH 配置文件:

#!/bin/bash

TARGET_ADDRESS=$1 # the first script argument
HOST_PATH=$2 # the second script argument
TARGET_USER=root
TMP_DIR=$(mktemp -d)
SSH_CFG=$TMP_DIR/ssh-cfg
SSH_SOCKET=$TMP_DIR/ssh-socket
TARGET_PATH=/tmp/file

# Create a temporary SSH config file:
cat > "$SSH_CFG" <<ENDCFG
Host *
        ControlMaster auto
        ControlPath $SSH_SOCKET
ENDCFG

# Open a SSH tunnel:
ssh -F "$SSH_CFG" -f -N -l $TARGET_USER $TARGET_ADDRESS

# Upload the file:
scp -F "$SSH_CFG" "$HOST_PATH" $TARGET_USER@$TARGET_ADDRESS:"$TARGET_PATH"

# Run SSH commands:
ssh -F "$SSH_CFG" $TARGET_USER@$TARGET_ADDRESS -T <<ENDSSH
# Do something with $TARGET_PATH here
ENDSSH

# Close the SSH tunnel:
ssh -F "$SSH_CFG" -S "$SSH_SOCKET" -O exit "$TARGET_ADDRESS"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM