Bash Script for SSH to the machine without asking for a password and without using keys

I understand that this question has been asked several times, but I could not find the answer anywhere in my search.

I work in a development environment where security is not a problem, and anyone could just guess the password if I thought for a few seconds.

What I'm trying to do is simple. I created an alias function in my local .bashrc file, and I would like this function to automatically log in with the default password.

My current implementation looks something like this:

function s () { ssh root@192.168.1.$1 } 

When I run it, I get something like this:

 ~]s 122 ssh root@192.168.1.122 root@192.168.1.122 password: 

Using Bash and not using RSA keys, I would like this to use the default password password.

I tried the following when IP and User are already set.

 Do=$(expect -c " spawn ssh $User@${IP[0]}.${IP[1]}.${IP[2]}.${IP[3]} expect \"yes/no\" send \"yes\r\" expect \"assword\" send \"password\"") echo $Do $Do 

It gives the following error:

 Connecting and logging into server using expect usage: send [args] string while executing "send" invoked from within "expect "assword" send "password"" Administrator@192.168.1.176 password: bash: spawn: command not found... 

Using the following command, I can connect the machine. If I remove the interaction, it will simply run the uptime command and close the connection. With the help of the interaction team, I cannot see what I am typing or actually interacting with the machine. Any ideas?

 Do=$(expect -c "spawn ssh $User@${IP[0]}.${IP[1]}.${IP[2]}.${IP[3]}; set timeout 4; expect \"assword\"; send \"password\n\"; expect \"test\"; send \"uptime\n\"; interact;");echo $Do; 
+6
bash ssh
Nov 08
source share
2 answers

You can do this with the expect tool: http://expect.sourceforge.net/

It is widely available, so depending on your system, the equivalent of sudo apt-get install expect or yum install expect will install it.

Here is an example expect script with ssh. This brings you to the system and gives you the ability to manage an interactive prompt:

 #!/usr/bin/expect set login "root" set addr "127.0.0.1" set pw "password" spawn ssh $login@$addr expect "$login@$addr\ password:" send "$pw\r" expect "#" send "cd /developer\r" interact 

Here is an example of how to use expect as part of a bash script. This log goes into ssh, cd to / var, runs the script, and then ends the ssh session.

 #!/bin/bash ... login_via_ssh_and_do_stuff() { # build the expect script in bash expect_sh=$(expect -c " spawn ssh root@127.0.0.1 expect \"password:\" send \"password\r\" expect \"#\" send \"cd /var\r\" expect \"#\" send \"chmod +x my_script.sh\r\" expect \"#\" send \"./my_script.sh\r\" expect \"#\" send \"exit\r\" ") # run the expect script echo "$expect_sh" } 

You can leave these fragments in a script on your local system, and then simply add an alias to the scripts.

In addition: I know that you said that security is not a problem, but I would like to note once again that the β€œcorrect” way to ssh without using a password is to use ssh key-pair =)

+7
Nov 08
source share

Use sshpass , which is available in package repositories on major Linux servers.

For example, when the password is in the password.txt file:

 sshpass -fpassword.txt ssh username@hostname 

sshpass starts ssh in dedicated tty , tricking it into thinking that it is getting a password from an interactive user.

+4
Dec 23 '13 at 17:36
source share



All Articles