3

I need to find a way to autologin on Remote Machine. There are several way in which we can do this but this is little tricky for me.

Auto Login to a remote machine and execute command or script and redirect the output in the local system file.

ssh remote-host < ./script >> storageinfo_$date.txt

But the hard part is to we can't directly connect to the remote host; we need to first connect to the Jumphost1 --> Jumphost2 --> and then --> remote-host

Jumphostx is sshkeygen enabled but with passphrase for eg: userpass
remote-host is not sshkeygen enabled eg: remotepass

We used to do this with .ssh/config file in the below manner. This was successful so far in the test env. But we are not supposed to install expect in the live env.

# cat .ssh/config

Host jump1-*
    User ldap-user
    IdentityFile ~/.ssh/id_rsa
    ForwardAgent yes
    ServerAliveInterval 60
    ServerAliveCountMax 12

Host jump1-centos01-temporary 
    Hostname 13.1.2.19
    Port 2222

Host jump1-centos01        
    Hostname 1.2.1.18
    Port 22
    ProxyCommand ssh -W %h:%p jump1-centos01-temporary

Host remote-host
    Hostname 12.1.1.8
    ProxyCommand ssh -W %h:%p jump1-centos01
    User root

ssh connection with expect and send patern

# cat expect.sh 

#!/usr/bin/env expect
set timeout 7
set date [exec date "+%d-%B-%Y"]

spawn sh -c "ssh va1ap-vsns0001n < ./isi.py > storageinfo_$date.txt"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "userpass\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "userpass\r"
expect "Password:"
send "remotepass\r"
interact
0

ssh -t $jump1 ssh -t $jump2 ssh -t $tgt

would achieve the connection you're looking for; -t ensures a pty is allocated, which it normally wouldn't be if ssh is asked to execute a command at the destination.

In this circumstance, an agent and agent forwarding would seem wholly appropriate, so it would look more like this:

ssh -A -t $jump1 ssh -A -t $jump2 ssh -t $tgt

If that doesn't work for you, script can be used as an alternative to expect in some circumstances. For example, echo $passwd | script -q -f -c "ssh -q -t $host sudo -p \"''\" \"$some_command\" is a pattern I've needed to use for non-interactive purposes.

Really, though, I'd push for getting some type of key-based or single-signon auth configured; the workflow you describe sounds like a nightmare, especially if your test and prod environments are required to be so different.

  • I have tried the -t pattern this before it work but there was some problem. We tried it with 2 different way one was with .ss/config file and running the except and send password format & the second was python script with paramiko which will create a tunnel. Python script can login to the remote server but it can't send my local script and get redirect the output back to the local machine. – Poo May 23 '17 at 8:33
  • my .ssh/config works absolutely fine but because of security reason we cant install expect package. – Poo May 23 '17 at 8:33
  • Honestly, you have two big problems. First problem, you're required to employ password authentication. Whatever is forcing you to send passwords is, in my view, a major source (if not the major source) of your difficulties. The second problem is the expect limitation. Your security guys are effectively asking you to reimplement expect in lieu of installing the tool properly, and that, on its own, means introducing greater risk of programmer error and the resulting security consequences. – Michael Mol May 23 '17 at 14:04
  • "Python script can login to the remote server but it can't send my local script and get redirect the output back to the local machine" Absent getting the environment saner, this would be the next thing I'd pursue. Why can't the Python script copy your local script out, execute it and copy the output streams back? It sounds like you're 90% of the way to a working alternative to expect already. – Michael Mol May 23 '17 at 14:06
  • We are not suppose to create any file on the remote host due to restriction. – Poo May 24 '17 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.