I want to pass a value to the shell script via ssh:
I have two Linux machines: machine1 and machine2
I have two scripts on those machines: "script1" on machine1 and "script2" machine2.
I have done ssh setting so I can login to machine2 from machine1 without password
/opt/script1
#!/bin/sh
echo "enter your name"
read name
ssh root#machine2 "/opt/script2 $name"
/opt/script2
#!/bin/sh
echo "$name"
but no string is printing
Please let me know the procedure to do this.
can u try the scripts like this
script1.sh
echo "enter your name"
read name
ssh root#machine2 "/opt/script2.sh $name"
script2.sh
echo $1
Passing command-line parameter to shell script
$0 is the name of the command
$1 first parameter
$2 second parameter
$3 third parameter etc. etc
$# total number of parameters
$# all the parameters will be listed
If i entered my name as Rupert script1 would be doing this
ssh root#machine2 "/opt/script2 Rupert"
Script2 is looking for $name but this has not been set on machine2 so the script will print nothing.
You can edit script2 to the below
#!/bin/sh
name=$1
echo "$name"
You should change the script 1 as following:
#!/bin/sh
echo "enter your name"
read name
ssh root#machine2 `/opt/script2 $name`
Related
I have a simple Bash script that takes in inputs and prints a few lines out with that inputs
fortinetTest.sh
read -p "Enter SSC IP: $ip " ip && ip=${ip:-1.1.1.1}
printf "\n"
#check IP validation
if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "SSC IP: $ip"
printf "\n"
else
echo "Enter a valid SSC IP address. Ex. 1.1.1.1"
exit
fi
I tried to upload them into my server, then try to run it via curl
I am not sure why the input prompt never kick in when I use cURL/wget.
Am I missing anything?
With the curl ... | bash form, bash's stdin is reading the script, so stdin is not available for the read command.
Try using a Process Substitution to invoke the remote script like a local file:
bash <( curl -s ... )
Your issue can be simply be reproduced by run the script like below
$ cat test.sh | bash
Enter a valid SSC IP address. Ex. 1.1.1.1
This is because the bash you launch with a pipe is not getting a TTY, when you do a read -p it is read from stdin which is content of the test.sh in this case. So the issue is not with curl. The issue is not reading from the tty
So the fix is to make sure you ready it from tty
read < /dev/tty -p "Enter SSC IP: $ip " ip && ip=${ip:-1.1.1.1}
printf "\n"
#check IP validation
if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "SSC IP: $ip"
printf "\n"
else
echo "Enter a valid SSC IP address. Ex. 1.1.1.1"
exit
fi
Once you do that even curl will start working
vagrant#vagrant:/var/www/html$ curl -s localhost/test.sh | bash
Enter SSC IP: 2.2.2.2
SSC IP: 2.2.2.2
I personally prefer source <(curl -s localhost/test.sh) option. While it is similar to bash ..., the one significant difference is how processes handled.
bash will result in a new process being spun up, and that process will evoke commands from the script.
source on the other hand will use current process to evoke commands from the script.
In some cases that can play a key role. I admit that is not very often though.
To demonstrate do the following:
### Open Two Terminals
# In the first terminal run:
echo "sleep 5" > ./myTest.sh
bash ./myTest.sh
# Switch to the second terminal and run:
ps -efjh
## Repeat the same with _source_ command
# In the first terminal run:
source ./myTest.sh
# Switch to the second terminal and run:
ps -efjh
Results should look similar to this:
Before execution:
Running bash (main + two subprocesses):
Running source (main + one subprocess):
UPDATE:
Difference in use variable usage by bash and source:
source command will use your current environment. Meaning that upon execution all changes and variable declarations, made by the script, will be available in your prompt.
bash on the other hand will be running in as a different process; therefore, all variables will be discarded when process exits.
I think everyone will agree that there are benefits and drawbacks to each method. You just have to decide which one is better for your use case.
## Test for variables declared by the script:
echo "test_var3='Some Other Value'" > ./myTest3.sh
bash ./myTest3.sh
echo $test_var3
source ./myTest3.sh
echo $test_var3
## Test for usability of current environment variables:
test_var="Some Value" # Setting a variable
echo "echo $test_var" > myTest2.sh # Creating a test script
chmod +x ./myTest2.sh # Adding execute permission
## Executing:
. myTest2.sh
bash ./myTest2.sh
source ./myTest2.sh
./myTest2.sh
## All of the above results should print the variable.
I hope this helps.
How do I make $1 and $2 variables to the remote shell through ssh. Below is the sample,
#!/bin/bash
user_name="${1}"
shift
user_password="${1}"
shift
tenant_name="${1}"
realscript="/IDM_ARTIFACTS/reset.sh"
ssh -qT oracle#slc05pzz.us.oracle.com bash -c "'echo $user_name'" < "$realscript"
I am able to echo $user_name but not able to access it in $realscript.
Cant call using HERE tags or single quotes'' as the script doesn't have straight forward commands.
What other options do I have? Please help
I do not have your script, so I put a test one on my remote host:
$ realscript=/home/jack/show_params.sh
$ second="second one"
$ ssh TEST cat ${realscript}
#!/bin/bash
nParams=$#
echo There are ${nParams} parameters.
for (( ii=1; ii<=${nParams}; ii++ )); do
echo "$1"
shift
done
$ ssh TEST 'bash '${realscript}' "first one" '\'${second}\'
There are 2 parameters.
first one
second one
The quoting gets a bit weird, but you can pass into parameters variables with spaces.
I am trying create a simple bash script that will add a record in the /etc/hosts file of my virtual machine. I've managed to do this successfully manually using:
echo sed -i '2i127.0.0.1 hostname.whatever.com hostname' /etc/hosts
But I would like to create a script that asks for the hostname and then creates it dynamically, for example:
#!/bin/bash
echo "Please enter hostname:"
read hostname
echo sed -i '2i127.0.0.1 $hostname hostname' /etc/hosts
This works fine but I would like to also be able to set the shortname "hostname" also. How would I take the first letters of the hostname variable before the .dot and also echo that in to the file correctly?
Parameter expansion:
read -p "Please enter hostname: " hostname
echo "${hostname%%.*}"
Read into array:
IFS=. read -p "Please enter hostname: " -a hostname
echo "${hostname[0]}"
Both of these solutions uses native bash without forking a new process. The first one is also POSIX compliant, so it will work with /bin/sh.
You can get the name before the dot with cut like:
$ hostname=hostname.whatever.com
$ shortname=`cut -d. -f1 <<< $hostname`
$ echo $shortname
hostname
Okay, so I am new to bash scripting, about 2 whole hours and I'm banging my head against a wall. I need to carry some variables from one script to another and can't seem to get it to work.
This works fine
script1.sh
echo "Enter your name"
read name
export name
./script2.sh
sript2.sh
echo $name
This does not
script1.sh
echo "Enter your name"
read name
export name
ssh $user#$domain "bash -s" < ./script2.sh
sript2.sh
echo $name
The code is dumb, I get that but basically what I want to do is pass database variables to script 2 to use on the server I've SSH'd into. I would do this all in one script but it seems the only way that I can get the server commands to run is to call a second script after the login, otherwise it runs the server commands locally and fails. I'm doing this from a Mac if that makes any difference at all.
Help?
** Update with more legitimate code ****
login.sh
#!/bin/bash
echo "Enter server username:"
read user
echo "Enter server domain"
read domain
echo "Enter your password"
read password
echo "Enter database name"
read database
export database
export user
export domain
export password
ssh $user#$domain "bash -s" < ./create_files.sh
create_files.sh
#!/bin/bash
cd public_html
tar -zcvf test.tar.gz test.html
echo "db:"$database
mysqldump -u $user -p"$password" $database > $database.sql
The goal here is to use the details from login.sh in create_files.sh after this is done I also need to use the login details to scp the created files back to my local machine. Hope this clarifies the problem.
When invoking ssh, it creates a new shell process which is not inheriting the new variables that you exported in the first script. Instead, try adding them as commandline parameters to the second script, like so:
#!/bin/bash
echo "Enter server username:"
read user
echo "Enter server domain"
read domain
echo "Enter your password"
read password
echo "Enter database name"
read database
ssh $user#$domain "bash -s" < ./create_files.sh "$user" "$password" "$database"
Then modify your second script to look like this:
#!/bin/bash
cd public_html
tar -zcvf test.tar.gz test.html
echo "db:$3"
mysqldump -u "$1" -p"$2" "$3" > "$3".sql
I am trying to run the shell script in the remote host , with this i also want to pass some arguments. my local host shell scripts is below
pwd
echo $0
echo $1
echo $2
echo thanku everyone!!
To run this script am using the command
ssh user#server 'ksh' < ./code
I want to pass arguments with this command. please clarify me in this..
Thanks in advance.
You can't pass parameters because you are starting ksh and using ./code as it's standard input.
But you can set an environment variable and then use it as command line, watch!
ichramm#wilderkrieger:~$ cat code
function run() {
echo $1;
echo $2;
}
run $COMMAND_LINE
ichramm#wilderkrieger:~$ ssh localhost 'COMMAND_LINE="hello world" ksh' < ./code
hello
world