简体   繁体   English

如何将变量传递给 shell 上的 ssh 命令

[英]How to pass the variable to ssh command on shell

I am writing the script to ssh the server.我正在将脚本写入 ssh 服务器。

I have the server which require the verification code and password login, and I want the script can login the server and run some script on server every day (crontab).我有需要验证码和密码登录的服务器,我希望脚本可以每天登录服务器并在服务器上运行一些脚本(crontab)。

I only have the access on the server and I cannot the right to setup the crontab on the server.我只有服务器上的访问权限,我无权在服务器上设置 crontab。 So i want to setup the crontab and script on my own server to do.所以我想在我自己的服务器上设置 crontab 和脚本来做。

(If i cannot run the script on server, at least I want to login my server via one line command.) (如果我不能在服务器上运行脚本,至少我想通过一行命令登录我的服务器。)

Currnet Flow当前流量

user ~% ssh xxxx@example.com

The authenticity of host 'example.com' can't be established.
RSA key fingerprint is SHA256?[yyyyyyyyyyyyyyyyyyy].
Are you sure you want to continue connecting (yes/no/[fingerprint])? (yes)
Verification code:  (input)
Password: (input)

xxxx$ ./a.sh

I write the command below我在下面写命令

print "(verification code)\n(pwd)\n" | ssh -t -t xxxx@example.com

I think that this command will pass " the code , enter, the password " to the ssh and perform the one line command login.我认为该命令会将“代码,输入,密码”传递给 ssh 并执行一行命令登录。

However the output is然而 output 是

Verification code: | (cursor)

Any one can help me how to fix this (let the script auto press enter)??任何人都可以帮我解决这个问题(让脚本自动按回车键)??

Use 'sshpass', for example:使用“sshpass”,例如:

sshpass -p <password> ssh <username>@<remote-address> ./a.sh

First, I don't have an answer if "verification code and password login" is the only authentication option setup in the server.首先,如果“验证码和密码登录”是服务器中唯一的身份验证选项设置,我没有答案。

Since you have not mention key based authentication in your question, I would suggest investigating whether it is an option.由于您在问题中没有提到基于密钥的身份验证,我建议您调查它是否是一个选项。

Use this to generate private/public keys for the machine sending the command:使用它为发送命令的机器生成私钥/公钥:

ssh-keygen -t rsa

That would normally create the keys in ~/.ssh (id_rsa and id_rsa.pub).这通常会在 ~/.ssh(id_rsa 和 id_rsa.pub)中创建密钥。

Then, use this to authorise login on the server:然后,使用它来授权服务器上的登录:

ssh-copy-id xxxx@example.com

That would add your public key (~/.ssh/id_rsa.pub) to ~/.ssh/authorized_keys in the server.这会将您的公钥 (~/.ssh/id_rsa.pub) 添加到服务器中的 ~/.ssh/authorized_keys 中。

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

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