0

I login to ssh and run command manually and I want to script that.

For now I do :

ssh 192.168.1.5 -p 1234 and then I see root@192.168.1.5's password: then I press enter (empty password) and then I want to run shell command like ps

How can I do that all in 1 script?

  • Hello! Welcome to AskUbuntu! does this answer your question? https://askubuntu.com/a/16053/1130604 – William Martens Nov 08 '21 at 21:38
  • In other words, you want to run a command on a remote machine. Should need root privileges? Do you need to get what this command send to stdout on the local machine? You have root privileges but can't change nothing on the server? – Pablo Bianchi Nov 08 '21 at 21:58

3 Answers3

1

Running a command over SSH is easy. Just put the command at the and of the SSH command. For example, to run echo Hello over SSH, with the user user and the host host, I could run this:

ssh user@host "echo Hello"
# and type the password (if there is one)
cocomac
  • 3,394
0

You can actually set up ssh with public key authentication. This won't require you to type in your password and is also considered more secure. This is a good guide to make this happen: https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server

You might not want to set a password for the ssh key, or you want to add it to your keyring so that you won't get another password prompt.

At this point, you can run ssh with no user input, since you can specify a command to run: ssh someone@somehost command. Or in your case: ssh root@192.168.1.5 -p 1234 ps

sfphoton
  • 31
  • 5
0

this explains something called "Authorized keys" and would be my "easy" way of doing it, as you say you wish for empty password (correct? if not, please - point that out!)

+ Related and as well as detailed link - also on AskUbuntu