Login without authorization

Description of the problem

I want to access the server without asking for a password (it will be specified in the script) and run a command on this server.

My code

#!/usr/bin/expect
spawn sudo su - <server_name>
expect "[sudo] password for chronicles:"
set Password "xxxxxxx"
send "$Password\r"
#set timeout 300
send "whoami\r"
send "ls -ltr\r"
expect eof

Output

invalid command name "sudo"
    while executing

Limitations

  • I do not have permission to change env variables or change .bash_profile / .bashrc.
  • Su server_name command not allowed
+5
source share
3 answers

David is right that this is usually a bad idea . Sometimes there are good reasons for this or something similar (for example, automatically logging into serial consoles to control shutdowns), but you have not provided any guidance as to why it makes sense for you to do it this way.

, invalid command name spawn, [sudo] expect. tcl, [] , . , , expect, glob, , [] . , , , , :

    expect "\\\[sudo\\\] password for chronicles:"

, , , expect, .

+8

- SSH. .

Google, , , . SSH - .

+2

[]interpreted as “command quotes” (“command” as in “Command Command Language”, which is short for Tcl) c Tcl.

{}is the strongest quote in Tcl, you can use it to prevent any interpretation:

expect {[sudo] password for chronicles:}

of course, you can also just omit [sudo]:

expect "password for chronicles:"
+1
source

All Articles