0
我試圖追加公鑰到遠程服務器上的授權密鑰文件與pexpect但它不工作。看起來,輸出到文件的重定向不會發生,儘管沒有錯誤。這裏是我的代碼:如何追加到遠程服務器上的文件與pexpect
print "DEGUG: === append to authorized keys START ==="
print "DEBUG: sending echo <local public key> >> /root/.ssh/authorized_keys"
print " local public key: " + local_public_key
child.sendline('echo ' + local_public_key + ' >> /root/.ssh/authorized_keys')
child.expect(prompt)
child.expect(prompt)
child.sendline('echo $?')
child.expect(prompt)
ec = child.before.strip()
print("exit code: >>>>" + str(ec) + "<<<<")
print "DEGUG: === append to authorized keys END ==="
這裏是輸出:
DEGUG: === append to authorized keys START ===
DEBUG: sending echo <local public key> >> /root/.ssh/authorized_keys
local public key: ssh-rsa
AAAAB3NzaC1yc2EAAAABIwAAAQEAy408yT80SP2x9rZkR56j4wsbpqUSYOW1uY8Lf4XfJLinbtcpwyyYGJCuNbA30ve1+Lt/AJ2d1VdSAS4Afu1LKucFmj2lN8dqKWsCUBYXXtkUHILVelRTglMGFLTbLJofXYwUXA60UWWilYvWJ2j4HRjHgxUP7i0fYy4ruy5zpspZ8Svr+PMY9UjA3HSnPhFB2ZHkC+9kzexfVCM7nvKw9LekxY0x/BrD5z6D+652sr8A9EOgBsg9A/soL4JMycdWmTBDe16O5f4Db5Qt7oqn3Hbw7lki1XULIvTzyNhUtoiGiQiCbCh7G1ba+IccvfrIaK+kpmugeoaTC4zldXEGbw==
echo ssh-rsa
AAAAB3NzaC1yc2EAAAABIwAAAQEAy408yT80SP2x9rZkR56j4wsbpqUSYOW1uY8Lf4XfJLinbtcpwyyYGJCuNbA30ve1+Lt/AJ2d1VdSAS4Afu1LKucFmj2lN8dqKWsCUBYXXtkUHILVelRTglMGFLTbLJofXYwUXA60UWWilYvWJ2j4HRjHgxUP7i0fYy4ruy5zpspZ8Svr+PMY9UjA3HSnPhFB2ZHkC+9kzexfVCM7nvKw9LekxY0x/BrD5z6D+652sr8A9EOgBsg9A/soL4JMycdWmTBDe16O5f4Db5Qt7oqn3Hbw7lki1XULIvTzyNhUtoiGiQiCbCh7G1ba+IccvfrIaK+kpmugeoaTC4zldXEGbw==
>> /root/.ssh/authorized_keys
R56j4wsbpqUSYOW1uY8Lf4XfJLinbtcpwyyYGJCuNbA30ve1+Lt/AJ2d1VdSAS4Afu1LKucFmj2lN8dqKWsCUBYXXtkUHILVelRTglMGFLTbLJofXYwUXA60UWWilYvWJ2j4HRjHgxUP7i0fYy4ruy5zpspZ8Svr+PMY9UjA3HSnPhFB2ZHkC+9kzexfVCM7nvKw9LekxY0x/BrD5z6D+652sr8A9EOgBsg9A/soL4JMycdWmTBDe16O5f4Db5Qt7oqn3Hbw7lki1XULIvTzyNhUtoiGiQiCbCh7G1ba+IccvfrIaK+kpmugeoaTC4zldXEGbw==
ssh-rsa
AAAAB3NzaC1yc2EAAAABIwAAAQEAy408yT80SP2x9rZkR56j4wsbpqUSYOW1uY8Lf4XfJLinbtcpwyyYGJCuNbA30ve1+Lt/AJ2d1VdSAS4Afu1LKucFmj2lN8dqKWsCUBYXXtkUHILVelRTglMGFLTbLJofXYwUXA60UWWilYvWJ2j4HRjHgxUP7i0fYy4ruy5zpspZ8Svr+PMY9UjA3HSnPhFB2ZHkC+9kzexfVCM7nvKw9LekxY0x/BrD5z6D+652sr8A9EOgBsg9A/soL4JMycdWmTBDe16O5f4Db5Qt7oqn3Hbw7lki1XULIvTzyNhUtoiGiQiCbCh7G1ba+IccvfrIaK+kpmugeoaTC4zldXEGbw==
[[email protected] ~]# >> /root/.ssh/authorized_keys
[[email protected] ~]# echo $?
echo $?
0
[[email protected] ~]# exit code: >>>>echo $?
0
[[email protected] ~]<<<<
DEGUG: === append to authorized keys END ===
當我檢查遠程authorized_keys
文件,新的密鑰不添加。 看來,密鑰被回顯到標準輸出而不是寫入文件。不知道如何解決這個問題。
這就是它......傻我! ;-)謝謝你指出這一點。 – Lidia