SSH shell to achieve automatic landing

Foreword

Developed using a docker, each landing always enter their own development machine  ssh user_name@ip_string, and then confirm the input password, fast chips also often wrong. As a lazy, definitely tricky to find a way to see the next ssh command, because it is encrypted to interact with a server, so there is no direct option comes with a password, I had to give up.

A few days ago when colleagues technology sharing, only to see him actually enter the command line ./test.shsuccessfully landed on the development machine, very surprised, then back search study a little, then into the article.


shell script basis

Before writing automatic ssh login script, first say a few basic shell script, this is not the basis of what some of the syntax, online everywhere, here summed up the operating mechanism shell script -

Run shell script

First thing to say about the shell of several start-up mode, it is stepped on script-initiated pit, only use the original script ten minutes to get in, it took two hours to get it. But also it allows us to run the shell, know why.

Performed by the file name

shell script can be executed directly by file name, note that the files need to execute permissions. By  sudo chmod +x ./file_name.sh adding execute permissions to the file;

Specifies the script interpreter to execute file

We used  sh file_name.sh that specifies the script interpreter  /bin/shto explain the execution of the script; the script interpreter common are: /bin/bashetc. We can use the ls -l /bin/*shcommand to view the current script interpreter available;

Use. ./File_name source command or script execution

These do not like the same way as the first two fork a child process to execute the script, but the use of shell execution of the current environment, for the time being modified .bashrc or .bash_profile, we do not have to restart the shell or re-login system, will be able to the current changes to take effect.

shebang

When we write a shell script, always used to add a line at the top  #!/binbash, it is the script shebang, as to why such a strange name called, C language and Unix developer Dennis Ritchie called it 可能是类似于"hash-bang"的英国风描述性文字;

Posted a explanation on the wiki:

In computer science, the Shebang is a string and the line number of the well constituted exclamation mark, which appears in the text file of the first two characters of the first line. Shebang presence in the document, Unix-like operating system loader program analyzes the contents Shebang, these elements are as interpreter instructions, and the instruction is called, and the file path containing the interpreter as Shebang parameters.

Simply put, it indicates the interpreter this script runs, so when using the file name directly execute a shell script, you must bring shebang; in addition, we can also directly behind shebang additional options when we perform the default option to perform ;

As  test.shof shebangit is  #!/bin/sh -xthat when we execute the script:

./test.sh hello

It is equivalent to:

bin/sh -x ./test.sh hello;

Automatic landing ssh written a script, need to use the shebang (interpreter) is  /usr/bin/expect;

Note that: when the specified script interpreter to execute the script, shebang will be covered by the specified script interpreter, namely priority to execute the script (habitually use sh ./test.sh but prompt command using the specified script interpreter not found)


expect interpreter

expect to achieve an automatic and interpreter interactive tasks, it can also explain the common shell syntax command, which features in the following command:

spawn command:

spawn commandCommand will fork a child process to execute command command, and then execute subsequent commands in this sub-process;

In automatic ssh login script we use  spawn ssh user_name@ip_str, fork a child process execute ssh login command;

expect command:

expect command is a critical command interpreter expect, as its general usage  expect "string", i.e., to obtain a desired string string string string may be used in the * wildcard the like;

After the string returned by the command-line information matches, expect will immediately execute the script down;

set timeout command:

set timeout nCommand will expect a command to wait for the timeout to n seconds, not to get in their expected order n seconds, expect to false, the script will continue down;

send the command:

General usage send command is  send "string", we usually they will enter commands as input a message to the command line, of course, do not forget to stringadd on later  \r represented enter a carriage return;

interact command:

interact command is very simple, when executing this command, the child process will operate the right fork of the script to the user, allowing the user to interact with the current shell;


Complete script

The following is a complete version of the script  test.sh:

#!/usr/bin/expect                   // 指定shebang

set timeout 3                       // 设定超时时间为3秒
spawn ssh user_name@172.***.***.*** // fork一个子进程执行ssh命令
expect "*password*"                 // 期待匹配到 'user_name@ip_string's password:' 
send "my_password\r"                // 向命令行输入密码并回车
send "sudo -s\r" 
send "cd /data/logs\r"              // 帮我切换到常用的工作目录
interact                            // 允许用户与命令行交互

Execute  sudo chmod +x ./test.shcommand to add execute permissions to the shell scripts;

Run  ./test.shcommand, a key to successful landing!

After a few simple commands ,, interact with them to solve the problem with the command line, many complex functions also goes without saying -


alias alias

The script is finished, but still some small flaws:

  • Enter the ./file_name.shcommand is too long. . .
  • It can only be executed in the script directory, or use the absolute path of the output of the command is longer.

Here we think of linux alias command:

The alias command:

The alias command to use  alias alias_name="ori_command"the alias_name set ori_command alias, so we enter the implementation alias_name, is equivalent to the implementation of the ori_command;

However, we will find that when you close the current shell, then open a shell window, then use alias_name, the system prompts command not found;

There is no way it can maintain order? Bash_profile edit files.

bash_profile file

We edit bash_profile file, this file will be created in the terminal window when first performed once, so you can help us once again set an alias;

Execute commands vim ~./bash_profile, add internal documents:

alias alias_name="/root_dir/../file_name.sh

After saving, re-use  . ~./bash_profileor source ~./bash_profile the current script execution again set the alias command, complete set;

In this way, we matter in which directory, just type alias_namethe command, press Enter, the real key to a landing!


to sum up

As a program ape and keep the 偷懒awareness (of course this nor that lazy lazy ...), in the unix-like system, do not waste shellthis amazing tool that allows the computer to serve us -

More than a month did not write a blog, look at the recent APUE, UNP a book on C and Unix entry is still shallow, not scrawl fraught; usually they used to do notepad notes are also more scattered, unsystematic;

Slowly accumulate it, appropriate project and write, welcome attention ~

 

 

Transfer: https://www.cnblogs.com/zhenbianshu/p/5867440.html

Guess you like

Origin www.cnblogs.com/qiumingcheng/p/11544569.html