Pass commands as input to another command (su, ssh, sh, etc)

前端 未结 3 601
南方客
南方客 2020-11-21 23:23

I have a script where I need to start a command, then pass some additional commands as commands to that command. I tried

su
echo I should be root n         


        
相关标签:
3条回答
  • 2020-11-21 23:24

    If you want a generic solution which will work for any kind of program, you can use the expect command.

    Extract from the manual page:

    Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be. An interpreted language provides branching and high-level control structures to direct the dialogue. In addition, the user can take control and interact directly when desired, afterward returning control to the script.

    Here is a working example using expect:

    set timeout 60
    
    spawn sudo su -
    
    expect "*?assword" { send "*secretpassword*\r" }
    send_user "I should be root now:"
    
    expect "#" { send "whoami\r" }
    expect "#" { send "exit\r" }
    send_user "Done.\n"
    exit
    

    The script can then be launched with a simple command:

    $ expect -f custom.script
    

    You can view a full example in the following page: http://www.journaldev.com/1405/expect-script-example-for-ssh-and-su-login-and-running-commands

    Note: The answer proposed by @tripleee would only work if standard input could be read once at the start of the command, or if a tty had been allocated, and won't work for any interactive program.

    Example of errors if you use a pipe

    echo "su whoami" |ssh remotehost
    --> su: must be run from a terminal
    
    echo "sudo whoami" |ssh remotehost
    --> sudo: no tty present and no askpass program specified
    

    In SSH, you might force a TTY allocation with multiple -t parameters, but when sudo will ask for the password, it will fail.

    Without the use of a program like expect any call to a function/program which might get information from stdin will make the next command fail:

    ssh use@host <<'____HERE'
      echo "Enter your name:"
      read name
      echo "ok."
    ____HERE
    --> The `echo "ok."` string will be passed to the "read" command
    
    0 讨论(0)
  • 2020-11-21 23:45

    Adding to tripleee's answer:

    It is important to remember that the section of the script formatted as a here-document for another shell is executed in a different shell with its own environment (and maybe even on a different machine).

    If that block of your script contains parameter expansion, command substitution, and/or arithmetic expansion, then you must use the here-document facility of the shell slightly differently, depending on where you want those expansions to be performed.

    1. All expansions must be performed within the scope of the parent shell.

    Then the delimiter of the here document must be unquoted.

    command <<DELIMITER
    ...
    DELIMITER
    

    Example:

    #!/bin/bash
    
    a=0
    mylogin=$(whoami)
    sudo sh <<END
        a=1
        mylogin=$(whoami)
        echo a=$a
        echo mylogin=$mylogin
    END
    echo a=$a
    echo mylogin=$mylogin
    

    Output:

    a=0
    mylogin=leon
    a=0
    mylogin=leon
    

    2. All expansions must be performed within the scope of the child shell.

    Then the delimiter of the here document must be quoted.

    command <<'DELIMITER'
    ...
    DELIMITER
    

    Example:

    #!/bin/bash
    
    a=0
    mylogin=$(whoami)
    sudo sh <<'END'
        a=1
        mylogin=$(whoami)
        echo a=$a
        echo mylogin=$mylogin
    END
    echo a=$a
    echo mylogin=$mylogin
    

    Output:

    a=1
    mylogin=root
    a=0
    mylogin=leon
    

    3. Some expansions must be performed in the child shell, some - in the parent.

    Then the delimiter of the here document must be unquoted and you must escape those expansion expressions that must be performed in the child shell.

    Example:

    #!/bin/bash
    
    a=0
    mylogin=$(whoami)
    sudo sh <<END
        a=1
        mylogin=\$(whoami)
        echo a=$a
        echo mylogin=\$mylogin
    END
    echo a=$a
    echo mylogin=$mylogin
    

    Output:

    a=0
    mylogin=root
    a=0
    mylogin=leon
    
    0 讨论(0)
  • 2020-11-21 23:45

    A shell script is a sequence of commands. The shell will read the script file, and execute those commands one after the other.

    In the usual case, there are no surprises here; but a frequent beginner error is assuming that some commands will take over from the shell, and start executing the following commands in the script file instead of the shell which is currently running this script. But that's not how it works.

    Basically, scripts work exactly like interactive commands, but how exactly they work needs to be properly understood. Interactively, the shell reads a command (from standard input), runs that command (with input from standard input), and when it's done, it reads another command (from standard input).

    Now, when executing a script, standard input is still the terminal (unless you used a redirection) but the commands are read from the script file, not from standard input. (The opposite would be very cumbersome indeed - any read would consume the next line of the script, cat would slurp all the rest of the script, and there would be no way to interact with it!) The script file only contains commands for the shell instance which executes it (though you can of course still use a here document etc to embed inputs as command arguments).

    In other words, these "misunderstood" commands (su, ssh, sh, sudo, bash etc) when run alone (without arguments) will start an interactive shell, and in an interactive session, that's obviously fine; but when run from a script, that's very often not what you want.

    All of these commands have ways to accept commands by ways other than in an interactive terminal session. Typically, each command supports a way to pass it commands as options or arguments:

    su root -c 'who am i'
    ssh user@remote uname -a
    sh -c 'who am i; echo success'
    

    Many of these commands will also accept commands on standard input:

    printf 'uname -a; who am i; uptime' | su
    printf 'uname -a; who am i; uptime' | ssh user@remote
    printf 'uname -a; who am i; uptime' | sh
    

    which also conveniently allows you to use here documents:

    ssh user@remote <<'____HERE'
        uname -a
        who am i
        uptime
    ____HERE
    
    sh <<'____HERE'
        uname -a
        who am i
        uptime
    ____HERE
    

    For commands which accept a single command argument, that command can be sh or bash with multiple commands:

    sudo sh -c 'uname -a; who am i; uptime'
    

    As an aside, you generally don't need an explicit exit because the command will terminate anyway when it has executed the script (sequence of commands) you passed in for execution.

    0 讨论(0)
提交回复
热议问题