Score:0

Connect to ssh and run command in 1 script

fr flag

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 [email protected]'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?

William Martens avatar
cn flag
Hello! Welcome to AskUbuntu! does this answer your question? https://askubuntu.com/a/16053/1130604
vn flag
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?
Score:1
cn flag

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)
yfr24493AzzrggAcom avatar
fr flag
That not work because the ssh requires enter for empty password
William Martens avatar
cn flag
@yfr24493AzzrggAcom That may be the case, but it would be quite insecure to have something like `ssh someuser@somehost "somecommand" "somepassword"` the answer below me(by the time writing this, @sfphoton's answ) mentions the Public key- which I would really advice looking into.
yfr24493AzzrggAcom avatar
fr flag
I can't change the SSH server
William Martens avatar
cn flag
@yfr24493AzzrggAcom sorry, what do you specifically mean- change?
Score:0
us flag

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 [email protected] -p 1234 ps

yfr24493AzzrggAcom avatar
fr flag
That not work because the ssh requires enter for empty password. And I can't change the SSH server
sfphoton avatar
us flag
Why can you not change the ssh server? You only need to copy a file (your public key) on it
Score:0
cn flag

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.