I am taking input from user for a shell script, and want to run this script on different servers. I tried to pass the variables as follows:
USERNAME=****
HOSTS="**** ***** *****"
FOO=$1
BAR=$2
for HOSTNAME in ${HOSTS} ; do
ssh $USERNAME#$HOSTNAME bash << EOF
#script using FOO and BAR variables goes here
EOF
Login to the server is successful but variables are not being passed.
How can handle this situation without creating a temp file?
Do it like this:
VAR="something"
ssh $USERNAME#$HOSTNAME "
VAR=$VAR
export VAR
"
Possible Duplicate of this SuperUser Answer.
In a nutshell: You can pass values with a command similar to the following:
ssh username#machine VAR=value cmd cmdargs
Related
I'm writing a script that will scp a tar file from my local server to a remote host. Since the script generates the file through a pre-requisite process, the name is generated dynamically. My script needs to take the name of the file and pass it to scp for transfer.
#!/usr/bin/expect -f
spawn scp test.$(date +%y%m%d_%H%M).tar user#IP-ADDRESS:/destination/folder
set pass "password"
expect "password: "
send -- "$pass\r"
expect eof
I've tried setting the filename as a variable but keep seeing the same error:
can't read "(date +%y%m%d_%H%M)": no such variable
while executing "spawn scp test.$(date +%y%m%d_%H%M).tar user#IP-ADDRESS:/destination/folder"
$(date +%y%m%d_%H%M) is not a Tcl command. If you use expect, you have to learn Tcl. To get a formatted date in Tcl, use the clock command. Also, interpolation of the result from a command in Tcl is not done by $(....), but by [....]. You can find examples for this construct here.
Decided to go another route since the team was able to provision a new Artifactory repo for this binary and alike. However, to the advice provided here I was able to make a few discoveries which I used to fix my issues:
I also had a password with $ symbol and that also caused a world of issues.
#!/bin/bash
TEST=$(date +%y%m%d_%H%M)
/usr/bin/expect <<eof
set password {pas\$word}
spawn scp "$TEST" user#IP-ADDRESS:/destination/folder
expect "*password:"
send "$pasword\r"
expect eof
I have a problem with an execution of a shell script into a remote shell.
I can't get value of $ARQ_END.
ssh -T user#MACHINE << 'EOSSH'
/app/work/leo/ReturnFileName.sh #This script returns a filename like: ADDRESS_BR_RECIFE_20170913.txt
ARQ_END="`/app/work/leo/ReturnFileName.sh`"
EOSSH
echo $ARQ_END #Returns nothing! Expected to return: ADDRESS_BR_RECIFE_20170913.txt
Setting a variable in a subshell isn't visible in the parent shell. You need to set the variable directly in the parent shell. The way to do that is to pass the output of ReturnFileName.sh up through the ssh session and to the parent shell and capture it there.
ARQ_END=$(ssh user#MACHINE /app/work/leo/ReturnFileName.sh)
echo "$ARQ_END"
Thanks, it works!
I used the case as you posted:
ARQ_END=$(ssh user#MACHINE /app/work/leo/ReturnFileName.sh)
echo "$ARQ_END"
I am passing variable from one shell script to another which is being executed on another remote server.
Script 1
echo "Identification No."
read id
export id
ssh atul#10.95.276.286 'bash -s' < data_file.sh
Script 2
echo "ID is ---- "$id
cd /abc/xyz/data/
cat data_abcxyz.txt|grep '|$id|'|wc -l
By this way I am not able to get any output even the id is also null in the second script.
I have also tried
ssh atul#10.95.276.286 'bash -s' < data_file.sh "$id"
But got no output.
Any help on this is greatly appreciated. I am using unix AIX.
export on one host is absolutely not going to affect an entirely different host... it doesn't even affect another shell running on the current host.
Your second attempt is better and might even work if your script were checking for positional arguments but it isn't. (It might not even work in that case as I'm not at all sure that the command line argument would make it through to the script through ssh and bash -s.
You might be able to do something more like:
ssh atul#10.95.276.286 "bash -s $id" < data_file.sh
to pass the argument to the remote bash directly but your script would still need to use positional arguments and not expecting named variables to already exist.
Exporting won't have any effects on the environment of remote scripts.
You can set up a remote script's environment by specifying the env variables on the command line before the actual command, which you can btw use for local commands too.
ssh atul#10.95.276.286 "id=$id bash -s" < data_file.sh
If you pass "$id" this way:
ssh atul#10.95.276.286 'bash -s' < data_file.sh "$id"
It'll be your script's first parameter, AKA "$1" and you'll be able to access it from your script that way.
Note that '|$id|' in your "Script 2" will be interpreted as a literal string, since you're using single quotes.
I want to pass all the remote server names as shell script arguments and then want my script to copy the given file/directory to all remote servers. Below is my code.
#!/bin/bash
/usr/bin/expect <<EOD
#connect via scp
usr=Joe
pwd=Password
file_location=/home/file1.txt
for a in $#
do
spawn scp -r $file_location "$usr#$a:$file_location"
expect -nocase "password: "
send "$pwd\r"
expect eof
EOD
Username and password is same for all the remote servers. So I am hard-coding them for time being but while running the script I see a problem with that..
>./scp1.sh server1 server2
invalid command name "usr=Joe"
while executing
"usr=Joe"
Any help is appreciated. Thanks!
To read command line arguments from expect:
$argc - number items of arguments passed to a script.
$argv - list of the arguments.
$argv0 - name of the script.
This was take from this site
If you want more help with expect then google about tcl because expect is an extension to tcl language
I am trying to write a script to login to several remote servers and execute a script in each server.
However, I'd like to pass a variable through ssh. Something like this:
var="/home/dir/"
ssh -T $mchname <<'ENDSSH'
$var"run_script" < input > output &
ENDSSH
This naive try does not work ($var is simply null). What would be the correct syntax? Thanks in advance.
Remove the quotes from around ENDSSH. They prevent expansion of variables within the here document.
Try this:
var="/home/dir/"
ssh -T $mchname VAR=$var 'bash -s'<<'ENDSSH'
$VAR"run_script" < input > output &
ENDSSH