9
我在Python中編寫腳本,需要使用SSH連接到remote_server
,並將file
從remote_server
移動到host_server
。我需要在沒有密碼的情況下執行它,因爲它需要適用於任何遠程服務器和任何主機服務器用戶。如何使用paramiko無密碼連接到遠程服務器?
我的代碼:
#get IP and username for remote access
IP = input("Enter host_server IP: ").split()
username = input("Enter username: ").split()
#password = ???????
#create a file on host_server for file
file_a = open(date+"file.txt", "a") #ignore the date variable
file = str(date+"file.txt")
#move file to host_server
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(IP[0], username = user[0], password = password[0])
print "Connected to server."
transfer = ssh.open_sftp()
transfer.get("file.txt", file)
transfer.close()
print "Transfer completed."
問:有沒有辦法來設置腳本內的公共密鑰,而無需訪問一個命令行終端,這樣每次腳本運行時,它會設置密碼的使用SSH訪問?