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