How to ssh into remote linux server using username / ip address and password from window using shell script? - bash

What I am trying to do is to login to remote Linux server using SSH from my Windows machine by running a shell script via git bash.
I would like to write a script which will be used by an user with basic IT knowledge. This script will execute a bunch of commands on the remote machine, so it does need to establish a SSH connection.
What I have tried to write in this script so far is:
ssh username#ip <password>

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

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

How to run a script from local on remote but at some point continue running the script on the local server?

I need to run a bash script that takes some parameters from server-1 and then from my local server where I ran the script with
ssh user#server-1 bash -s <script.sh
I then need to use those parameters to be executed with all kind of commands on my local server and also server-2 is involved. But the script will still be running on server-1 because of
ssh user#server-1 bash -s <script.sh
Maybe I can use 2 scripts but I want them to be only on local server. and putting in the script more commands after SSH doesn't seem to be working.
I would place the script on the remote server and remote execute it via SSH.
If the script should change over time, then break it up into 2-3 steps
1. gather any additional parameter from remote machine
2. copy script to remote machine using scp
3. ssh to "remote execute" script on remote machine
Am not sure what parameter you need from the remote system.
I would try to hand it over via command line options to the script in #3.
Otherwise "hack"/patch it in before #2.

Is there a way to run one bash file which executes commands in local and server terminal also

I am running the bash files to make a Mongo dump on daily bases.But In local directory I am running a one bash file which connects to server terminal.And in server terminal I am running the other file which makes a Mongo dump.
But is it possible to make one file which connects to MongoDB server terminal and run the commands on the sever.
I tried with many commands but it was not possible to run the commands on the server terminal with one bash file, when the server terminal opens up then the left over commands does not execute.
Is it possible to do one bash file and execute the server commands on the server..?
Connect to your DB remotely using this command :
mongo --username username --password secretstuff --host YOURSERVERIP --port 28015
You can then automate this by including your pertaining commands ( including the above ) in a bash script that you can run from anywhere.
To solve the above problem, answer from Matias Barrios seems to be correct for me. You don't use a script on the server, but use tools on your local machine that connect to the server services and manage them.
Nevertheless, to execute a script on a distant server, you could use ssh. This is not the right solution in your case, but answer the question in your title.
ssh myuser#MongoServer ./script.sh param1
This can be used in a local script and execute script.sh on the server MongoServer (with param1 and) with system privileges of the user myuser.
Beforehand, don't forget to avoid password request with
ssh-copy-id myuser#MongoServer
This will copy your ssh public key in the myuser directory of the MongoServer

How to return to the script once sudo is done

I am on a server and running a script file that has following code.
ssh username#servername
sudo su - root
cd /abc/xyz
mkdir asdfg
I am able to ssh... but then the next command is not working.. the script is not sudo-ing. any idea?
Edit: Able to create a mech id and then do the things.. though still looking for the answer to above question :|
First of all your command will "stuck" on the first line because it will go into an interactive mode. The ssh command will require a password to be provided by a user (unless there is an sshkey being used) . And if the ssh is logged into the remote server then it will wait for user commands from standard input.
Secondly the lines following the ssh command will be executed only when the first process has exited. This is why your script is not "sudoing" - it's waiting for the ssh to end.
So if your point is to run a command on a remote server then put the command as a parameter into the same line as ssh connection. In your case:
ssh user#server sudo su - root
But this will not be of satisfaction for you. I suggest you create a script of what you want to execute on the remote server and then execute the script.
ssh user#server scriptName
The sudo thing here is very tricky because again your script might get stuck in the interactive mode waiting for a password to be inserted so I suggest you think again on the basis of the script.
mb47!
You want to run the script on the remote computer, correct?
On the remote machine, create a file containing the commands you would like to execute.
Then, on the other machine, run ssh user#machine /path/to/script/you/created/earlier
I hope this helps!
ALinuxLover

Get the local user during ssh session

Is it possible to display the local user on remote host?
eg.:
local_user#ownpc$ ssh remote_user#server
then
echo $some_variable_containing_local_user
that results local_user
Purpose: I have a very limited read access to a UNIX server, i'd like to perform scp from remote to local over the current ssh session. Performed via a shell script, regardless of who logged in. (without any additional installs or local variable modifications)
Ps.: I am not looking for scp ssh:user#host/folder/file /home/user/folder
Thank you!
Using an unquoted here-doc allows local variables to be expanded before passing the commands to ssh
local_user#ownpc$ ssh remote_user#server <<END
echo you are $USER
END
should output
you are local_user
That may give you a basis to script your remote actions in the here-doc.
I'm not 100% clear on your situation but both of these methods work for me:
ssh remote_user#remote_host whoami
and
ssh remote_user#remote_host 'echo $USER'
The first executes "whoami" on the remote host while the second echos out what $USER is set to in the remote shell. I'm a little surprised the second one works because often ssh remote executing does not start a shell but it appears to work anyway.

Shell Script program to download files from linux remote server

I am very new in shell scripting , i want to download some files from linux remote server ,so how can i proceed for that.That remote server is ssh based .
first of all, ftp service is better choice to get files from remote server.
If only sshd service is available, then you may use ssh based command sftp or scp.
However, using sftp or scp commands will invoke an interactive password prompt, which is a problem in shell script --> You have to ask for help to expect command. see Automate scp file transfer using a shell script .
Besides expect, you may also set up trust relationship between two servers, then you may use scp without password. See http://www.linuxproblem.org/art_9.html

Resources