没有密码的Unix SSH [英] Unix SSH without password

查看:47
本文介绍了没有密码的Unix SSH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我对Unix完全陌生,我需要编写一个"shell脚本"(?)以连接到另一个终端并运行一些SQL查询.我到底该怎么做?我一直在浏览本板和其他板上的一些答案,如果找到答案,我听不懂.

Hey all I'm completely new to Unix and I need to write up a "shell script" (?) to connect to another terminal and run a few SQL queries. How on earth do I do this? I've been browsing a few answers from this and other boards and if I have found my answer I don't understand it.

我能够手动连接,输入密码等,但是我需要使该过程自动化.我无权访问Perl(正如一些答案所建议),并且我无法编辑etc/shadow文件.因此,我认为这必须严格通过Unix本身来完成.这是我目前正在使用的:

I am able to manually connect, enter password, etc, but I need to automate the process. I don't have access to Perl (as a few answers have suggested) and I am unable to edit the etc/shadow file. So I assume this has to be done strictly through Unix itself. This is what I am currently using:

X=`vUser='USER-NAME'
vPass='PASSWORD'
vTable='TABLENAME'

vHOST='HOST-NAME'

vPORT=4443

ssh root@vHost
expect {
    "root@the-host password:"{
        send -s "'vPass'\r"
    }
}

SQL_Query='select * from vTable limit 10'

mysql -p$vPASS -D$vTable -u$vUser P$vPort<<EOF 

$SQL_Query
EOF`
echo $X>Output.dat

请完整解释所有答案.我正在尝试学习.

Please explain all answers in full. I'm trying to learn.

推荐答案

尝试一下:

  1. 将SSH 公用密钥复制到剪贴板(cat ~/.ssh/id_rsa.pub的输出).如果您没有SSH密钥对,请使用本教程生成它
  2. 将您的公共密钥粘贴到服务器的~/.ssh/authorized_keys文件中.如果没有,请使用nano ~/.ssh/authorized_keys创建它并将其粘贴在其中.
  3. 在计算机上,可以使用以下命令在服务器中运行脚本:

  1. Copy your SSH public key to your clipboard (output of cat ~/.ssh/id_rsa.pub). If you don't have an SSH key pair then generate it with this tutorial.
  2. Paste your public key to your server's ~/.ssh/authorized_keys file. If it doesn't have one, create it with nano ~/.ssh/authorized_keys and paste it there.
  3. In your computer, you can run the script in the server with the following command:

ssh user@server_ip 'bash -s' < local_script.sh

或者,如果您要运行单个命令,则可以这样做:

Or if you have a single command to run then this will do:

ssh user@server_ip "echo Test | tee output.log"

  • 如果您不喜欢SSH一直要求您输入密码,请使用ssh-agent

    对于特定于SQL的脚本,您可以将所有SQL命令放在一个文件中,例如query.sql.您应该将query.sql复制到服务器(scp query.sql user@server_ip:~/),然后运行

    For SQL-specific scripts, you can put all your SQL commands in a single file, say query.sql. You should copy query.sql to your server (scp query.sql user@server_ip:~/) and then run

    ssh user@server_ip "mysql -uyourusername -pyourpassword < query.sql | tee output.log"
    

    输出将保存在output.log中.还要检查此答案

    The output will be saved in output.log. Check this answer too.

    这篇关于没有密码的Unix SSH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆