12

I am trying to connect to a remote host from my local host through the below command.But there was a setting in the remote host that soon after we login it will prompt to enter a badge ID,password and reason for logging in, because it was coded like that in profile file on remote-host How can I overcome those steps and login directly non-interactively, without disturbing the code in profile.

jsmith@local-host$ ssh -t -t generic_userID@remote-host
Enter your badgeID, < exit > to abort:
Enter your password for <badgeID> :
Enter a one line justification for your interactive login to generic_userID
Jill448
  • 1,745
  • 10
  • 37
  • 62

3 Answers3

12

Small amendment: to overcome remote server expect approach is required, but in case local script connects to bunch of remote servers, which configuration may be broken, just use SSH options:

ssh -f -q -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null USER@TARGETSYSTEM

This will omit ask for password in case there is no ssh_key setup, exit silently and continue with script/other hosts.

Puts ssh to background with -f, which is required when calling ssh command from sh (batch) file to remove local console redirect to remote input (implies -n).

galoget
  • 722
  • 9
  • 15
Arunas Bartisius
  • 1,879
  • 22
  • 23
  • Having StrictHostKeyChecking and UserKnownHostsFile in the ssh and in the ssh config file was the only way I got a non interactive ssh. – AntonioCS Aug 26 '23 at 19:21
3

Look into setting up a wrapper script around expect. This should do exactly what you're looking for.

Here are a few examples you can work from.

Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54
1

I have upvoted Marvin Pinto's answer because there is every reason to script this, in case there are other features in the profile that you need, such as Message of the Day motd.

However, there is a quick and dirty alternative if you don't want to make a script and you don't want other features from the profile. Depending on your preferred shell on the remote host, you can insist that the shell bypasses the profile files. For example, if bash is available on the remote host, you can invoke it with:

ssh -t -t generic_userID@remote-host bash --noprofile

I tested the above on the macOS 10.13 version of OpenSSH. Normally the command at the end of the ssh invocation is run non-interactively, but the -t flag allows bash to start an interactive shell.

Details are in the Start-up files section of the Bash Reference Manual.

dcorking
  • 1,146
  • 1
  • 14
  • 24
  • 2
    This may not work as expected. That ssh command would cause sshd to invoke the string "bash --noprofile" as a shell command. In other words, sshd is going to invoke the equivalent of `$SHELL -c 'bash --noprofile'`. The shell instance started by sshd will still go through whatever initialization it performs. – Kenster Dec 31 '17 at 14:09
  • @Kenster I tested this with bash as the default shell, and `~/.bash_profile` does not run. – dcorking Dec 31 '17 at 20:28
  • `-t user@host bash --noprofile` was helpful in simulating a non-interactive shell on a remote host. I wanted to simulate the environment (specifically `$PATH`) under which a script will execute, since non-interactive shells do not implicitly `source .bash_profile` as an interactive shell does. – Bill Feth Dec 07 '20 at 17:01