LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-04-2015, 08:25 AM   #1
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Rep: Reputation: Disabled
pass variable value in ssh command


Hello all,
I am trying to execute
Code:
ssh $a@$b mkdir abc
in a shell script where, a and b are variables
a=username and b=server.ip
It is giving me an error
Quote:
ssh connect to host port 22 connection refused
why am I getting the error?? ssh does not accept variables? Can anyone help.
Thanks in Advance
 
Old 06-04-2015, 08:54 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,075
Blog Entries: 14

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Try putting single quote around the command you want ssh to execute @b and also put brackets around your variables to ensure it sees them distinctly:
ssh ${a}@${b} 'mkdir abc'

Also if that still doesn't work be sure to verify a simple ssh works as you expect:
ssh <user>@<hostIP>
It's always possible your issue has nothing to do with variables but simply a failure to connect to the remote server as the user you're specifying which is what the error is telling you. Command line without variables would give same error if that is the case.
 
1 members found this post helpful.
Old 06-04-2015, 08:59 AM   #3
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Original Poster
Rep: Reputation: Disabled
thanks... Yeah I have tried using the basic ssh <user>@<ip> command and it works perfectly.. will try the ssh ${a}@${b} 'mkdir abc' command and let u know abt the result..
 
Old 06-04-2015, 10:14 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
What shell are you using?
In bash, neither the braces (since variable names cannot contain '@') nor the single quotes are necessary, although the quotes are recommended and the braces make the command more readable. The command you showed in the original post just has to work the way it is. Please check the contents of the variables a and b and make sure you didn't misspell any of them.
 
Old 06-05-2015, 03:19 AM   #5
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Original Poster
Rep: Reputation: Disabled
tested : ssh ${a}@${b} 'mkdir -p .ssh' but its not working

i am executing .sh file from cygwin terminal

contents: #!/bin/bash
a=`grep -Po '(?<=Username=).*' /cygdrive/d/vj/rs`
b=`grep -Po '(?<=Password=).*' /cygdrive/d/vj/rs`
c=`grep -Po '(?<=IP ADD=).*' /cygdrive/d/vj/rs`
echo -ne '\n' | ssh-keygen -t rsa -N "" -f id_rsa
echo -ne '\n'
echo -ne '\n'
echo -ne '\n' | ssh ${a}@${b} 'mkdir -p .ssh'

manually, all commands are working perfectly.
automatically, all commands are working except last command.
its not accepting '$' (maually & automatically)
giving an error: ssh: connect to host port 22: Connection refused
 
Old 06-05-2015, 04:30 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,905

Rep: Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921
please use [code]here comes your script[/code] to keep formatting
also try
Code:
#!/bin/bash
set -xv
.....
to see what's happening. (That will not solve anything, just print the lines as they executed, so you will be able to check tthc program flow).
Also at the last line you do not need echo:
Code:
ssh $a@$b 'mkdir -p ~/.ssh'
can you please explain what do you mean by "its not accepting '$' (maually & automatically)"
 
Old 06-05-2015, 05:47 AM   #7
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Original Poster
Rep: Reputation: Disabled
in the above.. a, b ,c is a variable in which values of username, password, IP address stored from the file (rs).
checked these three commands one by one maually.. its working
if i write: ssh $a@$b 'mkdir -p ~/.ssh' instead of ssh root@XXX.XXX.XXX.XXX 'mkdir -p ~/.ssh'
then it is not taking value of a and b
 
Old 06-05-2015, 06:53 AM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
Quote:
Originally Posted by vjlxmi View Post
in the above.. a, b ,c is a variable in which values of username, password, IP address stored from the file (rs).
checked these three commands one by one maually.. its working
if i write: ssh $a@$b 'mkdir -p ~/.ssh' instead of ssh root@XXX.XXX.XXX.XXX 'mkdir -p ~/.ssh'
then it is not taking value of a and b
Yeah - but if "b" is supposed to be the password it definitely won't work.
 
Old 06-05-2015, 07:06 AM   #9
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Original Poster
Rep: Reputation: Disabled
sorry typing mistake..
its ssh $a@$c 'mkdir -p ~/.ssh'
where a is 'root' and c is IP Address
 
Old 06-05-2015, 09:10 AM   #10
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
Quote:
Originally Posted by vjlxmi View Post
sorry typing mistake..
its ssh $a@$c 'mkdir -p ~/.ssh'
where a is 'root' and c is IP Address
Now add the "-vx" option to the bash command and see what is happening.
 
Old 06-05-2015, 01:49 PM   #11
joec@home
Member
 
Registered: Sep 2009
Location: Galveston Tx
Posts: 291

Rep: Reputation: 70
what you are looking for is:

echo 'mkdir abcd' | sshpass -p 'MyPassWord' ssh root@1.2.3.4 bash
 
1 members found this post helpful.
Old 06-05-2015, 07:28 PM   #12
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
Quote:
Originally Posted by joec@home View Post
what you are looking for is:

echo 'mkdir abcd' | sshpass -p 'MyPassWord' ssh root@1.2.3.4 bash
You don't need (and likely don't want) the echo. Just using "ssh user@host mkdir abcd" is enough.
 
Old 06-05-2015, 07:38 PM   #13
joec@home
Member
 
Registered: Sep 2009
Location: Galveston Tx
Posts: 291

Rep: Reputation: 70
Quote:
Originally Posted by jpollard View Post
You don't need (and likely don't want) the echo. Just using "ssh user@host mkdir abcd" is enough.
True, though I actually replaced cat with echo from a much larger routine. A slice of that routine would look like:

cat MyScript |sshpass -p $PASSWORD ssh root@$SERVERLIST bash

But the script needed each line to end in ;\ for it to send correctly.
 
Old 06-05-2015, 07:39 PM   #14
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,482

Rep: Reputation: 622Reputation: 622Reputation: 622Reputation: 622Reputation: 622Reputation: 622
Just as an aside
Code:
echo -ne "\n"
Is meaninless, the -n switch tells echo to suppress the trailing newline and the -e switch tells it to accept escape codes, so you are saying efectivly don't use new line and add new line, the above command is just the same an echo with no arguments.
 
1 members found this post helpful.
Old 06-05-2015, 09:48 PM   #15
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
Quote:
Originally Posted by joec@home View Post
True, though I actually replaced cat with echo from a much larger routine. A slice of that routine would look like:

cat MyScript |sshpass -p $PASSWORD ssh root@$SERVERLIST bash

But the script needed each line to end in ;\ for it to send correctly.
In which case I would expect that
Code:
sshpass -p $PASSWORD ssh root@$SERVERLIST bash <MyScript
Would work better.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass contents of variable to curl command hattori.hanzo Programming 1 07-23-2013 02:09 AM
Pass a shell variable to an AWK command chogall Programming 1 12-23-2010 11:12 AM
How do you pass an expect command result into a variable and then use it again? edomingox Programming 2 05-09-2010 02:51 AM
How to pass a result of exec command in perl to a variable??? HyperTrey Programming 3 05-23-2008 01:47 PM
How do I pass a C variable to a Bash command ? Linh Programming 6 07-07-2003 04:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration