Execute shell script in remote machine using ssh command with config file - linux

I want to execute a shell script in remote machine and i achieved this using the below command,
ssh user#remote_machine "bash -s" < /usr/test.sh
The shell script executed properly in the remote machine. Now i have made some changes in script to get some values from the config file. The script contains the below lines,
#!bin/bash
source /usr/property.config
echo "testName"
property.config :
testName=xxx
testPwd=yyy
Now if i run the shell script in remote machine, i am getting no such file error since /usr/property.config will not be available in remote machine.
How to pass the config file along with the shell script to be executed in remote machine ?

Only way you can reference to your config file that you created and still run your script is you need to put the config file at the required path there are two ways to do it.
If config is almost always fixed and you need not to change it, make the config locally on the host machine where you need to run the script then put the absolute path to the config file in your script and make sure the user running the script has permission to access it.
If need to ship your config file every time you want to run that script, then may just simply scp the file before you send and call the script.
scp property.config user#remote_machine:/usr/property.config
ssh user#remote_machine "bash -s" < /usr/test.sh
Edit
as per request if you want to forcefully do it in a single line this is how it can be done:
property.config
testName=xxx
testPwd=yyy
test.sh
#!bin/bash
#do not use this line source /usr/property.config
echo "$testName"
Now you can run your command as John has suggested:
ssh user#remote_machine "bash -s" < <(cat /usr/property.config /usr/test.sh)

Try this:
ssh user#remote_machine "bash -s" < <(cat /usr/property.config /usr/test.sh)
Then your script should not source the config internally.
Second option, if all you need to pass are environment variables:
There are a few techniques described here: https://superuser.com/questions/48783/how-can-i-pass-an-environment-variable-through-an-ssh-command
My favorite one is perhaps the simplest:
ssh user#remote_machine VAR1=val1 VAR2=val2 bash -s < /usr/test.sh
This of course means you'll need to build up the environment variable assignments from your local config file, but hopefully that's straightforward.

Related

SSH run commands from local file and also pass local env variables

I need to run SSH on Linux and execute commands from a local file into the remote machine. This is working fine, but I also need to pass local environment variables to the remote machine, so the commands can use the values.
Here is the command I'm running:
ssh -i ${SSH_PRIV_KEY} ${SSH_USER}#${IP} < setup.sh
I have a bunch of environment variables set and when the remote machine runs the commands in setup.sh file it needs be able to use the env vars from the local machine.
I tried many things, this but solutions from other threads like this don't work correctly:
myVar='4.0.23'
export $myVar
ssh -i ${SSH_PRIV_KEY} ${SSH_USER}#${IP} myVar=myVar < setup.sh
Only thing I can come up with is to append the start of the file and hardcode the values there before executing ssh, but if possible I would like to find a cleaner solution because I want this to be reusable and the only thing that changes for me between runs is the env vars.
I ended up using this code to get the env vars I need to be stored in a file, then combine the files into one and pass that to the ssh as command:
envvars="
envvar='$envvar'
envvar2='$envvar2'
"
echo $envvars > envfile
cat envfile setup.sh > finalScript
cat $()
ssh -i ${SSH_PRIV_KEY} ${SSH_USER}#${IP} < finalScript

How to run shell script located on Linux server from Windows environment?

I am trying to run a shell script located on a Linux server from Windows. The shell script does two things:
Do a sed command to replace text in an .sql file in the same directory.
Run the .sql file with sqlplus.
The shell script:
!/bin/sh
arg1=$1
arg2=$2
arg3=$(echo $arg1 | tr '[:lower:]' '[:upper:]')
arg4=$(echo $arg2 | tr '[:lower:]' '[:upper:]')
echo $arg1
echo $arg2
echo $arg3
echo $arg4
sed -i "s/$arg3/$arg4/g" sequence.$arg1.sql
sqlplus $arg2/$arg2#MYDB <<EOF
#sequence.$arg1.sql
exit;
(My database is located on the same Linux server.)
1) Script runs correctly when I log in to the server via MobaXterm
Connect to server with userID.
Set my_env.
cd to the shell script's directory.
Run script with ./myscript.sh with arguments.
2) Same shell script runs successfully via .cmd manually
Create a Windows script test.cmd on my Windows PC.
In the .cmd file I have the line:
plink.exe -ssh userID#Server
After the console window pops up, I repeat the steps 2 to 4 and script runs successfully.
What I am failing to do so is to automate the whole process.
Here's the line in my .cmd file which I attempted:
plink.exe -ssh userID#Server /myfilepath/myscript.sh %arg1% %arg2%
I can see the arguments passed correctly using multiple echo in the shell script. However, the shell script fails to locate the .sql file.
Error log:
/mypath/myscript.sh[1]: !/bin/sh^M not found [No such file or directory]
myarg1value
myarg2value
:No such file or directory[myarg1value]
/mypath/myscript.sh[12]: sqlplus: not found [No such file or directory]
I also tried below, but unfortunately with same result:
plink.exe -ssh userID#Server -m command.txt
Where file command.txt contains:
. my_env
cd /filepath/
./myscript.sh %arg_with_actual_value%
I do not know why it is not working, especially when 2) works and the script is relatively simple.
Do I assume things incorrectly about plink (path, variable, etc.)?
Is Cygwin the only way out?
I tried not to rely on yet another tool as I have been using plink.
EDIT: While the line
sed -i "s/$arg3/$arg4/g" sequence.$arg1.sql
fails to run on the .sh, i can run it on the .cmd file itself via:
plink.exe -ssh userID#Server sed -i "s/%arg3%/%arg4%/g" /myfilepath/sequence.%arg1%.sql
Hence I am suspecting the problem comes from the .sh file not having the required components to run (i.e. set env variable, path, etc)
This is not a solution but partially fixed some issue, thanks to Martin Prikryl and Mofi's input:
in the command.txt, the following needs to be set:
ORACLE_SID
ORACLE_HOME
PATH
after these are set, the sqlplus and sed will work normally. However, passing values from .cmd through plink to Linux's shell script seems to have issue with the actual value being passed. The variable will have the assigned value along with some unreadable characters. In this case,
sqlplus $arg2/$arg2#MYDB
login fails because arg2 contains some other char.
#sequence.$arg1.sql
this line also fails as it will try to opens 2 files, one being called sequence.myvalue and another one called "%s", which i suspect the assigned variable contains some sort of unreadable nextline character.
EDIT: fixed, we can use the same treatment from sed - run sqlplus directly from plink instead of passing value and running a .sh script in Linux:
sqlplus $arg2/$arg2#MYDB #/myfilepath/sequence.%arg1%.sql

Using SSH to execute a command - $PATH is fine but running perl scripts fails?

Going from a Linux host to another Linux host
Say I run:
ssh user#server ' . /etc/profile; /path/to/myScript.pl'
I always get errors involving scripts within that perl script like...
/path/to/otherscript.sh was not found No Such File or Directory
-even though it's obviously there. Running this script locally on "server" works just fine. What's also confusing is that the output of....
ssh user#server ' . /etc/profile; echo $PATH'
...looks EXACTLY the same as echo $PATH when running on "server" locally.
Any ideas as to why this is not working? I do not have permissions to modify the perl script to always* include the complete path to the files listed.
if it's useful this is running with a shebang of #!/usr/bin/env perl - reading up on it now, would this alter my path?**

change directory command in my script is not being recognize when i run the script using plink

I'm running a batch file (export.bat) in Windows 7 using plink to execute a script in a remote Linux server machine, but I get this error:
./test.sh: line 3: back.sh: command not found
Batch file:
#echo off
cls
plink 1.1.10.11 -l user -pw pass "bash ./test.sh"
Script in the remote server:
#!/bin/sh
cd /path/path/path
script --table filename--filebase /path/path/path/path
exit
I'm assuming script in your remote script is actually back.sh, and that it exists in /path/path/path.
To execute a script back.sh in the directory you cd to (i.e. the current directory), use ./back.sh instead of just back.sh.
PS: You should generally try to avoid sanitizing input and output of things you post on StackOverflow. It often ends up confusing. For example, you say you get the error line 3: back.sh: command not found, but your remote script does not contain the command back.sh on line 3 or anywhere.
Instead, invest 15 minutes in making a runnable test case with no sensitive data, that you can actually execute and copy files and errors from verbatim. The bash tag wiki has tips for this.

shell script, for loop, ssh and alias

I'm trying to do something like this, I need to take backup from 4 blades, and
all should be stored under the /home/backup/esa location, which contains 4
directories with the name of the nodes (like sc-1, sc-2, pl-1, pl-2). Each
directory should contain respective node's backup information.
But I see that "from which node I execute the command, only that data is being
copied to all 4 directories". any idea why this happens? My script is like this:
for node in $(grep "^node" /cluster/etc/cluster.conf | awk '{print $4}');
do echo "Creating backup fornode ${node}";
ssh $node source /etc/profile.d/bkUp.sh;
asBackup -b /home/backup/esa/${node};
done
Your problem is this piece of the code:
ssh $node source /etc/profile.d/bkUp.sh;
asBackup -b /home/backup/esa/${node};
It does:
Create a remote shell on $node
Execute the command source /etc/profile.d/bkUp.sh in the remote shell
Close the remote shell and forget about anything done in that shell!!
Run asBackup on the local host.
This is not what you want. Change it to:
ssh "$node" "source /etc/profile.d/bkUp.sh; asBackup -b '/home/backup/esa/${node}'"
This does:
Create a remote shell on $node
Execute the command(s) source /etc/profile.d/bkUp.sh; asBackup -b '/home/backup/esa/${node}' on the remote host
Make sure that /home/backup/esa/${node} is a NFS mount (otherwise, the files will only be backed up in a directory on the remote host).
Note that /etc/profile is a very bad place for backup scripts (or their config). Consider moving the setup/config to /home/backup/esa which is (or should be) shared between all nodes of the cluster, so changing it in one place updates it everywhere at once.
Also note the usage of quotes: The single and double quotes make sure that spaces in the variable node won't cause unexpected problems. Sure, it's very unlikely that there will be spaces in "$node" but if there are, the error message will mislead you.
So always quote properly.
The formatting of your question is a bit confusing, but it looks as if you have a quoting problem. If you do
ssh $node source /etc/profile.d/bkUp.sh; esaBackup -b /home/backup/esa/${node}
then the command source is executed on $node. After the command finishes, the remote connection is closed and with it, the shell that contains the result of sourcing /etc/profile.d/bkUp.sh. Now esaBackup command is run on the local machine. It won't see anything that you keep in `bkUp.sh
What you need to do is put quotes around all the commands you want the remote shell to run -- something like
ssh $node "source /etc/profile.d/bkUp.sh; esaBackup -b /home/backup/esa/${node}"
That will make ssh run the full list of commands on the remote node.

Resources