我使用的是phpseclib,需要製作一些PHP函數,讓某人以編程方式ssh進入他們的服務器並更改root密碼,並更改可能忘記密碼的用戶的密碼(所以必須記錄以root身份)。如何在使用phpseclib ssh2 exec()函數時設置標誌?
我試過使用libssh2,但發現它有點討厭使用。我現在看着看起來更健壯的phpseclib。但是,當我試圖用 '蘇' 命令,像這樣:
echo $ssh->exec('su');
我得到的回覆:
su: must be run from a terminal
,當我嘗試使用sudo:
echo $ssh->exec('sudo passwd root');
我得到錯誤:
sudo: no tty present and no askpass program specified
無論如何,事實證明,su是d isabled直接ssh訪問,但在看看this article後,事實證明,你可以用下面的命令做到這一點:
ssh -t -t -l 'username' 'host' 'su -'
那進入我的筆記本電腦終端時,就是終於爲我工作反正(運行Ubuntu的),然後我輸入我的密碼,然後輸入root密碼完成。
從鏈接到網站上面引述:正如我上面用說
Ssh commands (using -t) the remote sshd to establish a 'pseudo-terminal' pipe to the worker process when -t is given.
. ssh does this as long as its stdin is a terminal.
. But if ssh's stdin is a non-terminal, ssh won't direct sshd to establish a
pseudo-terminal unless TWO -t's are given:echo password | ssh -t -t -l username remote_host
. So with -t -t (from ssh) sshd sets up a pseudo-terminal to the client process.
. The client, whether it be 'tty' or 'su' cannot tell it is connected to a ficticious >terminal:
echo dummystr | ssh -t -t -l username host.com -c ''tty'
echo password | ssh -t -t -l username host.com -c 'su -'
So there is the answer. Use double -t if you are 'su root'ing' on a linux box through an >interactive client ssh like the one from OpenBSD.
因此,它實際上是從終端的工作:
ssh -t -t -l 'username' 'host' 'su -'
,但我真的希望能夠執行此命令使用phpseclib。唯一的問題是我不知道如何將任何標誌放入exec()函數中。具體來說,我需要輸入-t標誌(兩次)。
我找的年齡並沒有找到任何內容。非常感謝你的幫助。對不起,這篇文章的長度也是如此。 :)
乾杯
喬
您可以共享您正在使用的庫。我不得不穀歌phpseclib,然後發現有兩個SSH實現。 – Till 2011-04-23 01:21:03
啊,對不起。我正在使用我認爲是最新的phpseclib版本:v 1.53 – Joe 2011-04-23 10:15:58
不,我的意思是,phpseclib提到'Net_SSH'和'Net_SSH2'。你在用哪個? :) – Till 2011-04-23 17:03:55