2017-04-11 217 views
0

我有Linux遠程機器,它在登錄嘗試中運行腳本,它將運行腳本,並且它需要用戶輸入。使用ssh執行自動執行用戶登錄的輸入執行命令

主要目的是在登錄到該用戶時測試登錄用戶,以及遠程自動腳本響應的密碼。

遠程機器內部信息:

$ cat /etc/passwd

remoteuser表:dontCare項:dontCare項:dontCare項:dontCare項:/usr/bin/somescript.sh

當用戶登錄到remoteuser表,somescript.sh會運行。

它要求用戶輸入

$ read -p '>' tmp ,我不想做那個腳本進行任何更改腳本的內容。登錄

login: remoteuser 

Password: remoteuserpassword 

Quit Configuration (q, quit) 

是否有可能對我來說,使用ssh命令發送用戶輸入? 使用exec_command

但還是目前我的方法是使用SSH的paramiko,

連接= paramiko.SSHClient()未能發送輸入正確的順序或未能傳遞給該腳本。

​​到我不使用登錄從根測試,因爲它不要求密碼該遠程用戶的paramiko EXEC connection.exec_command('echo q')

echo q | su - remoteuser

回答

0

我結束了使用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') 

,並執行該

execute(remoteuser_login, host='ipaddress') 
0

是否有可能對我來說,使用ssh命令發送用戶輸入?

不是,除非您更改腳本。 SSH處理您的腳本爲用戶殼(除非你有也sshd_config一些其他配置),這意味着它具有以下參數運行它,如果你提供一些:

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

這是方式,因爲它同將在/usr/bin/bash -c "commnad"的用戶shell中運行任何其他任意命令。在你的情況下,它看起來像

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

但最有可能的腳本的參數被忽略。你的腳本等待標準輸入的命令,我相信你也可以在Paramiko中提供這個輸入。

+0

我的主要目的是測試登錄用戶,並與自動腳本的口令響應時登錄到該用戶。 – sangelion