I'm trying to write a script that allows connection to various servers, e.g.
#!/bin/bash
# list of servers
server1=10.10.10.10
server2=20.20.20.20
ssh ${$1}
And I'd like to run it like:
sh connect.sh server1
Can't figure out how to use the parameter's name as a variable. Arrays do not work on my Ubuntu too.
Use shell indirection like this:
x=5
y=x
echo ${!y}
5
For your script, following works:
#!/bin/bash
# list of servers
server1=10.10.10.10
server2=20.20.20.20
arg1="$1"
ssh ${!arg1}
Easiest way would be to switch on $1:
case "$1" in
server1) ssh "$server1"
;;
server2) ssh "$server2"
;;
*) ssh "$server1" # when no parameter is passed default to server1
;;
esac
Try this:
#!/bin/bash
# list of servers
server1=10.10.10.10
server2=20.20.20.20
if [ "$1" == "server1" ]; then
ssh $server1;
elif [ "$1" == "server2" ]; then
ssh $server2;
fi
Related
I have an SSH command that is able to use variables defined in the script.
Example:
b="03-18-2022"
I'm able to pass this in to my ssh call and use it.
However I want to be able to define $b when I run the script: bash file.sh -b 03-18-2022
.. When I try doing this the SSH command cannot recognize the variable
CODE:
Getting the variable from the input:
while getopts ":b:" arg; do
case "${arg}" in
b) b="$OPTARG";;
esac
done
echo "Locally using begin: $b
printf -v b_str %q "$b"
ssh myserver "bash -s $b_str" << 'EOF'
b=$1
echo "remotely using $b"
The last echo works when the variable is defined in the script but not when it is passed in from the command line
is anybody aware if there is a syntax to pass a remote host parameters (user and IP/hostname) together with script arguments on local host and make it execute on the remote host?
I'm not meaning like this: $ ssh user#remoteServer "bash -s" -- < /path/script.ssh -a X -b Y
I want instead for the script to be able to be passed like this: $/path/script.ssh user#remoteServer -a X -b Y
But I'm not sure how to achieve, in the script, this kind of behaviour:
[...] script [...]
connect to user#remoteServer
[...] execute the script code (on the remote host) [...]
end of script
Any suggestion? Do I need to work this from another way instead?
EDIT
I've managed to make the script execute something after it connects via SSH, but I'm a bit as for why some commands are executed before they are passed to the remote host terminal; my code looks like this at the moment:
while getopts 'ha:u:d:s:w:c:' OPT; do
case $OPT in
a) host=$OPTARG;;
u) user=$OPTARG ;;
d) device=$OPTARG ;;
s) sensor=$OPTARG ;;
w) warn_thresh=$OPTARG ;;
c) crit_thresh=$OPTARG ;;
h) print_help
*) printf "Wrong option or value\n"
print_help
esac
done
shift $(($OPTIND - 1))
# Check if host is reachable
if (( $# )); then
ssh ${user}#${host} < $0
# Check for sensor program or file
case $device in
linux) do things
raspberry) do things
amlogic) do things
esac
# Read temperature information
case $device in
linux) do things
raspberry) do things
amlogic) do things
esac
# Check for errors
if (())
then
# Temperature above critical threshold
# Check for warnings
elif (())
then
# Temperature above warning threshold
fi
# Produce Nagios output
printf [......]
fi
The script seemingly runs without issue, but I get no output.
A simplistic example -
if (( $# )) # if there are arguments
then ssh "$1" < $0 # connect to the first and execute this script there
else whoami # on the remote, there will be no args...
uname -n # if remote needs arguments, change the test condition
date # these statements can be as complex as needed
fi
My example script just takes a target system login as its first argument.
Run it with no args it outputs the data for the current system; use a login, it runs there.
If you have password-less logins with authorized keys it's very smooth, otherwise it will prompt you.
Just parse your arguments and behave accordingly. :)
If you need arguments on the remote, use a more complex test to decide which branch to take...
Edit 2
I repeat: If you need arguments on the remote, use a more complex test to decide which branch to take...
while getopts 'ha:u:d:s:w:c:' OPT; do
case $OPT in
a) host=$OPTARG;;
u) user=$OPTARG ;;
d) device=$OPTARG ;;
s) sensor=$OPTARG ;;
w) warn_thresh=$OPTARG ;;
c) crit_thresh=$OPTARG ;;
h) print_help
*) printf "Wrong option or value\n"
print_help
esac
done
shift $(($OPTIND - 1))
# handoff to remote host
if [[ -n "$host" ]]
then scp "${user}#${host}:/tmp/" "$0"
ssh "${user}#${host}" "/tmp/${0##*/} -d $device -s $sensor -w $warn_thresh -c $crit_thresh"
exit $?
fi
# if it gets here, we're ON the remote host, so code accordingly
# Check for sensor program or file
case $device in
linux) do things
raspberry) do things
amlogic) do things
esac
# Read temperature information
case $device in
linux) do things
raspberry) do things
amlogic) do things
esac
# Check for errors
if (())
then
# Temperature above critical threshold
# Check for warnings
elif (())
then
# Temperature above warning threshold
fi
# Produce Nagios output
printf [......]
fi
I have a bash script which ssh's to a server, and depending on the status of a variable performs a task:
#!/bin/bash
foo=$1
ssh user#host.com '
echo In host
if [ "$foo" == "yes" ]; then
echo "Foo!"
fi
'
When I run sh script.sh yes, although the ssh command works, the conditional evaluates to false. I can see this if I echo $foo - it prints an empty line. How can I access the value of foo within the ssh command?
Variables aren't transferred to a remote machine. You can expand the variable in the code sent through ssh, but you have to be extremely careful because it opens the door to uncontrolled code execution:
#!/bin/bash
foo=$1
ssh user#host.com '
echo In host
if [ "'"$foo"'" == "yes" ]; then
echo "Foo!"
fi
'
Now imagine (don't try) what happens if foo='$(rm -rf /)'.
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 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`