bat, shell script ssh automatically enters the password

My computer is win11, and it comes with ubuntu system, open cmd, and then enter ubuntu, you can directly enter the ubuntu system, and then execute simple linux commands. I have written scripts to automatically connect to remote services under both systems.

Through ssh remote connection, if the remote connection server can be set to password-free login. Then the script would be as simple as

ssh [email protected]

For example, the server I want to connect to is 192.168.20.9. User is root, password is 123456

1. If it is in the cmd environment, then I write a ssh209.bat file with the content:

ssh [email protected]

Then save and add its path to the environment variable. Then if you execute ssh209 in any path, you can directly enter this 20.9 server.

2. If it is under ubuntu, then I write a ssh209 file with the content:

#!/bin/sh
ssh [email protected]

Then save it under /usr/bin, then you can enter this server by executing ssh209 in any path.

But if you cannot set password-free login, then you need to add an automatic input function to realize automatic password input.

1. If it is in the cmd environment, you need to write a vbs script to achieve it.

Set ws = CreateObject("WScript.Shell")
ws.run "ssh [email protected]"
wscript.sleep 1000
ws.sendkeys("123456")
ws.sendkeys("{ENTER}")
wscript.quit 

Then save it as vbs209.vbs

You can execute vbs209.vbs directly, but it must be executed under the current path. So I wrote a bat script, ssh209.bat, which reads: start vbs209.vbs

In this way, in any path, I can enter the remote server without entering the password as long as I execute ssh209.

2. If it is ubuntu, you must first install expect through sudo apt install expect. Then write ssh209

#!/bin/expect
set timeout 30
spawn ssh [email protected]
expect "password:"
send "123456\r"
interact

After saving, execute ssh209 in any path to remotely access the server.

These two scripts, the bat script will open a new window, which is a bit of a fly in the ointment. And the shell script doesn't open a new window.

Guess you like

Origin blog.csdn.net/IamstudyingJava/article/details/130087974