2

I'm writing a shell script to connect to a Linux based remote machine using ssh

After successfully logging in to the remote machine I'm able to execute Linux commands, now the real problem I'm facing is when trying to run the same script for another remote machine which will ask for a local authentication(username and password) upon ssh login to proceed further.

Can someone guide me on how to fill the first line with my username and immediately do a carriage return and perform similar action for password.

I've tried the below code connect.sh

sshpass -p <remote-passwd> ssh root@<remote-ip> 'bash -s' < test.sh

test.sh contain

ls
pwd

If I run connect.sh script it executes perfectly without asking for remote machine password. It also executes ls and pwd

Can I actually replace ls and pwd with my username and password to achieve what I'm trying to do??

Also, Am I looking on something which is not possible?? (I have seen a similar code in VB.NET which is solving my purpose but it is not a robust code and I really don't have any idea on VB scripts)

Update: I'm able to login to remote machine non interactively, but the remote machine environment immediately asks for a local authentication which again requires keyboard interaction, I'm looking for achieving this authentication non interactively

prince
  • 1,129
  • 1
  • 15
  • 38
  • logging in automatically over SSH is indeed possible, Check http://stackoverflow.com/questions/12202587/automatically-enter-ssh-password-with-script – bdvll Aug 12 '16 at 13:27
  • See also [**write a shell script to ssh to a remote machine and execute commands**](http://stackoverflow.com/questions/13928116/write-a-shell-script-to-ssh-to-a-remote-machine-and-execute-commands).For details on setting up *passwordless* access, see: [**How to setup the passwords-less authentication between two different accounts**](http://stackoverflow.com/questions/36171227/how-to-setup-the-passwords-less-authentication-between-two-different-accounts/36172337?s=1|0.4287#36172337). – David C. Rankin Aug 12 '16 at 13:30
  • @bdvll I'm okay with this part of the problem, my problem is after achieving this... – prince Aug 12 '16 at 13:33
  • @prince Ok, so what exactly is it that you are having issues with? you could use the 'expect' syntax to add the pass and username passing to your bashcript and then run your commands. – bdvll Aug 12 '16 at 13:36
  • @bdvll I'll test with that once, and post the update. Thanks! – prince Aug 12 '16 at 13:37
  • 1
    You should configure public key authentication so you don't have to type a username and password. – Barmar Aug 12 '16 at 14:04
  • @Barmar, please see update on my question. Thanks. – prince Aug 13 '16 at 19:43
  • If you need to control interactive programs, take a look at `Expect`. – Barmar Aug 15 '16 at 06:26

1 Answers1

0

If at all possible, you should configure a public key on the server so you don't have to supply a password. This is more secure and will solve your problem more directly.

You may also want to look into orchestration frameworks, rather than implementing this all yourself. If you're doing small things, Fabric is a good option. If this looks like it'll become something much larger, you should look into something like Ansible, which can also additionally handle system configuration and a million other things, but requires very little setup to get started with.

Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101