无密码的 Unix SSH [英] Unix SSH without password

查看:21
本文介绍了无密码的 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'
"
    }
}

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 public 密钥复制到剪贴板(cat ~/.ssh/id_rsa.pub 的输出).如果您没有 SSH 密钥对,请使用 本教程 生成它.
  2. 将您的 public 密钥粘贴到服务器的 ~/.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天全站免登陆