python - 使用 paramiko SSH 到具有超级用户的设备
问题描述
我正在尝试使用 r/o 用户然后超级用户创建脚本来 ssh 我的设备并执行命令。以下是我在腻子上执行的步骤
- 以管理员身份登录(r/o 用户)和密码。
- sudo su 和密码
- 执行命令
- 打印执行命令的输出
我尝试了下面的代码使其工作,但我没有得到任何输出。
import paramiko
import pandas as pd
import openpyxl
from paramiko import AuthenticationException
from paramiko.ssh_exception import SSHException, NoValidConnectionsError
import socket
import time
import os
import inspect
import datetime
start = time.time()
print("Starting................................Please Wait")
df=pd.DataFrame(columns=["ip","Status","Remarks"])
ips = ['10.11.8.71']
root = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
#ips = open (root+ "/440_ip.txt")
for ipp in ips:
ip = ipp.strip()
port=22
username='admin'
#password='AeGEBUx66m_1ND'
#cmd='interface wireless set [ find default-name=wlan1 ] ampdu-priorities=0,1,2,3,4,5,6,7 rate-set=configured rx-chains=0,1 scan-list=5825-5875 security-profile=iB440 ssid=iBw supported-rates-a/g="" basic-rates-a/g=""'
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,timeout=5,password='HeWGEUx66m=_4!ND')
stdin, stdout, stderr = ssh.exec_command('sudo bash', get_pty = True)
time.sleep(0.1)
stdin.write('HeWGEUx66m=_4!ND\n')
stdin.flush()
stdin.write('whoami\n')
#time.sleep(1)
stdin.flush()
dd = stdout.readlines()
print(dd)
ssh.close()
运行代码后没有错误,似乎卡在某个循环中。,
开始......................请稍候
在单行命令中使用 ssh 和 plink
C:\Users\Administrator\AppData\Local\Programs\Python\Python38>plink.exe -ssh -t admin@10.11.8.71 sudo bash admin@10.11.8.71's password: Access granted. Press Return to begin session. Password: bash-3.2# whoami root bash-3.2#```
解决方案
为了让它正常工作,我使用了 paramiko 的频道,它就像魅力一样。
import paramiko
from paramiko import *
from paramiko.channel import Channel
import time
import os
import inspect
import datetime
from socket import *
import pandas as pd
df = pd.DataFrame(columns=["Ip","Status",'Remarks'])
root = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
#ips = ['10.23.0.30', '10.23.0.11','10.23.0.12','10.23.0.13']
ips = open(root+ "\\ahip.txt")
for ipp in ips:
ip = ipp.strip()
print(ip)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(ip, port=22,timeout = 5,username='op', password='C#Uj!AnX')
channel:Channel = ssh.invoke_shell()
#print(type(channel))
channel_data = str()
while True:
#channel.recv_ready():
#time.sleep(1)
channel_data += str(channel.recv(999))
channel.send("su -\n")
time.sleep(1)
#channel_data += str(channel.recv(999))
# if "Password" in channel_data:
channel.send("HeWGEUx\n")
time.sleep(3)
#channel_data += str(channel.recv(999))
channel.send("/bs/lteCli\n")
time.sleep(3)
channel.send("logger threshold set cli=6\n")
time.sleep(4)
channel.send("db set stackCfg [1] EnCLPC=0\n")
time.sleep(4)
channel.send("db get stackCfg EnCLPC\n")
time.sleep(3)
#channel.send("db get stackCfg EnCLPC\n")
time.sleep(.1)
channel_data += str(channel.recv(99999))
str2 = 'Done'
df.loc[ip]=[ip,str2,channel_data]
#print(channel_data)
channel.close()
ssh.close()
break
except (timeout ,AuthenticationException):
print(ip+'not done')
str1 = 'Not Done'
df.loc[ip]=[ip,'failed',str1]
推荐阅读
- loops - 如何在 for 循环的每次迭代中保存变量
- c++ - 是否使用对使用 reinterpret_cast 未定义行为强制转换的指针的引用?
- python - 蟒蛇 | Flask Url_For MissingSchema:无效的 URL 错误
- angular - 有没有办法自动更新和构建一个android应用程序
- hibernate - jBPM在计时器事件后创建重复任务:多个实例正在运行
- python - 通过 dj_rest_auth 在用户注册中保存额外的字段
- azure - 如何在 Azure 上托管静态 HTML 并使其仅对我组织的经过身份验证的用户可用?
- javascript - 使用 Axios 未收到来自后端 RestAPI 的响应错误
- javascript - 如何在由 Visual Studio 代码内部人员打开的两个项目中使用 npm?
- mysql - 从不同行开始的 sql 压缩数据库的命令行导入