• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » Bash Script for SSH Login with Password

By Abhishek Ghosh August 27, 2022 8:07 pm Updated on August 27, 2022

Bash Script for SSH Login with Password

Advertisement

The title “Bash Script for SSH Login with Password” means executing a bash script so you can directly log in to the server without the need to type the SSH command or password. A bash script to automate the SSH login is helpful (for certain automation) and avoids the need for key based login setup. However, we are describing the methodologies to automate SSH login for things such as Raspberry Pi. The methods for automating SSH login in these manners are not for use on unmanaged web servers (such as one running PHP, Apache2, MySQL, or WordPress and having a public IP address).

 

Bash Script for SSH Login

 

The easiest method is the below:

Vim
1
2
3
4
5
6
7
8
9
10
11
# !/bin/bash
 
read -p 'Username: ' user
read -sp 'Password: ' pass
 
if (( $user == "rootuser" && $pass == "rootpass" ))
then
echo -e "\nWelcome! You are logged in\n"
else
echo -e "\nUnsuccessful login attempt\n"
fi

Now do the steps to execute the script:

Advertisement

---

Vim
1
2
3
4
chmod +x login.sh
sh login.sh
# or
# ./login.sh

That is probably prone to failure in different situations. Another method is by installing a package named expect :

Vim
1
2
apt install expect -y
# dnf install expect

The easiest script I know is like the one below:

login.sh
Vim
1
2
3
4
5
6
#!/usr/bin/expect -f
spawn ssh linuxhint@192.168.1.103
expect "Password:*"
send "yourpassword\r"
expect "$ "
interact

yourpassword written above is your real password. You have to make it executable and run:

Vim
1
2
3
4
chmod +x login.sh
sh login.sh
# or
# ./login.sh

It works great for known hosts. However, it can not handle additional dialogues. I found the below script described by www.golinuxcloud.com which addresses that issue :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/expect
set USER [lindex $argv 0]
set HOST [lindex $argv 1]
set PWD [lindex $argv 2]
log_file /var/log/ssh_tmp.log
set timeout  30
log_user  1
set send_slow {1 .01}
send_log  "Connecting to $HOST using $USER user\n"
eval spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o Connecttimeout =30 "$USER\@$HOST"
expect  {
       timeout       { send_user  "timeout  while connecting to $HOST\n"; exit }
       "*No route to host*" { send_user  "$HOST not reachable\n"; exit }
         "*assword: " { send -s $PWD\r }
        }
expect  {
         timeout  { send_user  "timeout  waiting for prompt\n"; exit }
         "*]#"   { send_user  "Login successful to $HOST\n" }
        }
send "hostname\r"
expect  {
         "*]#"   { send "exit\r" }
        }
send_user  "Disconnected\n"
close

I have kept the script on GitHub as gist for my usage. You can wget it and use it.

Another method described to achieve a similar result is by installing a package named sshpass. The advantage of the SSH Pass package is that you can use it with key-based login too.

Vim
1
2
3
echo 'YourPassword' > passwordFile.txt
chmod 600 passwordFile.txt
sshpass -f /path/to/passwordFile.txt /usr/bin/ssh -p 8484 root@111.22.33.444

However, if you want key-based login, then perform the below steps.

Vim
1
2
3
mkdir -p ~/.ssh
cd ~/.ssh
ssh-keygen -type dsa -i mysshkeys

Press Return when prompted for passphrase. Press Return a second time to confirm. There will be two files in the ~/.ssh directory, mysshkey.pub and mysshkey. mysshkey.pub is the public key, you’ll put this on remote servers. mysshkey is your private key.

Bash Script for SSH Login with Password

On the server you wish to SSH into. Login to the remote server:

Vim
1
2
3
4
5
mkdir -p ~/.ssh
# Copy and paste the contents of mysshkey.pub into ~/.ssh/authorized_keys
# Make sure that ~/.ssh/authorized_keys is chmod'd to 600
# Now, 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. The last method is the safest one.

Tagged With bash script for ssh login with password
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Bash Script for SSH Login with Password

  • WordPress & PHP : Different AdSense Units on Mobile Devices

    Here is How To Serve Different AdSense Units on Mobile Devices on WordPress With PHP. WordPress Has Function Which Can Be Used In Free Way.

  • Adding Help & Option in Bash Script

    Many New Hobbyist Programmers and Students Write Bash Scripts. Adding Help & Option in Bash Script is Easy. Here is Basic Guide With Example.

  • Run Bash Scripts on Windows 10 Via Git Bash To Launch Linux GUI App

    Run Bash Scripts on Windows 10 Via Git Bash. Git Bash Offers Integrity Options With Windows 10 Bash Making it Easy to Work on Both Windows & unix System.

  • How passwords work maintaining your online security

    How passwords work? How hackers crack the passwords to invade systems? How the encryption of passwords are done? Everything explained regarding passwords.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy