This question already has answers here:
Bash: controlling SSH
(8 answers)
Closed 9 years ago.
Everyday I am connecting to a server through ssh. I go through this routine:
IC001:Desktop user$ ssh user#my.server.com
user#my.server.com's password:
Last login: Tue Jun 4 10:09:01 2013 from 0.0.0.0
$
I would like to automate this process and create a bash script to do it for me. I don't care about security and okay to store my password openly in the script. I am also okay for it to get typed openly on the screen while the script gets executed. So I've created this:
#!/bin/bash
ssh user#my.server.com
echo mypassword
But it doesn't work. I've also tried send instead of echo, but it also didn't work. Please advise if it is possible to do.
Double check if you are not able to use keys.
Otherwise use expect:
#!/usr/bin/expect -f
spawn ssh user#my.server.com
expect "assword:"
send "mypassword\r"
interact
Create a new keypair: (go with the defaults)
ssh-keygen
Copy the public key to the server: (password for the last time)
ssh-copy-id user#my.server.com
From now on the server should recognize your key and not ask you for the password anymore:
ssh user#my.server.com
Related
This question already has answers here:
Shell script to automate SSH login using password
(2 answers)
Closed 5 years ago.
I am trying to make a script in bash that connects via ssh to another machine.
I only have user access to this machines so
I can't use expect (It is not installed and I can't install it)
I can't install ssh keys
So I have to log in via username and password.
Is there a way to make my script send my password just using bash?
You can do that with sshpass, however, it's insecure.
sshpass -p YOUR_PW ssh ...
That will connect without asking for password.
You might need to add the flag -oStrictHostKeyChecking=no to ssh for auto-accepting keys.
This question already has answers here:
How to enter ssh password using bash? [duplicate]
(2 answers)
Closed 7 years ago.
I have a simple bash application in Mac OS that establishes a SSH connection.
the code is like this:
$ ssh user1#machine.com -p61023
When I run the script, console always prompts asking for a password. (It is a very rudimentary process)
appelelog#sunlineclass.com's password:
How I can automate this process?
Create a key pair.
Add the private key to the Keychain: ssh-add -K ~/.ssh/host_id_rsa.
Never type the password again, other than the login password.
If you run the server, then other things I do:
Listen on port 422 instead of 22, to reduce break-in attempts.
Only allow login via public/private key pairs, and not passwords.
Only allow login by a select list of users (never root).
This question is off-topic, however, and you should have asked it here.
#Kalanidhi gave me a hint:
Here is my new script
sshpass -p 'yourpassword' ssh user#machine.com -p61023
This question already has answers here:
How to run the sftp command with a password from Bash script?
(12 answers)
Closed 8 years ago.
Is it possible to pass SFTP USER/PASS to a server in an automated script that will log in and retrieve a file?
I know that KEY PAIRS are the recommended method but assume thats not possible in this case.
In the simplest case you use a key based authorization so you don't need to enter any credentials.
For doing that create a key:
ssh-keygen -t rsa
And copy it to the target system:
ssh-copy-id -i ~/.ssh/id_rsa.pub user#remote-system
Now you can login to the system without a password.
If your problem is the missing ssh-copy-id command try this here:
cat ~/.ssh/*.pub | ssh user#remote-system 'umask 077; cat >>.ssh/authorized_keys'
I'm trying to write a shell script (Bash) to log into a SonicWall firewall device and issue a command to perform automated backups of the devices ruleset. I prefer to do this in Bash but I will accept a python, perl, except, or applescript solution. If it cannot be done in bash please mention that.
Problems:
1.) SSH server on firewall is custom, a user name and password has to be specified after issuing a
$ ssh server.com
so no matter what username you issue e.g.
$ ssh admin#server.com
the SSH server still presents a username and password box after
2.) The SSH server is minimal and I cannot use public-keys
I tried using a here-document but it isn't working and it results in an immediate "connection closed by remote host".
The command I need to execute takes the form of this:
export preferences ftp "ftp.server.com" "user1" "mypassword" "output.exp"
Connecting gives me this:
$ ssh admin#server.com
Copyright (c) 2010 SonicWALL, Inc.
User:
After a username is issued it brings up the password prompt:
User:user1
Password:
I tried a here-document to no avail.
$ ssh server <<+
user1
mypassword
export preferences ftp "ftp.server.com" "user1" "mypassword" "output.exp"
exit
+
Pseudo-terminal will not be allocated because stdin is not a terminal.
Connection to 10.1.1.1 closed by remote host.
I tried using echo to pipe in commands too but that doesn't work either.
Typing the commands in manually works just fine.
Any help on this would be greatly appreciated.
As others have suggested, expect is probably what you want to use here.
Here's a short example of how to work with it from bash to get you started:
login=root
IP=127.0.01
password=helloworld
# +whatever variables you need to use
# Run the expect script from bash
expect_sh=$(expect -c "
spawn ssh $login#$IP
expect \"password:\"
send \"$password\r\"
expect \"#\"
send \"cd $dest_dir\r\"
expect \"#\"
send \"chmod +x $server_side_script $other_script\r\"
expect \"#\"
send \"./$device_side_script\r\"
expect \"#\"
send \"cat results_file\r\"
expect \"#\"
send \"exit\r\"
")
# Output or do something with the results
echo "$expect_sh"
You can automate the ssh session using the original expect, here is a nice article discussing it in detail: http://solar1.net/drupal/automating%20SSH%20with%20expect or the Python module pexepect: http://linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/
I'm not a BASH expert but i had to do something where interactive password prompts was causing me a problem.
Basically your script needs to wait to be asked to enter login credentials, and pass them when prompted in order to login, once logged in you can issue the command.
I recommend looking at spawning "expect" sessions. Basically in your script you use expect to basically say "i expect to see password: in the response, when i do, i need to pass in the following data".
Here's the wiki page which helps explain it http://en.wikipedia.org/wiki/Expect
and if you google around you will find lots of help.
that didn't work for me.
I had to pass the variables to the script at launch.
Example launch script login2.sh, with three arguments:
-bash-4.1$ ./login2.sh Jan2**** HIE_SUPER 10.244.112.182
So I have a script that ssh into another computer. Since I use it often, I was wondering if I could have the script automatically enter in the password prompted by ssh, or in any other way bypass copy-pasting the password every single time I run the script?
Better setup up ssh keys with empty passphrase than putting your password in a script.
https://wiki.archlinux.org/index.php/SSH_Keys
It may be better to have a passphrase-less private key on the client side, paired with a public key in the server-side authorized_keys file with a specific command that gets run.
For example, having the following in .ssh/authorized_keys2 for the given user on the remote host:
command="date" ssh-rsa AAAAB3NzaC1yc2EAAAABIw[...]Q== Comment for passphraseless key
Will only ever run date when you connect using that key:
[localhost] % ssh -i /path/to/id user#remotehost
Sun 20 Nov 2011 20:29:59 EST
Connection to remotehost closed.