简体   繁体   English

在Shell脚本中使用ssh,然后在密码后执行命令

[英]ssh in shell script, then execute commands after password

I'm trying to write a simple shell script to execute commands on my server box via ssh. 我正在尝试编写一个简单的Shell脚本,以通过ssh在服务器上执行命令。

I'm not trying to pass a password within the shell script, I'm just wondering how I can get it to run commands on that box after the password is entered. 我不是想在shell脚本中传递密码,而是想知道如何在输入密码后如何在该框中运行命令。

So far, when I execute my script, nothing happens after I enter the password. 到目前为止,执行脚本时,输入密码后没有任何反应。 Or, it executes when I kill the ssh process. 或者,它在我杀死ssh进程时执行。

I'm sure it's really easy, but I've been searching for hours and nothing has come up on the net, probably because nobody else needs to ask that. 我敢肯定这确实很容易,但是我一直在搜索几个小时,网上没有任何反应,可能是因为没有其他人需要问这个。

Is there some way to do this natively within the shell? 是否有某种方法可以在Shell中本地执行此操作? Thanks! 谢谢!

Look if you want to ssh without prompting password then you need a key authentication rather password authentication. 看看是否要在不提示输入密码的情况下进行ssh命令,则需要密钥验证而不是密码验证。 Else try; 否则尝试;

sudo apt-get install sshpass
sshpass -p your_password ssh user@hostname

And to execute command at remote; 并在远程执行命令;

ssh user@host 'command'

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

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