Taking the input from auto-execute user login using ssh execute command

柔情痞子 提交于 2020-01-06 18:12:21

问题


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


回答1:


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.




回答2:


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')


来源:https://stackoverflow.com/questions/43336007/taking-the-input-from-auto-execute-user-login-using-ssh-execute-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!