Shell Script for logging into a ssh server
I tried writing a shell script which can do automatic login into a ssh server using password which is mentioned in the script. I have written the following code:
set timeout 30
/usr/bin/ssh -p 8484 root@172.31.72.103
expect
{
"root@172.31.72.103's password"
{
send "passwordr"
}
}
This code is not running properly, still it is asking for the password. Can somebody please help me in solving this
ssh password expect
add a comment |
I tried writing a shell script which can do automatic login into a ssh server using password which is mentioned in the script. I have written the following code:
set timeout 30
/usr/bin/ssh -p 8484 root@172.31.72.103
expect
{
"root@172.31.72.103's password"
{
send "passwordr"
}
}
This code is not running properly, still it is asking for the password. Can somebody please help me in solving this
ssh password expect
2
Related: pass password to su/sudo/ssh
– Piotr Dobrogost
Feb 18 '15 at 12:45
serverfault.com/questions/241588/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Nov 30 '15 at 21:23
add a comment |
I tried writing a shell script which can do automatic login into a ssh server using password which is mentioned in the script. I have written the following code:
set timeout 30
/usr/bin/ssh -p 8484 root@172.31.72.103
expect
{
"root@172.31.72.103's password"
{
send "passwordr"
}
}
This code is not running properly, still it is asking for the password. Can somebody please help me in solving this
ssh password expect
I tried writing a shell script which can do automatic login into a ssh server using password which is mentioned in the script. I have written the following code:
set timeout 30
/usr/bin/ssh -p 8484 root@172.31.72.103
expect
{
"root@172.31.72.103's password"
{
send "passwordr"
}
}
This code is not running properly, still it is asking for the password. Can somebody please help me in solving this
ssh password expect
ssh password expect
edited Feb 8 '12 at 0:10
Gilles
540k12810941609
540k12810941609
asked Feb 7 '12 at 6:17
pradeepchhetripradeepchhetri
6,31593456
6,31593456
2
Related: pass password to su/sudo/ssh
– Piotr Dobrogost
Feb 18 '15 at 12:45
serverfault.com/questions/241588/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Nov 30 '15 at 21:23
add a comment |
2
Related: pass password to su/sudo/ssh
– Piotr Dobrogost
Feb 18 '15 at 12:45
serverfault.com/questions/241588/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Nov 30 '15 at 21:23
2
2
Related: pass password to su/sudo/ssh
– Piotr Dobrogost
Feb 18 '15 at 12:45
Related: pass password to su/sudo/ssh
– Piotr Dobrogost
Feb 18 '15 at 12:45
serverfault.com/questions/241588/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Nov 30 '15 at 21:23
serverfault.com/questions/241588/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Nov 30 '15 at 21:23
add a comment |
10 Answers
10
active
oldest
votes
I once wrote an expect script to log in to a ssh server (like your case) and my script was something like this:
#!/usr/bin/expect
spawn ssh MyUserName@192.168.20.20
expect "password"
send "MyPasswordr"
interact
I think maybe the interact is missing in your script.
this is absolutely working fine for me and i am stuck in this place, the interact provides interactive terminal with prompt, but i am trying to automate further more steps like docdandlsand read the contents of the file as well. Is it possible to do after interact? Please reply
– Hansie
Aug 1 '18 at 14:57
@Hansie you can send anlscommand after the login. For example after sending password, do anexpectwith the command prompt text (to make sure you are logged in), thensend "lsr". All of these goes beforeinteract.
– saeedn
Aug 3 '18 at 0:09
the question was slightly incorrect and i am sorry after doing ls and read contents, i am able to print the same. But how to get that out of ssh session into a variable to access from local machine. ref: stackoverflow.com/questions/32341234/expect-script-return-value. But output is not working for me. My question posted: stackoverflow.com/questions/51628465/…. I needfile_listafter exit to access from local command prompt
– Hansie
Aug 3 '18 at 6:02
add a comment |
You're going about it the wrong way. What you want to do is generate a passwordless ssh-key pair and then (as long as the server supports RSA key authentication) you can get in without having to type a password for all. This is a security risk if your private key is stored somewhere that it could be stolen.
Follow these steps:
mkdir -p ~/.sshcd ~/.sshssh-keygen -type dsa -i mysshkeys- Press Return when prompted for passphrase
- Press Return a second time to confirm.
There will now be two files in your ~/.ssh directory, mysshkey.pub and mysshkey. mysshkey.pub is your public key, this one is safe to put on remote servers. mysshkey is your private passwordless key, it is not safe to put on remote servers (or somewhere someone else could get a copy).
On the server you wish to SSH into:
- Login to the remote server
mkdir -p ~/.ssh- Copy and paste the contents of
mysshkey.pubinto~/.ssh/authorized_keys
- Make sure that
~/.ssh/authorized_keysischmod'd to600
Now, to put it into action on your local machine you run the following command:
ssh -i ~/.ssh/mysshkey <remote_server_ip>
And you will be logged in without being prompted for a password.
This is a much preferable method of managing automated logins as you don't end up hard-coding your password multiple places that need to be updated if you ever change it.
2
I would use RSA keys rather than DSA. But other than that, full agree.
– glglgl
Feb 7 '12 at 9:03
9
sometimes you cannot add keys to the remote host, eg network appliances.
– DarkHeart
Apr 13 '16 at 5:07
1
I tried this. ssh is still asking for a password. Do you specifically have to give an IP address? I tried it with both <user>@<domain-name> and just the domain name.
– Jay Bienvenu
Jun 20 '17 at 14:39
This command said too many arguments? (for step 3)
– Joseph Astrahan
Aug 8 '17 at 2:59
I had to change to ssh-keygen -t dsa and manually enter file location instead.
– Joseph Astrahan
Aug 8 '17 at 3:10
|
show 3 more comments
On Debian-based distributions, the sshpass package provides an easier way of doing what you want. The package is available for many other popular distributions. You need to set it up first:
echo 'YourPassword' > passwordFile.txt
chmod 600 passwordFile.txt
Then invoke the SSH command from a script like this:
sshpass -f /path/to/passwordFile.txt /usr/bin/ssh -p 8484 root@172.31.72.103
This provides more flexibility, such as if you're using a different locale or need to change the password, than solutions using expect.
add a comment |
First install the sshPass sudo apt-get install sshpass
Then create an alias in .bashrc file as
alias sshLogin='sshpass -p <your ssh password> ssh username@remote_host'
Now reload your changed .bashrc file by source ~/.bashrc
You are now done.
Now you can run the ssh using the above created alias sshLogin in terminal.
add a comment |
you can use this:
sshpass -p 'yourpassword' ssh user@ip
add a comment |
SSH Passwordless Login Using SSH Keygen in 5 Easy Steps:
Environment setup:

Step 1: Authentication SSH-Kegen Keys on – (192.168.0.12)
First login into server 192.168.0.12 with a user and generate a pair of public keys using following command.

Step 2: Create .ssh Directory on – 192.168.0.11
Use SSH from server 192.168.0.12 to connect server 192.168.0.11 to create .ssh directory under it, using following command.

Step 3: Upload Generated Public Keys to – 192.168.0.11
Use SSH from server 192.168.0.12 and upload new generated public key (id_rsa.pub) on server 192.168.0.11 under user's .ssh directory as a file name authorized_keys.

Step 4: Set Permissions on – 192.168.0.11
Due to different SSH versions on servers, we need to set permissions on .ssh directory and authorized_keys file.

Step 5: Login from 192.168.0.12 to 192.168.0.11 Server without Password
From now onwards we can log into 192.168.0.11 as sheena user from server 192.168.0.12 as tecmint user without password.

Thanks... this worked for me, except at step 4 I got a dramatic "warning": "UNPROTECTED PRIVATE KEY FILE! ... .../.ssh/id_rsa' are too open. ... private key will be ignored... bad permissions". I didchmod 700 id_rsain directory .ssh in the local (client, i.e. 192.168.0.12 in your example) server: problem solved
– mike rodent
Apr 21 '18 at 8:14
2
Please don't post pictures of text.
– roaima
Jun 16 '18 at 22:39
add a comment |
As already described in other answers, I also use sshpass but I combine it with the read command to store my password in an temporary environment variable. This way my password is never written anywhere in clear. Here is the one line command I use:
read -s PASS; sshpass -p $PASS ssh <user>@<host adress>
After that you have to enter your password (nothing appears on the screen) and then pressing enter will open the connection.
add a comment |
All what you need it to create a hashed key and save it on your PC
Just type
ssh-keygen -t rsa -b 4096 # just press Enter till the end
then enter
ssh-copy-id <user>@<server>
then login normally using
ssh <user>@<server>
Now you don't need a password
Note: Saving your password in a plain text is dangerous
This method is creating a hashed value of your password using RSA with public key of length 4096 which is very secure.
1
This appears to be a repeat of this answer.
– roaima
Jun 16 '18 at 22:40
add a comment |
I recently did this, this may help you:
sshpass -p 'password' username@ipaddress
if this doesn't work then you'll have to generate keys in the other machine you want to connect with
ssh-keygen
it will generate private and public keys and ask you for a location, leave at empty it will save the keys in .ssh folder by default
it will ask you for passphrase, you can also leave it empty
the go in .ssh folder and change the public key name to 'authorized_keys'
cd .ssh/
mv id_rsa.pub authorized_keys
useradd -d /home/username username
this will add user to list
now go to home directory and give permission and restart sshd services
chmod 700 /home/username/.ssh
chmod 644 /home/username/.ssh/authorized_keys
chown root:root /home/dozee
sudo service sshd restart
now you will have to move the private key to the system at that location from where you are going to run the ssh command, then you can connect with
sshpass -p 'password' ssh -i id_rsa username@ip
if even that doesn't work then go in /etc/ssh open sshd_config with vim editor
check if the pubkeyAuthenticatoin is turned to yes or not, if not change it to yes , restart the sshd services and then try it, it will definitely work.
add a comment |
First argument is hostname and second is password.
#!/usr/bin/expect
set pass [lindex $argv 1]
set host [lindex $argv 0]
spawn ssh -t root@$host echo Hello
expect "*assword: "
send "$passn";
interact
Execution:
./script.expect
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f31071%2fshell-script-for-logging-into-a-ssh-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
I once wrote an expect script to log in to a ssh server (like your case) and my script was something like this:
#!/usr/bin/expect
spawn ssh MyUserName@192.168.20.20
expect "password"
send "MyPasswordr"
interact
I think maybe the interact is missing in your script.
this is absolutely working fine for me and i am stuck in this place, the interact provides interactive terminal with prompt, but i am trying to automate further more steps like docdandlsand read the contents of the file as well. Is it possible to do after interact? Please reply
– Hansie
Aug 1 '18 at 14:57
@Hansie you can send anlscommand after the login. For example after sending password, do anexpectwith the command prompt text (to make sure you are logged in), thensend "lsr". All of these goes beforeinteract.
– saeedn
Aug 3 '18 at 0:09
the question was slightly incorrect and i am sorry after doing ls and read contents, i am able to print the same. But how to get that out of ssh session into a variable to access from local machine. ref: stackoverflow.com/questions/32341234/expect-script-return-value. But output is not working for me. My question posted: stackoverflow.com/questions/51628465/…. I needfile_listafter exit to access from local command prompt
– Hansie
Aug 3 '18 at 6:02
add a comment |
I once wrote an expect script to log in to a ssh server (like your case) and my script was something like this:
#!/usr/bin/expect
spawn ssh MyUserName@192.168.20.20
expect "password"
send "MyPasswordr"
interact
I think maybe the interact is missing in your script.
this is absolutely working fine for me and i am stuck in this place, the interact provides interactive terminal with prompt, but i am trying to automate further more steps like docdandlsand read the contents of the file as well. Is it possible to do after interact? Please reply
– Hansie
Aug 1 '18 at 14:57
@Hansie you can send anlscommand after the login. For example after sending password, do anexpectwith the command prompt text (to make sure you are logged in), thensend "lsr". All of these goes beforeinteract.
– saeedn
Aug 3 '18 at 0:09
the question was slightly incorrect and i am sorry after doing ls and read contents, i am able to print the same. But how to get that out of ssh session into a variable to access from local machine. ref: stackoverflow.com/questions/32341234/expect-script-return-value. But output is not working for me. My question posted: stackoverflow.com/questions/51628465/…. I needfile_listafter exit to access from local command prompt
– Hansie
Aug 3 '18 at 6:02
add a comment |
I once wrote an expect script to log in to a ssh server (like your case) and my script was something like this:
#!/usr/bin/expect
spawn ssh MyUserName@192.168.20.20
expect "password"
send "MyPasswordr"
interact
I think maybe the interact is missing in your script.
I once wrote an expect script to log in to a ssh server (like your case) and my script was something like this:
#!/usr/bin/expect
spawn ssh MyUserName@192.168.20.20
expect "password"
send "MyPasswordr"
interact
I think maybe the interact is missing in your script.
answered Feb 7 '12 at 9:43
saeednsaeedn
1,71411315
1,71411315
this is absolutely working fine for me and i am stuck in this place, the interact provides interactive terminal with prompt, but i am trying to automate further more steps like docdandlsand read the contents of the file as well. Is it possible to do after interact? Please reply
– Hansie
Aug 1 '18 at 14:57
@Hansie you can send anlscommand after the login. For example after sending password, do anexpectwith the command prompt text (to make sure you are logged in), thensend "lsr". All of these goes beforeinteract.
– saeedn
Aug 3 '18 at 0:09
the question was slightly incorrect and i am sorry after doing ls and read contents, i am able to print the same. But how to get that out of ssh session into a variable to access from local machine. ref: stackoverflow.com/questions/32341234/expect-script-return-value. But output is not working for me. My question posted: stackoverflow.com/questions/51628465/…. I needfile_listafter exit to access from local command prompt
– Hansie
Aug 3 '18 at 6:02
add a comment |
this is absolutely working fine for me and i am stuck in this place, the interact provides interactive terminal with prompt, but i am trying to automate further more steps like docdandlsand read the contents of the file as well. Is it possible to do after interact? Please reply
– Hansie
Aug 1 '18 at 14:57
@Hansie you can send anlscommand after the login. For example after sending password, do anexpectwith the command prompt text (to make sure you are logged in), thensend "lsr". All of these goes beforeinteract.
– saeedn
Aug 3 '18 at 0:09
the question was slightly incorrect and i am sorry after doing ls and read contents, i am able to print the same. But how to get that out of ssh session into a variable to access from local machine. ref: stackoverflow.com/questions/32341234/expect-script-return-value. But output is not working for me. My question posted: stackoverflow.com/questions/51628465/…. I needfile_listafter exit to access from local command prompt
– Hansie
Aug 3 '18 at 6:02
this is absolutely working fine for me and i am stuck in this place, the interact provides interactive terminal with prompt, but i am trying to automate further more steps like do
cd and ls and read the contents of the file as well. Is it possible to do after interact? Please reply– Hansie
Aug 1 '18 at 14:57
this is absolutely working fine for me and i am stuck in this place, the interact provides interactive terminal with prompt, but i am trying to automate further more steps like do
cd and ls and read the contents of the file as well. Is it possible to do after interact? Please reply– Hansie
Aug 1 '18 at 14:57
@Hansie you can send an
ls command after the login. For example after sending password, do an expect with the command prompt text (to make sure you are logged in), then send "lsr". All of these goes before interact.– saeedn
Aug 3 '18 at 0:09
@Hansie you can send an
ls command after the login. For example after sending password, do an expect with the command prompt text (to make sure you are logged in), then send "lsr". All of these goes before interact.– saeedn
Aug 3 '18 at 0:09
the question was slightly incorrect and i am sorry after doing ls and read contents, i am able to print the same. But how to get that out of ssh session into a variable to access from local machine. ref: stackoverflow.com/questions/32341234/expect-script-return-value. But output is not working for me. My question posted: stackoverflow.com/questions/51628465/…. I need
file_list after exit to access from local command prompt– Hansie
Aug 3 '18 at 6:02
the question was slightly incorrect and i am sorry after doing ls and read contents, i am able to print the same. But how to get that out of ssh session into a variable to access from local machine. ref: stackoverflow.com/questions/32341234/expect-script-return-value. But output is not working for me. My question posted: stackoverflow.com/questions/51628465/…. I need
file_list after exit to access from local command prompt– Hansie
Aug 3 '18 at 6:02
add a comment |
You're going about it the wrong way. What you want to do is generate a passwordless ssh-key pair and then (as long as the server supports RSA key authentication) you can get in without having to type a password for all. This is a security risk if your private key is stored somewhere that it could be stolen.
Follow these steps:
mkdir -p ~/.sshcd ~/.sshssh-keygen -type dsa -i mysshkeys- Press Return when prompted for passphrase
- Press Return a second time to confirm.
There will now be two files in your ~/.ssh directory, mysshkey.pub and mysshkey. mysshkey.pub is your public key, this one is safe to put on remote servers. mysshkey is your private passwordless key, it is not safe to put on remote servers (or somewhere someone else could get a copy).
On the server you wish to SSH into:
- Login to the remote server
mkdir -p ~/.ssh- Copy and paste the contents of
mysshkey.pubinto~/.ssh/authorized_keys
- Make sure that
~/.ssh/authorized_keysischmod'd to600
Now, to put it into action on your local machine you run the following command:
ssh -i ~/.ssh/mysshkey <remote_server_ip>
And you will be logged in without being prompted for a password.
This is a much preferable method of managing automated logins as you don't end up hard-coding your password multiple places that need to be updated if you ever change it.
2
I would use RSA keys rather than DSA. But other than that, full agree.
– glglgl
Feb 7 '12 at 9:03
9
sometimes you cannot add keys to the remote host, eg network appliances.
– DarkHeart
Apr 13 '16 at 5:07
1
I tried this. ssh is still asking for a password. Do you specifically have to give an IP address? I tried it with both <user>@<domain-name> and just the domain name.
– Jay Bienvenu
Jun 20 '17 at 14:39
This command said too many arguments? (for step 3)
– Joseph Astrahan
Aug 8 '17 at 2:59
I had to change to ssh-keygen -t dsa and manually enter file location instead.
– Joseph Astrahan
Aug 8 '17 at 3:10
|
show 3 more comments
You're going about it the wrong way. What you want to do is generate a passwordless ssh-key pair and then (as long as the server supports RSA key authentication) you can get in without having to type a password for all. This is a security risk if your private key is stored somewhere that it could be stolen.
Follow these steps:
mkdir -p ~/.sshcd ~/.sshssh-keygen -type dsa -i mysshkeys- Press Return when prompted for passphrase
- Press Return a second time to confirm.
There will now be two files in your ~/.ssh directory, mysshkey.pub and mysshkey. mysshkey.pub is your public key, this one is safe to put on remote servers. mysshkey is your private passwordless key, it is not safe to put on remote servers (or somewhere someone else could get a copy).
On the server you wish to SSH into:
- Login to the remote server
mkdir -p ~/.ssh- Copy and paste the contents of
mysshkey.pubinto~/.ssh/authorized_keys
- Make sure that
~/.ssh/authorized_keysischmod'd to600
Now, to put it into action on your local machine you run the following command:
ssh -i ~/.ssh/mysshkey <remote_server_ip>
And you will be logged in without being prompted for a password.
This is a much preferable method of managing automated logins as you don't end up hard-coding your password multiple places that need to be updated if you ever change it.
2
I would use RSA keys rather than DSA. But other than that, full agree.
– glglgl
Feb 7 '12 at 9:03
9
sometimes you cannot add keys to the remote host, eg network appliances.
– DarkHeart
Apr 13 '16 at 5:07
1
I tried this. ssh is still asking for a password. Do you specifically have to give an IP address? I tried it with both <user>@<domain-name> and just the domain name.
– Jay Bienvenu
Jun 20 '17 at 14:39
This command said too many arguments? (for step 3)
– Joseph Astrahan
Aug 8 '17 at 2:59
I had to change to ssh-keygen -t dsa and manually enter file location instead.
– Joseph Astrahan
Aug 8 '17 at 3:10
|
show 3 more comments
You're going about it the wrong way. What you want to do is generate a passwordless ssh-key pair and then (as long as the server supports RSA key authentication) you can get in without having to type a password for all. This is a security risk if your private key is stored somewhere that it could be stolen.
Follow these steps:
mkdir -p ~/.sshcd ~/.sshssh-keygen -type dsa -i mysshkeys- Press Return when prompted for passphrase
- Press Return a second time to confirm.
There will now be two files in your ~/.ssh directory, mysshkey.pub and mysshkey. mysshkey.pub is your public key, this one is safe to put on remote servers. mysshkey is your private passwordless key, it is not safe to put on remote servers (or somewhere someone else could get a copy).
On the server you wish to SSH into:
- Login to the remote server
mkdir -p ~/.ssh- Copy and paste the contents of
mysshkey.pubinto~/.ssh/authorized_keys
- Make sure that
~/.ssh/authorized_keysischmod'd to600
Now, to put it into action on your local machine you run the following command:
ssh -i ~/.ssh/mysshkey <remote_server_ip>
And you will be logged in without being prompted for a password.
This is a much preferable method of managing automated logins as you don't end up hard-coding your password multiple places that need to be updated if you ever change it.
You're going about it the wrong way. What you want to do is generate a passwordless ssh-key pair and then (as long as the server supports RSA key authentication) you can get in without having to type a password for all. This is a security risk if your private key is stored somewhere that it could be stolen.
Follow these steps:
mkdir -p ~/.sshcd ~/.sshssh-keygen -type dsa -i mysshkeys- Press Return when prompted for passphrase
- Press Return a second time to confirm.
There will now be two files in your ~/.ssh directory, mysshkey.pub and mysshkey. mysshkey.pub is your public key, this one is safe to put on remote servers. mysshkey is your private passwordless key, it is not safe to put on remote servers (or somewhere someone else could get a copy).
On the server you wish to SSH into:
- Login to the remote server
mkdir -p ~/.ssh- Copy and paste the contents of
mysshkey.pubinto~/.ssh/authorized_keys
- Make sure that
~/.ssh/authorized_keysischmod'd to600
Now, to put it into action on your local machine you run the following command:
ssh -i ~/.ssh/mysshkey <remote_server_ip>
And you will be logged in without being prompted for a password.
This is a much preferable method of managing automated logins as you don't end up hard-coding your password multiple places that need to be updated if you ever change it.
edited Nov 11 '15 at 14:30
Steven Linn
1034
1034
answered Feb 7 '12 at 7:38
synthesizerpatelsynthesizerpatel
494613
494613
2
I would use RSA keys rather than DSA. But other than that, full agree.
– glglgl
Feb 7 '12 at 9:03
9
sometimes you cannot add keys to the remote host, eg network appliances.
– DarkHeart
Apr 13 '16 at 5:07
1
I tried this. ssh is still asking for a password. Do you specifically have to give an IP address? I tried it with both <user>@<domain-name> and just the domain name.
– Jay Bienvenu
Jun 20 '17 at 14:39
This command said too many arguments? (for step 3)
– Joseph Astrahan
Aug 8 '17 at 2:59
I had to change to ssh-keygen -t dsa and manually enter file location instead.
– Joseph Astrahan
Aug 8 '17 at 3:10
|
show 3 more comments
2
I would use RSA keys rather than DSA. But other than that, full agree.
– glglgl
Feb 7 '12 at 9:03
9
sometimes you cannot add keys to the remote host, eg network appliances.
– DarkHeart
Apr 13 '16 at 5:07
1
I tried this. ssh is still asking for a password. Do you specifically have to give an IP address? I tried it with both <user>@<domain-name> and just the domain name.
– Jay Bienvenu
Jun 20 '17 at 14:39
This command said too many arguments? (for step 3)
– Joseph Astrahan
Aug 8 '17 at 2:59
I had to change to ssh-keygen -t dsa and manually enter file location instead.
– Joseph Astrahan
Aug 8 '17 at 3:10
2
2
I would use RSA keys rather than DSA. But other than that, full agree.
– glglgl
Feb 7 '12 at 9:03
I would use RSA keys rather than DSA. But other than that, full agree.
– glglgl
Feb 7 '12 at 9:03
9
9
sometimes you cannot add keys to the remote host, eg network appliances.
– DarkHeart
Apr 13 '16 at 5:07
sometimes you cannot add keys to the remote host, eg network appliances.
– DarkHeart
Apr 13 '16 at 5:07
1
1
I tried this. ssh is still asking for a password. Do you specifically have to give an IP address? I tried it with both <user>@<domain-name> and just the domain name.
– Jay Bienvenu
Jun 20 '17 at 14:39
I tried this. ssh is still asking for a password. Do you specifically have to give an IP address? I tried it with both <user>@<domain-name> and just the domain name.
– Jay Bienvenu
Jun 20 '17 at 14:39
This command said too many arguments? (for step 3)
– Joseph Astrahan
Aug 8 '17 at 2:59
This command said too many arguments? (for step 3)
– Joseph Astrahan
Aug 8 '17 at 2:59
I had to change to ssh-keygen -t dsa and manually enter file location instead.
– Joseph Astrahan
Aug 8 '17 at 3:10
I had to change to ssh-keygen -t dsa and manually enter file location instead.
– Joseph Astrahan
Aug 8 '17 at 3:10
|
show 3 more comments
On Debian-based distributions, the sshpass package provides an easier way of doing what you want. The package is available for many other popular distributions. You need to set it up first:
echo 'YourPassword' > passwordFile.txt
chmod 600 passwordFile.txt
Then invoke the SSH command from a script like this:
sshpass -f /path/to/passwordFile.txt /usr/bin/ssh -p 8484 root@172.31.72.103
This provides more flexibility, such as if you're using a different locale or need to change the password, than solutions using expect.
add a comment |
On Debian-based distributions, the sshpass package provides an easier way of doing what you want. The package is available for many other popular distributions. You need to set it up first:
echo 'YourPassword' > passwordFile.txt
chmod 600 passwordFile.txt
Then invoke the SSH command from a script like this:
sshpass -f /path/to/passwordFile.txt /usr/bin/ssh -p 8484 root@172.31.72.103
This provides more flexibility, such as if you're using a different locale or need to change the password, than solutions using expect.
add a comment |
On Debian-based distributions, the sshpass package provides an easier way of doing what you want. The package is available for many other popular distributions. You need to set it up first:
echo 'YourPassword' > passwordFile.txt
chmod 600 passwordFile.txt
Then invoke the SSH command from a script like this:
sshpass -f /path/to/passwordFile.txt /usr/bin/ssh -p 8484 root@172.31.72.103
This provides more flexibility, such as if you're using a different locale or need to change the password, than solutions using expect.
On Debian-based distributions, the sshpass package provides an easier way of doing what you want. The package is available for many other popular distributions. You need to set it up first:
echo 'YourPassword' > passwordFile.txt
chmod 600 passwordFile.txt
Then invoke the SSH command from a script like this:
sshpass -f /path/to/passwordFile.txt /usr/bin/ssh -p 8484 root@172.31.72.103
This provides more flexibility, such as if you're using a different locale or need to change the password, than solutions using expect.
edited Jun 11 '15 at 0:53
terdon♦
131k32258437
131k32258437
answered Dec 28 '12 at 2:02
likeitlikeitlikeitlikeit
1,621196
1,621196
add a comment |
add a comment |
First install the sshPass sudo apt-get install sshpass
Then create an alias in .bashrc file as
alias sshLogin='sshpass -p <your ssh password> ssh username@remote_host'
Now reload your changed .bashrc file by source ~/.bashrc
You are now done.
Now you can run the ssh using the above created alias sshLogin in terminal.
add a comment |
First install the sshPass sudo apt-get install sshpass
Then create an alias in .bashrc file as
alias sshLogin='sshpass -p <your ssh password> ssh username@remote_host'
Now reload your changed .bashrc file by source ~/.bashrc
You are now done.
Now you can run the ssh using the above created alias sshLogin in terminal.
add a comment |
First install the sshPass sudo apt-get install sshpass
Then create an alias in .bashrc file as
alias sshLogin='sshpass -p <your ssh password> ssh username@remote_host'
Now reload your changed .bashrc file by source ~/.bashrc
You are now done.
Now you can run the ssh using the above created alias sshLogin in terminal.
First install the sshPass sudo apt-get install sshpass
Then create an alias in .bashrc file as
alias sshLogin='sshpass -p <your ssh password> ssh username@remote_host'
Now reload your changed .bashrc file by source ~/.bashrc
You are now done.
Now you can run the ssh using the above created alias sshLogin in terminal.
edited Aug 17 '18 at 12:57
Anthony Giorgio
1034
1034
answered Nov 28 '16 at 8:42
bpathakbpathak
5111
5111
add a comment |
add a comment |
you can use this:
sshpass -p 'yourpassword' ssh user@ip
add a comment |
you can use this:
sshpass -p 'yourpassword' ssh user@ip
add a comment |
you can use this:
sshpass -p 'yourpassword' ssh user@ip
you can use this:
sshpass -p 'yourpassword' ssh user@ip
edited Jan 29 '18 at 19:10
terdon♦
131k32258437
131k32258437
answered Nov 23 '16 at 12:08
Behrooz Mohamadi nasabBehrooz Mohamadi nasab
553
553
add a comment |
add a comment |
SSH Passwordless Login Using SSH Keygen in 5 Easy Steps:
Environment setup:

Step 1: Authentication SSH-Kegen Keys on – (192.168.0.12)
First login into server 192.168.0.12 with a user and generate a pair of public keys using following command.

Step 2: Create .ssh Directory on – 192.168.0.11
Use SSH from server 192.168.0.12 to connect server 192.168.0.11 to create .ssh directory under it, using following command.

Step 3: Upload Generated Public Keys to – 192.168.0.11
Use SSH from server 192.168.0.12 and upload new generated public key (id_rsa.pub) on server 192.168.0.11 under user's .ssh directory as a file name authorized_keys.

Step 4: Set Permissions on – 192.168.0.11
Due to different SSH versions on servers, we need to set permissions on .ssh directory and authorized_keys file.

Step 5: Login from 192.168.0.12 to 192.168.0.11 Server without Password
From now onwards we can log into 192.168.0.11 as sheena user from server 192.168.0.12 as tecmint user without password.

Thanks... this worked for me, except at step 4 I got a dramatic "warning": "UNPROTECTED PRIVATE KEY FILE! ... .../.ssh/id_rsa' are too open. ... private key will be ignored... bad permissions". I didchmod 700 id_rsain directory .ssh in the local (client, i.e. 192.168.0.12 in your example) server: problem solved
– mike rodent
Apr 21 '18 at 8:14
2
Please don't post pictures of text.
– roaima
Jun 16 '18 at 22:39
add a comment |
SSH Passwordless Login Using SSH Keygen in 5 Easy Steps:
Environment setup:

Step 1: Authentication SSH-Kegen Keys on – (192.168.0.12)
First login into server 192.168.0.12 with a user and generate a pair of public keys using following command.

Step 2: Create .ssh Directory on – 192.168.0.11
Use SSH from server 192.168.0.12 to connect server 192.168.0.11 to create .ssh directory under it, using following command.

Step 3: Upload Generated Public Keys to – 192.168.0.11
Use SSH from server 192.168.0.12 and upload new generated public key (id_rsa.pub) on server 192.168.0.11 under user's .ssh directory as a file name authorized_keys.

Step 4: Set Permissions on – 192.168.0.11
Due to different SSH versions on servers, we need to set permissions on .ssh directory and authorized_keys file.

Step 5: Login from 192.168.0.12 to 192.168.0.11 Server without Password
From now onwards we can log into 192.168.0.11 as sheena user from server 192.168.0.12 as tecmint user without password.

Thanks... this worked for me, except at step 4 I got a dramatic "warning": "UNPROTECTED PRIVATE KEY FILE! ... .../.ssh/id_rsa' are too open. ... private key will be ignored... bad permissions". I didchmod 700 id_rsain directory .ssh in the local (client, i.e. 192.168.0.12 in your example) server: problem solved
– mike rodent
Apr 21 '18 at 8:14
2
Please don't post pictures of text.
– roaima
Jun 16 '18 at 22:39
add a comment |
SSH Passwordless Login Using SSH Keygen in 5 Easy Steps:
Environment setup:

Step 1: Authentication SSH-Kegen Keys on – (192.168.0.12)
First login into server 192.168.0.12 with a user and generate a pair of public keys using following command.

Step 2: Create .ssh Directory on – 192.168.0.11
Use SSH from server 192.168.0.12 to connect server 192.168.0.11 to create .ssh directory under it, using following command.

Step 3: Upload Generated Public Keys to – 192.168.0.11
Use SSH from server 192.168.0.12 and upload new generated public key (id_rsa.pub) on server 192.168.0.11 under user's .ssh directory as a file name authorized_keys.

Step 4: Set Permissions on – 192.168.0.11
Due to different SSH versions on servers, we need to set permissions on .ssh directory and authorized_keys file.

Step 5: Login from 192.168.0.12 to 192.168.0.11 Server without Password
From now onwards we can log into 192.168.0.11 as sheena user from server 192.168.0.12 as tecmint user without password.

SSH Passwordless Login Using SSH Keygen in 5 Easy Steps:
Environment setup:

Step 1: Authentication SSH-Kegen Keys on – (192.168.0.12)
First login into server 192.168.0.12 with a user and generate a pair of public keys using following command.

Step 2: Create .ssh Directory on – 192.168.0.11
Use SSH from server 192.168.0.12 to connect server 192.168.0.11 to create .ssh directory under it, using following command.

Step 3: Upload Generated Public Keys to – 192.168.0.11
Use SSH from server 192.168.0.12 and upload new generated public key (id_rsa.pub) on server 192.168.0.11 under user's .ssh directory as a file name authorized_keys.

Step 4: Set Permissions on – 192.168.0.11
Due to different SSH versions on servers, we need to set permissions on .ssh directory and authorized_keys file.

Step 5: Login from 192.168.0.12 to 192.168.0.11 Server without Password
From now onwards we can log into 192.168.0.11 as sheena user from server 192.168.0.12 as tecmint user without password.

answered Sep 15 '17 at 0:47
PremrajPremraj
1,10011117
1,10011117
Thanks... this worked for me, except at step 4 I got a dramatic "warning": "UNPROTECTED PRIVATE KEY FILE! ... .../.ssh/id_rsa' are too open. ... private key will be ignored... bad permissions". I didchmod 700 id_rsain directory .ssh in the local (client, i.e. 192.168.0.12 in your example) server: problem solved
– mike rodent
Apr 21 '18 at 8:14
2
Please don't post pictures of text.
– roaima
Jun 16 '18 at 22:39
add a comment |
Thanks... this worked for me, except at step 4 I got a dramatic "warning": "UNPROTECTED PRIVATE KEY FILE! ... .../.ssh/id_rsa' are too open. ... private key will be ignored... bad permissions". I didchmod 700 id_rsain directory .ssh in the local (client, i.e. 192.168.0.12 in your example) server: problem solved
– mike rodent
Apr 21 '18 at 8:14
2
Please don't post pictures of text.
– roaima
Jun 16 '18 at 22:39
Thanks... this worked for me, except at step 4 I got a dramatic "warning": "UNPROTECTED PRIVATE KEY FILE! ... .../.ssh/id_rsa' are too open. ... private key will be ignored... bad permissions". I did
chmod 700 id_rsa in directory .ssh in the local (client, i.e. 192.168.0.12 in your example) server: problem solved– mike rodent
Apr 21 '18 at 8:14
Thanks... this worked for me, except at step 4 I got a dramatic "warning": "UNPROTECTED PRIVATE KEY FILE! ... .../.ssh/id_rsa' are too open. ... private key will be ignored... bad permissions". I did
chmod 700 id_rsa in directory .ssh in the local (client, i.e. 192.168.0.12 in your example) server: problem solved– mike rodent
Apr 21 '18 at 8:14
2
2
Please don't post pictures of text.
– roaima
Jun 16 '18 at 22:39
Please don't post pictures of text.
– roaima
Jun 16 '18 at 22:39
add a comment |
As already described in other answers, I also use sshpass but I combine it with the read command to store my password in an temporary environment variable. This way my password is never written anywhere in clear. Here is the one line command I use:
read -s PASS; sshpass -p $PASS ssh <user>@<host adress>
After that you have to enter your password (nothing appears on the screen) and then pressing enter will open the connection.
add a comment |
As already described in other answers, I also use sshpass but I combine it with the read command to store my password in an temporary environment variable. This way my password is never written anywhere in clear. Here is the one line command I use:
read -s PASS; sshpass -p $PASS ssh <user>@<host adress>
After that you have to enter your password (nothing appears on the screen) and then pressing enter will open the connection.
add a comment |
As already described in other answers, I also use sshpass but I combine it with the read command to store my password in an temporary environment variable. This way my password is never written anywhere in clear. Here is the one line command I use:
read -s PASS; sshpass -p $PASS ssh <user>@<host adress>
After that you have to enter your password (nothing appears on the screen) and then pressing enter will open the connection.
As already described in other answers, I also use sshpass but I combine it with the read command to store my password in an temporary environment variable. This way my password is never written anywhere in clear. Here is the one line command I use:
read -s PASS; sshpass -p $PASS ssh <user>@<host adress>
After that you have to enter your password (nothing appears on the screen) and then pressing enter will open the connection.
answered May 31 '18 at 19:48
StormRiderStormRider
211
211
add a comment |
add a comment |
All what you need it to create a hashed key and save it on your PC
Just type
ssh-keygen -t rsa -b 4096 # just press Enter till the end
then enter
ssh-copy-id <user>@<server>
then login normally using
ssh <user>@<server>
Now you don't need a password
Note: Saving your password in a plain text is dangerous
This method is creating a hashed value of your password using RSA with public key of length 4096 which is very secure.
1
This appears to be a repeat of this answer.
– roaima
Jun 16 '18 at 22:40
add a comment |
All what you need it to create a hashed key and save it on your PC
Just type
ssh-keygen -t rsa -b 4096 # just press Enter till the end
then enter
ssh-copy-id <user>@<server>
then login normally using
ssh <user>@<server>
Now you don't need a password
Note: Saving your password in a plain text is dangerous
This method is creating a hashed value of your password using RSA with public key of length 4096 which is very secure.
1
This appears to be a repeat of this answer.
– roaima
Jun 16 '18 at 22:40
add a comment |
All what you need it to create a hashed key and save it on your PC
Just type
ssh-keygen -t rsa -b 4096 # just press Enter till the end
then enter
ssh-copy-id <user>@<server>
then login normally using
ssh <user>@<server>
Now you don't need a password
Note: Saving your password in a plain text is dangerous
This method is creating a hashed value of your password using RSA with public key of length 4096 which is very secure.
All what you need it to create a hashed key and save it on your PC
Just type
ssh-keygen -t rsa -b 4096 # just press Enter till the end
then enter
ssh-copy-id <user>@<server>
then login normally using
ssh <user>@<server>
Now you don't need a password
Note: Saving your password in a plain text is dangerous
This method is creating a hashed value of your password using RSA with public key of length 4096 which is very secure.
edited Jun 17 '18 at 2:17
slm♦
253k70534685
253k70534685
answered Jun 16 '18 at 22:24
Mohammad HizzaniMohammad Hizzani
113
113
1
This appears to be a repeat of this answer.
– roaima
Jun 16 '18 at 22:40
add a comment |
1
This appears to be a repeat of this answer.
– roaima
Jun 16 '18 at 22:40
1
1
This appears to be a repeat of this answer.
– roaima
Jun 16 '18 at 22:40
This appears to be a repeat of this answer.
– roaima
Jun 16 '18 at 22:40
add a comment |
I recently did this, this may help you:
sshpass -p 'password' username@ipaddress
if this doesn't work then you'll have to generate keys in the other machine you want to connect with
ssh-keygen
it will generate private and public keys and ask you for a location, leave at empty it will save the keys in .ssh folder by default
it will ask you for passphrase, you can also leave it empty
the go in .ssh folder and change the public key name to 'authorized_keys'
cd .ssh/
mv id_rsa.pub authorized_keys
useradd -d /home/username username
this will add user to list
now go to home directory and give permission and restart sshd services
chmod 700 /home/username/.ssh
chmod 644 /home/username/.ssh/authorized_keys
chown root:root /home/dozee
sudo service sshd restart
now you will have to move the private key to the system at that location from where you are going to run the ssh command, then you can connect with
sshpass -p 'password' ssh -i id_rsa username@ip
if even that doesn't work then go in /etc/ssh open sshd_config with vim editor
check if the pubkeyAuthenticatoin is turned to yes or not, if not change it to yes , restart the sshd services and then try it, it will definitely work.
add a comment |
I recently did this, this may help you:
sshpass -p 'password' username@ipaddress
if this doesn't work then you'll have to generate keys in the other machine you want to connect with
ssh-keygen
it will generate private and public keys and ask you for a location, leave at empty it will save the keys in .ssh folder by default
it will ask you for passphrase, you can also leave it empty
the go in .ssh folder and change the public key name to 'authorized_keys'
cd .ssh/
mv id_rsa.pub authorized_keys
useradd -d /home/username username
this will add user to list
now go to home directory and give permission and restart sshd services
chmod 700 /home/username/.ssh
chmod 644 /home/username/.ssh/authorized_keys
chown root:root /home/dozee
sudo service sshd restart
now you will have to move the private key to the system at that location from where you are going to run the ssh command, then you can connect with
sshpass -p 'password' ssh -i id_rsa username@ip
if even that doesn't work then go in /etc/ssh open sshd_config with vim editor
check if the pubkeyAuthenticatoin is turned to yes or not, if not change it to yes , restart the sshd services and then try it, it will definitely work.
add a comment |
I recently did this, this may help you:
sshpass -p 'password' username@ipaddress
if this doesn't work then you'll have to generate keys in the other machine you want to connect with
ssh-keygen
it will generate private and public keys and ask you for a location, leave at empty it will save the keys in .ssh folder by default
it will ask you for passphrase, you can also leave it empty
the go in .ssh folder and change the public key name to 'authorized_keys'
cd .ssh/
mv id_rsa.pub authorized_keys
useradd -d /home/username username
this will add user to list
now go to home directory and give permission and restart sshd services
chmod 700 /home/username/.ssh
chmod 644 /home/username/.ssh/authorized_keys
chown root:root /home/dozee
sudo service sshd restart
now you will have to move the private key to the system at that location from where you are going to run the ssh command, then you can connect with
sshpass -p 'password' ssh -i id_rsa username@ip
if even that doesn't work then go in /etc/ssh open sshd_config with vim editor
check if the pubkeyAuthenticatoin is turned to yes or not, if not change it to yes , restart the sshd services and then try it, it will definitely work.
I recently did this, this may help you:
sshpass -p 'password' username@ipaddress
if this doesn't work then you'll have to generate keys in the other machine you want to connect with
ssh-keygen
it will generate private and public keys and ask you for a location, leave at empty it will save the keys in .ssh folder by default
it will ask you for passphrase, you can also leave it empty
the go in .ssh folder and change the public key name to 'authorized_keys'
cd .ssh/
mv id_rsa.pub authorized_keys
useradd -d /home/username username
this will add user to list
now go to home directory and give permission and restart sshd services
chmod 700 /home/username/.ssh
chmod 644 /home/username/.ssh/authorized_keys
chown root:root /home/dozee
sudo service sshd restart
now you will have to move the private key to the system at that location from where you are going to run the ssh command, then you can connect with
sshpass -p 'password' ssh -i id_rsa username@ip
if even that doesn't work then go in /etc/ssh open sshd_config with vim editor
check if the pubkeyAuthenticatoin is turned to yes or not, if not change it to yes , restart the sshd services and then try it, it will definitely work.
answered May 30 '18 at 18:37
Utkarsh GuptaUtkarsh Gupta
1
1
add a comment |
add a comment |
First argument is hostname and second is password.
#!/usr/bin/expect
set pass [lindex $argv 1]
set host [lindex $argv 0]
spawn ssh -t root@$host echo Hello
expect "*assword: "
send "$passn";
interact
Execution:
./script.expect
add a comment |
First argument is hostname and second is password.
#!/usr/bin/expect
set pass [lindex $argv 1]
set host [lindex $argv 0]
spawn ssh -t root@$host echo Hello
expect "*assword: "
send "$passn";
interact
Execution:
./script.expect
add a comment |
First argument is hostname and second is password.
#!/usr/bin/expect
set pass [lindex $argv 1]
set host [lindex $argv 0]
spawn ssh -t root@$host echo Hello
expect "*assword: "
send "$passn";
interact
Execution:
./script.expect
First argument is hostname and second is password.
#!/usr/bin/expect
set pass [lindex $argv 1]
set host [lindex $argv 0]
spawn ssh -t root@$host echo Hello
expect "*assword: "
send "$passn";
interact
Execution:
./script.expect
answered Feb 18 at 11:12
Shivam MehrotraShivam Mehrotra
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f31071%2fshell-script-for-logging-into-a-ssh-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Related: pass password to su/sudo/ssh
– Piotr Dobrogost
Feb 18 '15 at 12:45
serverfault.com/questions/241588/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Nov 30 '15 at 21:23