Home > Back-end >  How to ssh into remote linux server using username / ip address and password from window using shell
How to ssh into remote linux server using username / ip address and password from window using shell

Time:12-17

What i am trying to do is to login to remote server using SSH from my windows machine by running a shell script via git bash.

So i have a bunch of commands which i want to run over the remote linux machine and the person using this script is somone who has basic knowledge. so i just want then to open git bash and run script1.sh and i should login to remote server and run the script and connect to the server.

so what i have tried is: ssh username@ip <password>

CodePudding user response:

EDIT: You should consider installing Jenkins on the remote system.

Running a command on a remote system can be done via:

ssh user@host command arg1 arg2

If you omit the password, a password prompt will appear, to get around the password prompt, you should consider setting passwordless SSH login. https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login

CodePudding user response:

Rephrasing what you said, you want to write a script (namely script1.sh) which does the following (in this order):

  • Start a ssh connection with a remote
  • Execute some commands, possibly written in another script (namely script2.sh)
  • Close the connection / Keep it open for additional command line commands

If you want to put the remote commands on script2.sh instead of listing them in script1.sh, you need to consider that script2.sh must be on the remote server. If not, then you may consider to delegate to script1.sh the creation/copy of script2.sh on the remote machine. You may copy it in a temporary folder.

In this case, my suggestion is to write script1.sh as follows:

  • Copy of script2.sh with

    scp /path/to/local/script2.sh user@host:/path/to/remote/script2.sh

  • Switch bash sheel to remote shell with

    ssh user@host

  • Execution of script2.sh

    sh /path/to/remote/script2.sh

Instead, if you prefer to list everything in just one script file, you may want to write:

  • Switch bash sheel to remote shell with

    ssh user@host

  • Execution of commands

    echo "This command has been executed on the remote server"

    echo "This command has also been executed on the remote server"

    ..

  • Possibly closing the SSH connection to prevent that the user execute additional commands

You may consider to copy the ssh-keys on the remote server so to avoid password prompts. More information here.

  • Related