shell automatically lose the password

The first : sudo -s

echo "$passwd" |sudo -S mkdir /111

The second : Installing expect tools designed to automate interactive features

sudo apt-get install tcl tk expect
#!/usr/bin/expect
set timeout 3               
spawn ssh [email protected].*.*   
expect "*moonx:"  
send "$passwd\n"                                       
interact
  1. [#!/usr/bin/expect]

This line tells the operating system that script code uses a shell to execute. In fact, expect here and bash under linux, cmd under windows is a class thing.
  Note: This line needs to be the first line of the script.

  1. [set timeout 30]

Set the timeout, the unit is: seconds

  1. [spawn ssh [email protected].*.* ]

spawn are the environment before entering expect expect internal command can be executed, if not installed or expect to perform directly in the default SHELL command is not found spawn. Its main function is to add a process to run ssh shell, used to deliver interactive instructions.

  1. [expect "*moonx:: "]

An internal command also expect expect here, and expect the internal shell commands and command is the same, but it is not a function. This command is meant to determine the output in the last contains "password:" string, if there is an immediate return, otherwise after waiting for some time to return, waiting for a long time here is set up in front of three seconds.

  1. [send “$password\n”]

Here is the implementation of the interactive action, and action is equivalent to manually enter a password.

  1. [interact]

Upon completion of execution to maintain the cross-state, the control to the console, this time can be manually operated. If you do not log on after the completion of the sentence would quit rather than stay at the remote terminal.

Most editor:
basic introduction to using Expect

Ka.
Published 23 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/guaiderzhu1314/article/details/96311739