Taking the input from auto-execute user login usin

2019-09-12 13:05发布

I have Linux remote machine that running a script on the login attempt, which it will run the script, and its require the user input.

The main intention is to test the login user, the password with the auto-scripts response remotely when logon to that user.

Information inside the remote machine:

$ cat /etc/passwd

remoteuser:dontcare:dontcare:dontcare:dontcare:/usr/bin/somescript.sh

When user login into remoteuser, the somescript.sh will run.

The content of the script which asking for the user input

$ read -p '>' tmp and I don't want to do any changes to that script.

Example of login

login: remoteuser

Password: remoteuserpassword

Quit Configuration (q, quit)

Is it possible for me to send the user input using ssh command? Currently my approach is using paramiko ssh,

connection = paramiko.SSHClient() using exec_command

but still it failed to send the input in right sequence or failed to passed to that script.

$ echo q to the paramiko exec connection.exec_command('echo q')

I not using login as from root to test this remote user because it doesn't ask for password.

echo q | su - remoteuser

2条回答
Summer. ? 凉城
2楼-- · 2019-09-12 14:09

I ended up by using fabric.

from fabric.tasks import execute
from fabric.api import run, settings, env

def remoteuser_login(self):
    env.user = 'remoteuser'
    env.password = 'remotepassword'
    with settings(prompts={'>': 'q\n'}):
        run('exit')

and to execute that

execute(remoteuser_login, host='ipaddress')
查看更多
走好不送
3楼-- · 2019-09-12 14:10

Is it possible for me to send the user input using ssh command?

Not, unless you change the script. SSH handles your script as a users shell (unless you have also some other configuration in sshd_config), which means that it runs it with the following arguments, if you provide some:

/usr/bin/somescript.sh -c "command"

This is the same way as it would be running any other arbitrary command in the users shell with /usr/bin/bash -c "commnad". In your case it would look like

/usr/bin/somescript.sh -c "echo q"

but most probably the arguments of the script are ignored. Your script waits for the commands on standard input and I believe you can provide that input in Paramiko too.

查看更多
登录 后发表回答