简体   繁体   English

使用 ssh 制作应用程序以关闭 macbook

[英]Making an app using ssh to shutdown macbook

I have been trying to create an app that can shutdown my mac using ssh.我一直在尝试创建一个可以使用 ssh 关闭我的 mac 的应用程序。 I've tried ssh-copy-id -i ~/.ssh./id_rsa.pub ssh "my computer" | sudo shutdown -h now我试过ssh-copy-id -i ~/.ssh./id_rsa.pub ssh "my computer" | sudo shutdown -h now ssh-copy-id -i ~/.ssh./id_rsa.pub ssh "my computer" | sudo shutdown -h now but I get an error message saying zsh:exit 1 and zsh: suspended (tty output) sudo shutdown -h now and I don't know what to do. ssh-copy-id -i ~/.ssh./id_rsa.pub ssh "my computer" | sudo shutdown -h now但我收到一条错误消息说zsh:exit 1zsh: suspended (tty output) sudo shutdown -h now我不知道该怎么做。

There are several issues with your command:您的命令有几个问题:

ssh-copy-id -i ~/.ssh./id_rsa.pub ssh "my computer" | sudo shutdown -h now

  1. That's two commands without any separation.这是两个没有任何分隔的命令。 You need a semicolon or a line break between them.您需要在它们之间使用分号或换行符。
  2. ssh-copy-id needs the private key, but you've provided the public key ssh-copy-id需要私钥,但您提供了公钥
  3. That key looks like it has an extra dot in the key path该键看起来在键路径中有一个额外的点
  4. You're running sudo shutdown -h now locally rather than remotely您现在在本地而不是远程运行sudo shutdown -h now

Run this just one time.只运行一次。 It will prompt you for your user's password on mycomputer :它会提示您在mycomputer上输入您的用户密码:

ssh-copy-id -i ~/.ssh/id_rsa mycomputer

Then you can run this:然后你可以运行这个:

ssh -t mycomputer "sudo shutdown -h now"

This will be interactive since you need to enter your user password for sudo , but at least the key prevents you from needing to enter it a second time to gain access to the system.这将是交互式的,因为您需要输入sudo的用户密码,但至少该密钥可以防止您需要再次输入它来访问系统。

You could reduce this by allowing your user to run that command without a password.您可以通过允许您的用户在没有密码的情况下运行该命令来减少这种情况。 From a shell on that computer ( ssh mycomputer ), run visudo (if the last line of that file looks like @includedir /etc/sudoers.d , quit and run something like visudo -f /etc/sudoers.d/local ) and add this to the end:从该计算机上的 shell ( ssh mycomputer ) 运行visudo (如果该文件的最后一行看起来像@includedir /etc/sudoers.d ,退出并运行类似visudo -f /etc/sudoers.d/local的东西)将其添加到末尾:

gjwlsdnd224  ALL=(root) NOPASSWD: /sbin/shutdown -h now

where gjwlsdnd224 is your username.其中gjwlsdnd224是您的用户名。 If you do this, you don't need -t passed to ssh .如果你这样做,你不需要-t传递给ssh

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

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