LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-26-2016, 05:06 AM   #1
depam
Member
 
Registered: Sep 2005
Posts: 856

Rep: Reputation: 30
Logging in to another a remote server using SSH


Use Case:

(1) SSH in to one server and login with username and password using expect
(2) Once logged in, login to another server using another username and password via expect

I tried the below script but does not work

#!/bin/bash
SERVER1=1.1.1.1
SERVER2=2.2.2.2

/usr/bin/expect <<EOD
set timeout 1000000
spawn ssh $CLIENT_ID@$SERVER1
expect *$*
send "ssh $CLIENT_ID@$SERVER2\r"
expect "assword:"
send "Pass123\r"
interact
EOD

Can this be done?
 
Old 09-26-2016, 05:47 AM   #2
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDo, tinycore, Q4OS,Manjaro
Posts: 4,008

Rep: Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857
Yes, it can be done.

That is not the way I would do it.
Have you considered setting up keys for password-free authentication, and simply doing something like
Code:
 ssh guy1@1.1.1.1 "ssh guy2@2.2.2.2"
to accomplish the same thing with less effort?
 
Old 09-26-2016, 06:16 AM   #3
descendant_command
Senior Member
 
Registered: Mar 2012
Posts: 1,804

Rep: Reputation: 621Reputation: 621Reputation: 621Reputation: 621Reputation: 621Reputation: 621
Quote:
Originally Posted by wpeckham View Post
Yes, it can be done.

That is not the way I would do it.
Have you considered setting up keys for password-free authentication, and simply doing something like
Code:
 ssh guy1@1.1.1.1 "ssh guy2@2.2.2.2"
to accomplish the same thing with less effort?
+1

Laziness is a virtue in a sysadmin
 
Old 09-26-2016, 06:52 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,864
Blog Entries: 3

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
Quote:
Originally Posted by descendant_command View Post
+1

Laziness is a virtue in a sysadmin
True.

Taking it a step further, you can put your options in ~/.ssh/config and then just type ssh server2 as a shortcut to get to server2 via server1:

Code:
Host server1
        Hostname 1.1.1.1
        User guy1
        IdentityFile ~/.ssh/server1_ed25519
        IdentitiesOnly yes
        Port 22

Host server2
        Hostname 2.2.2.2
        User guy2
        IdentityFile ~/.ssh/server2_ed25519
        IdentitiesOnly yes
        Port 22
        ProxyCommand ssh -W %h:%p server1
Though if you have a more recent version of OpenSSH, version 7.3 to be specific, there is an even easier way. Which version do you have?
 
1 members found this post helpful.
Old 09-26-2016, 08:27 AM   #5
depam
Member
 
Registered: Sep 2005
Posts: 856

Original Poster
Rep: Reputation: 30
Thanks for your replies. I have initially tried the one suggested by @vwpeckham but it does not work for me.

$ ssh username@server1 'ssh username@server2'
Pseudo-terminal will not be allocated because stdin is not a terminal.
Authenticated with partial success.
Permission denied, please try again.
Permission denied, please try again.
Permission denied (password).
$

I do not have access to root for either server1 and server2 so I cannot modify any config. Server1 and Server2 doesn't also have expect there so this is triggered mainly on the client.
 
Old 09-26-2016, 08:33 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,864
Blog Entries: 3

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
Quote:
Originally Posted by depam View Post
...
$ ssh username@server1 'ssh username@server2'
Pseudo-terminal will not be allocated because stdin is not a terminal.
...
For the above, you need the -t option with the SSH client to force pseudo-terminal allocation.

Quote:
Originally Posted by depam View Post
...
I do not have access to root for either server1 and server2 so I cannot modify any config. Server1 and Server2 doesn't also have expect there so this is triggered mainly on the client.
For what I suggested in the earlier post regarding the configuration, I am referring to the client configuration file on your own computer. It is on the computer you are connecting from, usually in ~/.ssh/config Using it will save you work. No root access is needed or desired, as it will only work when done as your account.

At this point, I would highly recommend skimming through the manual page for ssh_config and ssh on your own computer so that you are familiar with the options your version has.
Code:
man ssh_config
man ssh
 
Old 09-26-2016, 08:57 AM   #7
depam
Member
 
Registered: Sep 2005
Posts: 856

Original Poster
Rep: Reputation: 30
@Turbocapitalist, This works like a charm. You're the man!!
 
Old 09-26-2016, 09:59 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,864
Blog Entries: 3

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
No worries.

If you don't want to type more than ssh server1 server2 each time, then you can put the equivalent of -t into your SSH client configuration ( ~/.ssh/config ) up near the top:

Code:
Host server1
        User clientid
        Hostname 1.1.1.1
        RequestTTY force
Or you can try the step-saving way up above with ProxyCommand

Last edited by Turbocapitalist; 09-26-2016 at 10:00 AM.
 
  


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
[SOLVED] I am trying to remote execute a python command vis ssh. I am getting a syntax error from the remote server. buckb Linux - Networking 7 12-29-2015 07:52 AM
How to specify facility when logging to remote server tikit Solaris / OpenSolaris 4 02-18-2010 01:20 PM
Logging into a remote server anaigini45 Linux - Newbie 5 01-03-2010 06:56 AM
logging message on both remote and local server noir911 Linux - Server 4 10-22-2008 11:51 PM
logging, remote ssh, scripts, and the at command gctaylor1 Programming 4 01-29-2007 08:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:03 AM.

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
Open Source Consulting | Domain Registration