Remote command execution here docs & assigning variables - linux

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..

Related

sending an output to another comand

I'm making a script that reads passwords from pass to ssh, I want to stop the script if the hostname is not found but I don't know how to read the output of ssh
I've tried this
test=$(ssh user#nonvalidip)
check="ssh: Could not resolve hostname nonvalidip: Name or service not known"
if [ $test = $check ]; then
echo "Please enter a valid ip"
fi
but $test is empty, how can I read the output of ssh and make that the test variable?
Assuming that you are trying to run a shell script to gain access through a system through SSH then if that connection is successful to run a command. To do this there are multiple things you could do that are much simpler than trying to make an interpret less language work. What I would strongly suggest is that to solve the first issue is to make a smaller script within the script. Such as doing something like this:
ssh user#known_address << EOF - This will start the session and keep everything running beneath it until it reaches the term EOF
Using this may help you later if you are in the Linux industry as well. The script should look something like this:
ssh user#known_address << EOF
scp /etc/passwd USER#your_address/Path
EOF - keep note that this is an example of what you can do but it is not very wise to keep extra copies of the password file laying around on other systems.
Then instead of using exact copies of what the system outputs you can simply use exit codes. This can be WAY more helpful along the way. You can receive the error codes of the last command you ran with echo $?
I cannot guarantee that this script will work but here's an example of what you can do
session() {ssh user#add << EOF; command1; command2; command3; EOF}; session; if $? == 1; echo "test failed".
Sources
https://www.shellhacks.com/ssh-execute-remote-command-script-linux/
Meaning of exit status 1 returned by linux command
https://www.shellscript.sh/functions.html

How have both local and remote variable inside an SSH command locally available

This is a variation of the question here: How have both local and remote variable inside an SSH command
How do I get the "ssh-variable" B to my local computer?
A=3
ssh host#name "echo $A; B=5; echo $A; echo \$B;"
echo $A
echo $B
echo \$B
returns 3, NOTHING, $B
How can I locally access a value for B?
some explanations based on comments:
I am using a bash script to access a remote host to modify some things there, amongst other things create a workspace there. The location of this workspace is what I want to store in a variable and save for later use.
Basically, I have a function to go to the remote host and make the workspace and then another function to use the path to that workspace to do other things there.
I was hoping for a lightweight, slim solution that can be integrated and easily read in a command similar to this:
ssh host#name "ws_allocate myworkspace; workspace=ws_find myworkspace;"
and then locally use $workspace. This is part of a larger bash script, that should be easy to understand for non-expert bash users (like myself)...
Turns out this is a duplicate of bash—Better way to store variable between runs?
You could do something like this:
X=$(ssh user#host 'echo $X')
This will run echo $X on the server and place the resulting output in a local variable X, which you can later use by saying $X
So to apply this to your example, you could say:
workspace=$(ssh host#name 'ws_allocate myworkspace; ws_find myworkspace;')
Note that the local variable workspace will contain the output from the command that is run remotely. So I'm assuming that in your example only the ws_find myworkspace will print any output and that the call to ws_allocate is silent.
Ok, so the solution that appears to me to be the easiest and that allows the value of $B to be stored on the remote host is to save it in a file. As described and discussed here already: bash—Better way to store variable between runs? :(
ssh ${supercomputer} "
ws_list -a >workspaces.txt;
if grep -q "${experiment}" workspaces.txt;
then echo 'we have a workspace already';
else ws_allocate ${experiment} 30;
fi;
ws_find ${experiment} > mypathtoworkspace;"
where supercomputer is the address to the host, experiment the workspace I want to create (or not, if it is there already) and workspace the workspace I want to create for my experiment.
I wanted to use the path to the remote workspace locally, e.g.
echo "${workspace}"
but this seems to not be possible - therefore I will download the file I am writing on the remote host and read the path from that file to use it locally
If anyone sees a better solution it is welcome!

Simple commands not found bash (#!/usr/bin/expect)

I've recently started using bash to automate a windows rescue disk with chntpw. I'm trying to set up the program to use the expect command to listen for certain chntpw dialog questions and input the right answers without any user input. For some reason after setting up the bash script to use #!/usr/bin/expect rather than #!/bin/bash then many standard terminal commands are no longer understood.
I'm running the script by typing this into terminal:
user#kali:~/Desktop/projects/breezee$ bash breezee1.sh
The terminal output is as follows:
BREEZEE 1.0
Welcome to BREEZEE
breezee1.sh: line 9: fdisk: command not found
[Select] /dev/:
Here is my code:
#!/usr/bin/expect
clear
echo "BREEZEE 1.0"
echo "Welcome to BREEZEE"
fdisk -l
#list partitions
echo -n "[Select] /dev/:"
#ask user to choose primary windows partition
read sda
clear
echo /dev/$sda selected
umount /dev/$sda
sudo ntfsfix /dev/$sda
sudo mount -t ntfs-3g -o remove_hiberfile /dev/$sda /mnt/
cd /mnt/Windows/System32/config
clear
chntpw -l SAM #list accounts on windows partition
chntpw -u Administrator SAM
#now supply chntpw with values to run the password clear (this answers the prompts)
expect '> '
send '1\r'
expect '> '
send '2\r'
expect '> '
send '3\r'
expect ': '
send 'y\r'
expect '> '
send 'q\r'
expect ': '
send 'y\r'
clear
echo "Operation Successfull!"
chntpw -l SAM #list accounts on windows partition
In short, I'm trying to use standard bash/terminal commands alongside the expect commands. I'm probably going about this all wrong, so please correct me as I've been troubleshooting this for about three days and haven't gotten far :(
When you specify the application that should run your script, you can only use the scripting language that application will understand.
Clearly, Expect is not bash, and does not understand bash commands.
i suggest you separate those two scripts. Write the first part for !#/bin/bash, the second for Expect. Make the first script invoke the second script and redirect it to chntpw.
expect uses tcl not bash. So you can write your script in TCL when you use #!/usr/bin/expect.
For example, echo "BREEZEE 1.0" should be written as:
puts "BREEZEE 1.0"
And you should use exp_send instead of send.
From expect manual:
exp_send is an alias for send. If you are using Expectk or some other variant of Expect in the Tk environment, send is defined by Tk for an entirely different purpose. exp_send is provided for compatibility between environments. Similar aliases are provided for other Expect's other send commands.

Executing multi level shell script and get response from remote machine

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.

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