Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在没有密码的情况下使用paramiko连接到远程服务器?_Python_Ssh - Fatal编程技术网

Python 如何在没有密码的情况下使用paramiko连接到远程服务器?

Python 如何在没有密码的情况下使用paramiko连接到远程服务器?,python,ssh,Python,Ssh,我正在用Python编写一个脚本,它需要使用SSH连接到远程\u服务器,并将文件从远程\u服务器移动到主机\u服务器。我需要做它没有密码,因为它需要为任何远程服务器和任何主机服务器用户的工作 我的代码: #get IP and username for remote access IP = input("Enter host_server IP: ").split() username = input("Enter username: ").split() #password = ???????

我正在用Python编写一个脚本,它需要使用SSH连接到
远程\u服务器
,并将
文件
远程\u服务器
移动到
主机\u服务器
。我需要做它没有密码,因为它需要为任何远程服务器和任何主机服务器用户的工作

我的代码:

#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设置无密码访问?

SSH.connect()
使用关键字参数
pkey
,您可以使用它来指定您的私有文件

#get IP and username for remote access
IP = input("Enter host_server IP: ").split()
username = input("Enter username: ").split()


#create a file on host_server for file
file_a = open(date+"file.txt", "a") #ignore the date variable
file = str(date+"file.txt")
import paramiko
import os
privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
ssh.connect(IP[0], username = user[0], pkey = mykey)