ssh 到 unix 服务器的脚本

发布于 2024-11-09 08:57:40 字数 120 浏览 4 评论 0原文

如果有人能告诉我如何使用用户名和密码作为参数连接到 unix 服务器,那将会很有帮助。我的用户名和密码是“anitha”。

我如何创建一个 shell 脚本,使用此用户名和密码自动连接到我的 unix 服务器?

It will be helpful if somebody could tell me how to connect to a unix server using username and password as arguments.my username and password is "anitha".

How can i create a shell script which automatically connect to my unix server with this username and password?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人事已非 2024-11-16 08:57:40

我猜您想从网络远程连接到您的 *nix 服务器。根据我的猜测,要:

  • 连接到远程 *nix 服务器,每个人都在使用 SSH

    ssh anitha@anitha ip-to-unix-server

  • 自动连接,编写简单的 bash shell 包裹您的 ssh 连接命令并做一些不建议的事情,你应该使用 ssh 无密码登录(又名公钥/私钥)

    #!/usr/bin/env bash
    ip=172.16.0.1 #用你的unix服务器的ip替换172.16.0.1
    username=anitha #你的 ssh 用户名
    密码=anitha #你的ssh密码
    command=who #你想对远程服务器做什么
    参数= #你的命令的参数
    期望 -c 'spawn ssh $username@$ip ;期待密码;发送“$密码\n”;交互'
  • 无需输入即可连接密码,您可能需要使用 SSH 无密码登录

I guess you want to remotely connect to your *nix server from network. Base on my guess, to:

  • connect to remote *nix server, everybody is using SSH

    ssh anitha@anitha ip-to-unix-server

  • automatically connect, write simple bash shell wrap around your ssh connect command and do something, not suggested, you should use ssh password less login (aka public/private key)

    #!/usr/bin/env bash
    ip=172.16.0.1   #replace 172.16.0.1 with your unix server's ip
    username=anitha #your ssh username
    password=anitha #your ssh password
    command=who     #what do you want to do with remote server
    arguments=      #arguments for your command
    expect -c 'spawn ssh $username@$ip ; expect password ; send "$password\n" ; interact'
  • connect without typing password, you may need to use SSH password less login

黒涩兲箜 2024-11-16 08:57:40

如果您确实需要使用非交互式键盘交互式身份验证,请使用 sshpass (双关语)或更好地切换到使用基于公钥的身份验证。

请注意,将密码以明文形式传递给 ssh 客户端是非常蹩脚的,因为密码会暴露在公共可读的进程列表中,任何人都可以读取它。 sshpass 通过创建伪终端并使用它与 ssh 客户端通信来解决此问题,因此至少密码在运行时不会暴露。

Use sshpass if you really need to use non-interactive keyboard-interactive authentication (pun intended) or better switch to using pubkey-based authentication.

Note that passing the password in clear to the ssh client is very lame as the password gets exposed in the publicly-readable process list where it can be read by anyone. sshpass works around this problem by creating a pseudo-terminal and communicating with the ssh client using it, so at least the password is not exposed at runtime.

鹿港小镇 2024-11-16 08:57:40

第 1 步:

jsmith@local-host$ [Note: You are on local-host here]

jsmith@local-host$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Pess enter key]
Your identification has been saved in /home/jsmith/.ssh/id_rsa.
Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub.
The key fingerprint is:
33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jsmith@local-host

第 2 步:
从本地主机运行此 Oneliner 以实现无密码 ssh 连接。

cat ~/.ssh/id_dsa.pub | ssh useronanotherserver@anotherservername  'cat >> ~/.ssh/authorized_keys'

Step 1:

jsmith@local-host$ [Note: You are on local-host here]

jsmith@local-host$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Pess enter key]
Your identification has been saved in /home/jsmith/.ssh/id_rsa.
Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub.
The key fingerprint is:
33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jsmith@local-host

Step 2:
From Local-host, run this One liner for password less ssh connectivity.

cat ~/.ssh/id_dsa.pub | ssh useronanotherserver@anotherservername  'cat >> ~/.ssh/authorized_keys'
留一抹残留的笑 2024-11-16 08:57:40

您应该使用 expect,它是 tcl 的扩展,专门用于自动执行登录任务。

You should use expect, which is an extension of tcl that was made specifically for automating login tasks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文