Executing multi level shell script and get response from remote machine - linux

I have two shell scripts like
mem.sh
RESULT=$(awk {print} /proc/meminfo)
r="$(./test.sh)"
RESULT="$RESULT$r"
echo ${RESULT#* }
exit ${RESULT%% *}
test.sh
echo "HI"
When I execute mem.sh locally it will print memory info followed by HI without any error, but I am in need of executing that script in remote machine, I am doing it like below
cat mem.sh | ssh user#ip >> result.txt
I am getting error like
unexpected EOF while looking for matching `"'
syntax error: unexpected end of file
I could't find solution, Please help me on this or Is there any way to do this?
Question #2: Is there any way to execute multiple shell scripts(available in local machine) in remote machine?
I am using linux machine.

Related

Remote command execution here docs & assigning variables

The format, syntax, I have obtained from another post [here][1]
Collecting the output of a remote ssh command in a variable:
I am trying to obtain a list of folder contents and compare them with a list of specific processes. I need this to know if all required instances of let's say IHS/WAS are running as expected. I cannot use specific IHS or WAS commands because I do not have access to the commands. I have limited read access to the systems and I am writing a script to obtain list of instances installed, running, etc.
Below is my code:
#!/bin/bash
HOST='xyzhostname'
$vari=$(ssh -T $HOST <<'EOF'
printf "getting folders: \n"
instances=$(ls /samplefolder/samplefolder/)
printf "got folders.\n"
printf "${instances}"
EOF
)
more code to get processes (ps -ef.....) and compare against each folder obtained in instances above will follow...
I get the below error when I run this code
./test.sh: line 9: =getting: command not found
Would appreciate any help on this..

List command not displaying all output

Trying to connect to multiple machines using bash on a machine that has the public ssh keys for the others and run a command on them to display the output on this machine. If I use the '$a' variable as in the code below when I execute the script I get this uncompleted output
bash: total: command not found
bash: line 1: drwxr-xr-x: command not found
bash: line 2: drwxr-xr-x: command not found
bash: line 3: -rw-r--r--: command not found
bash: line 5: -rwxr-xr-x: command not found
....
However if I use the commented block without calling the $a variable the bash prints the correct output of the command.
The code executed:
#!/bin/bash
a=$(ls -lah)
for i in "machine1" "machine2"
do ssh root\#"$i" "$a; exit;"
*#do ssh root\#"$i" "ls -lah; exit;" - displays accordingly*
done
Your code does not work because a=$(ls -lah) assigns ls -lah's output to variable a. Change it to a='ls -lah' and it will be ok.
See command substitution section in your bash's manual
Hi I have another answer which i have been using for a long time.
declare machines=("user#machine1" "user#machine2")
command="ls -lah"
for machine in "${machines[#]}"
do
ssh "$machine" "$command"
done
$a did not work because it will be executed.

Errant error output behavior with perl execute bash scripts on a remote machine

I have this line in my perl script that sshes into a remote machine and execute a bash script:
system("ssh -t remotemachine /dir/dir/bashscript");
In my bash script, I used exit code 2 some commands 2 >> error.txt to capture any errors that I may encounter and I want this error.txt to be written in the same folder where the bash script is stored.
My problem is when I ssh into the machine and run the program from the terminal, the error can be captured and written in error.txt but if I run the program from my perl, the program is able to run but the error is not captured.
Any suggestions?
Use the full path for the capture file.
some commands 2 >> /dir/dir/error.txt
Otherwise the file will be created in the users $HOME if it exists.

executing parameter file in a unix remote server

I am trying to run a script remotely using ssh and the need use some parameters from remote server. Kept all parameters in remote server location temp/test/test.prm file. Getting an error saying " invoke.sh: line 20: . /temp/test/test.prm: No such file or directory "
See below for sample script. Have very basic knowledge in scripting so plz direct me
#!/bin/sh
Param1=$1
Param2=$2
ssh usr#Server1
. ${Param1}/Client/scripts/Sample1.prm
cd $prmHome/$prmSetPath
ls | sed '/\.log$/d' > $prmHome/$prmScript/Filelist.txt
cd $prmHome/$prmScript
while read LINE
do
ExportFilName=$LINE
./conversion.sh $prmHome/tmp_export/convert_$Param2.csv $prmHome/$prmSetPath/'$ExportFilName'
done < Filelist.txt
rm -rf $prmHome/$prmScript/Filelist.txt
exit 0
Content of Sample1.prm
prmHome=/iis/home
prmSetPath=/export/set
PrmScript=/Client/scripts
I have tried the same trough command line after connecting to remote server using ssh and it is working, but when I am trying to do the same through a script (invoke.sh) its throwing no such file or directory error
UPDATE
This is unclear and will not work !
ssh usr#Server1
. ${Param1}/Client/scripts/Sample1.prm
As mentioned you should use
ssh usr#Server1 ". ${Param1}/Client/scripts/Sample1.prm"
format first.
And secondly what you expect following command to do ?
. ${Param1}/Client/scripts/Sample1.prm
Notice that there is a space in between . and the path, which is a synonym for source. So also check if the Sample1.prm have valid commands.
Looks like you are not running any ssh shell command on remote host but only on your local host.
How exactly you are running the ssh shell command ?
The code snippet in your example is poorly formatted.
The general structure should look like this e.g.
ssh user1#server1 date
or
ssh user1#server1 'df -H'
Please revise you invocation script, or make the formatting in the question appropriate.
Update:
If you want to execute your code on remote server via ssh, you can do following:
Create separate file my_script.sh for your code you want to run remotely, and paste following code:
#!/bin/bash
function my_function() {
prmHome=$1
prmSetPath=$2
PrmScript=$3
cd $prmHome/$prmSetPath
ls | sed '/\.log$/d' > $prmHome/$prmScript/Filelist.txt
cd $prmHome/$prmScript
while read LINE
do
ExportFilName=$LINE
./conversion.sh $prmHome/tmp_export/convert_$Param2.csv $prmHome/$prmSetPath/'$ExportFilName'
done < Filelist.txt
rm -rf $prmHome/$prmScript/Filelist.txt
exit 0
}
Then you can call you function remotely, by sourcing your file on remote server:
ssh usr#Server1 '. my_script.sh; my_function "/iis/home" "/export/set" "/Client/scripts"'
That's it :)
This code will not work:
ssh usr#Server1
. ${Param1}/Client/scripts/Sample1.prm
Change it like that:
ssh usr#Server1 ". ${Param1}/Client/scripts/Sample1.prm"

shell script can't see files in remote directory

I'm trying to write an interactive script on a remote server, whose default shell is zsh. I've been trying two different approaches to get this to work:
Approach 1: ssh -t <user>#<host> "$(<serverStatusReport.sh)"
Approach 2: ssh <user>#<host> "bash -s" < serverStatusReport.sh
I've been using approach 1 just fine up until now, when I ran into the following issue - I have a block of code that runs depending on whether certain files exist in the current directory:
filename="./service_log.*"
if ls $filename 1> /dev/null 2>&1 ; then
echo "$filename found."
##process files
else
echo "$filename not found."
fi
If I ssh into the server and run the command directly, I see "$filename found."
If I run the block of code above using Approach 1, I see "$filename not found".
If I copy this block into a new script (lets call this script2), and run it using Approach 2, then I see "$filename found".
I can't for the life of me figure out where this discrepancy is coming from. I thought that the difference may be that script2 is piped into bash whereas my original script is being run with zsh... but considering that running the same command verbatim on the server, with its default zsh shell, returns correctly... I'm stumped.
:( any help would be greatly appreciated!
I guess that when executing your approach 1 it is the local shell that expands "$(<serverStatusReport.sh)", not the remote. You can easily check this with:
ssh -t <user>#<host> "$(<hostname)"
Is the serverStatusReport.sh script also in the PATH on the local host?
What I do not understand is why you get this message instead of an error message.

Resources