Shell realizes SSH automatic login

Posted on


As a tool man, always keepLazyIn UNIX like systems, don’t waste itshellThis magic tool, let the computer serve us~

Shell realizes SSH automatic login

Basic knowledge, review

How scripts run

Execute. / file by file name_

Note: if you need to execute permission, you can usesudo chmod +x ./file_name.shadd to

Specify the script interpreter to execute the file sh file_

sh file_name.shThe script interpreter is specified/bin/shTo explain the execution script; common script interpreters include:/bin/bashWait, we can use itls -l /bin/*shCommand to view the currently available script interpreters;

Use. / file_ The name or source command executes the script

This method does not fork a child process to execute the script like the first two methods, but uses the current shell environment for execution. We don’t have to restart the shell or log in again to make the current changes take effect. Generally used for. Bashrc or. Bash_ When the profile is modified,


Definition: when we write a shell script, we always add a line to the front#!/binbashIt’s scriptedshebang

Function: it indicates the interpreter of the script runtime. When executing shell script directly with the file name, shebang must be brought with it.

Use: you can attach options directly after shebang. When executing, we use options by default;

astest.shOfshebangby#!/bin/sh -xWhen we execute the script:

./ helloamount to:bin/sh -x ./ hello;

Note: shebang will be overridden by the script interpreter when it is specified to execute the script.

automatic logon

Expect interpreter

Expect is an interpreter that can implement automatic and interactive tasks. It can also interpret common shell syntax commands. Its features are as follows:

  • The spawn command:

spawn commandThe command will fork a subprocess to execute the command command, and then execute the following commands in this subprocess;

  • Expect command:

    Its general usage isexpect "string", you can get the string string at once, and you can use * and other wildcards in the string string;stringAfter matching the information returned from the command line, expect will immediately execute the script down;

  • Set timeout command:

set timeout nThe command sets the wait timeout time of expect command to N seconds. If the expected command is not obtained within N seconds, expect is false, and the script will continue to execute downward;

  • Send command:

The general usage issend "stringr"To enter a message to the command line,rInput carriage return;

  • Interact command: allows the user to interact with the current shell;

Auto login script

Create scripttouch aliyun_login_shell.shThe contents are as follows:

  Set user 
  Set host [server IP]
  Set port
  Set password
  set timeout 30
  Spawn SSH - p $port $user @ $host  fork a subprocess executes SSH login command;
  expect "password:"
  send "$passwordr"

Script execution login

implementsudo chmod +x ./aliyun_login_shell.shCommand to add execution permission to shell script;

function./aliyun_login_shell.shCommand, one click login!

Optimize it:

Problems to be solved:

  • input./file_name.shThe order is too long…
  • It can only be executed in the script directory, otherwise the command output with absolute path is longer.

The script is finished, but there are still some small flaws:

  • input./file_name.shThe order is too long…
  • It can only be executed in the script directory, otherwise the command output with absolute path is longer.

Here we think of the Linux alias command:

  • Use the alias command to set the command alias, for example:alias alg=./aliyun_login_shellIn this wayalgIt’s equivalent to execution./aliyun_login_shell
  • Edit Bash_ Profile file, which will be executed once when the terminal window is created, so it can help us set the alias again;

Execute the ordervim ~./bash_profileIn the file, add:

alias alias_name="/root_dir/../

Reuse. ~./bash_profileorsource ~./bash_profile

From then on, no matter which directory we are in, we just need to enteralias_nameCommand, enter, real one click login!

Iterm2 tool Auto Login

Shell realizes SSH automatic login

reference resources:…

If you like it, you can pay attention to it, and you can make progress. Over!

Leave a Reply

Your email address will not be published.