python幾種ssh登錄遠程機器的方式


python3環境下

 

(1)、密碼驗證的登錄方式

#!/usr/bin/python3
import paramiko
import sys


ssh = paramiko.SSHClient()

#set for passwd login model 
know_host = paramiko.AutoAddPolicy()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

#get login value

try:
remote_ip = sys.argv[1]
ssh_port = sys.argv[2]
username = sys.argv[3]
password = sys.argv[4]

except IndexError:
print("please add: ip port username password")
sys.exit()

def ssh_login(remote_ip,ssh_port,username,password):
ssh.connect(
hostname = remote_ip,
port = ssh_port,
username = username,
password = password
)
stdin,stdout,stderr = ssh.exec_command("ifconfig ")
return (stdout.read().decode())
ssh.close()

#main 
resp=ssh_login(remote_ip,ssh_port,username,password)
print (resp)

#運行效果:
root@mypython# python3 ssh_login.py 4.11.31.195 32200 root ggkk "ls -l"

total 8
drwxr-xr-x 2 root root 4096 Sep 18 00:30 file
drwxr-xr-x 3 root root 4096 Sep 5 08:32 mydir

 

(2)、遠程執行命令中攜帶變量:

name="geling"
command=f"""
cat << EOF >> /root/mytest 
hello {name}! 
EOF

然后調用前面一節的函數
root@mycloud-geling:~# cat mytest 
hello geling! 

 

(3)、公鑰的方式遠程登錄

#!/usr/bin/python3
import paramiko
import sys
import os


ssh = paramiko.SSHClient()

#設置私鑰登錄相關
ssh.load_system_host_keys()
privatekey = os.path.expanduser('/root/.ssh/Identity') #指定私鑰地址
key = paramiko.RSAKey.from_private_key_file(privatekey) #創建私鑰對象

#get login value

try:
remote_ip = sys.argv[1]
ssh_port = sys.argv[2]
username = sys.argv[3]

except IndexError:
print("please add: ip port username ")
sys.exit()

def ssh_login(remote_ip,ssh_port,usernamei,key):
ssh.connect(
hostname = remote_ip,
port = ssh_port,
username = username,
pkey = key
)
stdin,stdout,stderr = ssh.exec_command(command)
print(stderr.read().decode())
return (stdout.read().decode())
ssh.close()

#設置遠程執行命令
name="zhuling"
command=f"""
cat << EOF >> /home/liuhhua/mytest 
hello {name}! 
EOF
"""

#main 
resp=ssh_login(remote_ip,ssh_port,username,key)
print (resp)

運行結果:
root@mycloud-geling:~# cat /home/liuhhua/mytest 
hello zhuling!

 

(4)、遠程秘鑰登錄並切換到root執行命令:

#!/usr/bin/python3
import paramiko
import sys
import os
import time


ssh = paramiko.SSHClient()

ssh.load_system_host_keys()
privatekey = os.path.expanduser('/root/.ssh/Identity')
key = paramiko.RSAKey.from_private_key_file(privatekey)

#get login value

try:
remote_ip = sys.argv[1]
ssh_port = sys.argv[2]
username = sys.argv[3]

except IndexError:
print("please add: ip port username ")
sys.exit()

def ssh_login(remote_ip,ssh_port,usernamei,key):
#普通用戶登錄
ssh.connect(
hostname = remote_ip,
port = ssh_port,
username = username,
pkey = key
)
#切換到root部分邏輯
sshsu = ssh.invoke_shell() #調用子shell
time.sleep(1)
sshsu.send('su - \n')
buff= ''
time.sleep(1)
while not buff.endswith('Password: '): #預期字符串結尾
resp = sshsu.recv(9999)
buff =resp.decode(encoding='UTF-8',errors='strict')
sshsu.send(root_pwd)
sshsu.send('\n')
buff = ''
while not buff.endswith('# '):
resp = sshsu.recv(9999)
buff +=resp.decode(encoding='UTF-8',errors='strict')
sshsu.send(command) #切換后執行命令
sshsu.send('\n')
buff = ''
while not buff.endswith('# '):
resp = sshsu.recv(9999)
buff +=resp.decode(encoding='UTF-8',errors='strict')
ssh.close()
result = buff 
return (result)

root_pwd="xxx" #root密碼
name="liuhhua" 
command=f"""
cat << EOF >> /home/liuhhua/mytest 
hello i can su root ,now from {name}! 
EOF
"""

#main 
resp=ssh_login(remote_ip,ssh_port,username,key)
print (resp)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM