10

I have this bash file, which asks for IP, password, etc. for OpenSSH to a device.

Now, if i use ssh root@ip, i have to enter the password. This is really irritating. Secondly; i cannot let my script send commands to it.

This is what i want->

  1. Not the password thing; i already found something; but it tells me the commands are not found?:

    #!/usr/bin/expect -f

    spawn ssh root@$IPADDR

    expect "password:"

    sleep 1

    send "$rpass\r"

    1. I want the user to be able to see some menus where it can choose from; after that; every command is done for him. So like a new window, or something like that?

    2. I do not want to use: -Any external stuff -No extra editing of the SSH connection

BASH INFO: GNU Bash, v. 4.0.33(1)-release (i486-pc-linux-gnu), running on Linux Mint. But it got to be available for several linux distro's, and also on Mac?

1

8 Answers 8

17

The proper way to do this without storing passwords in plaintext on your machine is with ssh. First run:

ssh-keygen

This will generate a new SSH key in ~/.ssh/id_rsa.pub. After that simply run:

ssh-copy-id [email protected]

If you're on OS X or another machine that does not have "ssh-copy-id" there are one-line alternatives such as this one:

cat ~/.ssh/id_rsa.pub | ssh user@machine "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"

Ultimately you just need to append the contents of ~/.ssh/id_rsa.pub on your local machine to ~/.ssh/authorized_keys on the remote server. How you do that is up to you, the above are just quick shortcuts to do that.

0
3

Many tools to go great lengths to prevent what you are doing. I recommend using ssh public keys to solve this problem instead of passwords.

The big alternative is to write your own modified ssh client based on the open source so as to take control of the password management.

Oh, well, I forgot. You can probably outsmart this with a pty, since then /dev/tty will be what you control. expect might help you with this.

2
  • 1
    as i stated: No extra editing of the SSH connection; including public keys. Dec 17, 2009 at 20:50
  • I could start editing the ssh client, but it's going to take some time. Thanks for suggesting. Dec 18, 2009 at 7:22
2

Expect is the usual tool for automating interactive sessions.

4
  • i already gave this in my question, but i do not have spawn and send either. Now, i really have to download them, put in my folder? So harsh. Dec 17, 2009 at 21:00
  • Those are expect commands, you ARE running the script through expect, yes? Dec 17, 2009 at 23:44
  • Spawn and send are commands that expect recognizes. They aren't separate downloads. You need to make sure that the path to expect is correct for your system in the shebang. You will also probably want to use the interact command in your expect script. Dec 18, 2009 at 1:07
  • how can i use expect as something to share with others? Dec 18, 2009 at 7:20
2

The proper way to go is to copy the keys as has been said here. To add something to the conversation, there are cases where sshpass can be handy.

The question asks specifically about scripting in a system with SSH. If it is the development of an embedded system, it can be useful to combine sshpass with command line options, as it reads on this post

sshpass -p raspberry ssh [email protected]

this can be combined with

ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no [email protected]

to avoid confirmation questions that prevent scripting from happening.

Again, only use this in development systems where different machines share an IP and security is not important.

https://ownyourbits.com/2017/02/22/easy-passwordless-ssh-with-sshh/

1

Use ssh-keygen to create a public key for your machine, then copy your local ~/.ssh/id_rsa.pub or ~/.ssh/identity.pub to the remote system, in ~/.ssh/authorized_keys.

You may need to tighten the permissions on the authorized_keys file: chmod 600

1

Have you considered Paramiko? It's a Python-library for interacting with SSH.

4
  • Can i interact with it trough bash? Dec 18, 2009 at 7:30
  • 1
    Depends on what you want. If you'd provided more details about what problem you're really trying to solve, it'd be easier to help. Dec 18, 2009 at 8:04
  • What i try to do is using bash make a sort of ssh client. I really like this Paramiko idea; but what i need to do is interacting it with bash. The code is written in bash and i want it to keep it like that. I do not know Python; i am a beginning programmer. Anyway; what i want to do is like (almost) everything what's possible with ssh; the password thing as itś in paramiko, cd to directory; permission editing; removing files/folders; creating folders; copying files/folders from PC to Device; etc. Dec 18, 2009 at 8:11
  • Advanced Necropost: Pexpect, specifically pxssh, pexpect.sourceforge.net/pxssh.html Does the job.
    – Zombro
    Sep 16, 2016 at 13:36
1

Even if I would use pem keys for this and this is an old topic, I also wanted to quote sshpass

0

You can use this script: https://github.com/aprey10/ssh-authorizer

It also allows to skip ssh keys passphrase request.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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