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->
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"
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?
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?
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.
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 user@my.server.com
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.
Expect is the usual tool for automating interactive sessions.
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 pi@192.168.0.145
this can be combined with
ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no pi@192.168.0.145
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/
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
Have you considered Paramiko? It's a Python-library for interacting with SSH.
Even if I would use pem keys for this and this is an old topic, I also wanted to quote sshpass