python小白-day9 数据库操作与Paramiko模块
时间:2022-03-14 18:39
12345678910111213141516 import
paramiko
# 创建SSH对象
ssh
=
paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname
=
‘192.168.11.200‘
, port
=
22
, username
=
‘hetan‘
, password
=
‘123456‘
)
# 执行命令
stdin, stdout, stderr
=
ssh.exec_command(
‘df‘
)
# 获取命令结果
result
=
stdout.read()
print
(result.decode())
# 关闭连接
ssh.close()
import
paramiko
# 创建SSH对象
ssh
=
paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname
=
‘192.168.11.200‘
, port
=
22
, username
=
‘hetan‘
, password
=
‘123456‘
)
# 执行命令
stdin, stdout, stderr
=
ssh.exec_command(
‘df‘
)
# 获取命令结果
result
=
stdout.read()
print
(result.decode())
# 关闭连接
ssh.close()
SSHClient 封装 Transport:
123456789101112 | import paramiko transport = paramiko.Transport(( ‘192.168.11.200‘ , 22 )) transport.connect(username = ‘hetan‘ , password = ‘123456‘ ) ssh = paramiko.SSHClient() ssh._transport = transport stdin, stdout, stderr = ssh.exec_command( ‘df‘ ) print (stdout.read().decode()) transport.close() |
SSHClient 封装 Transport: 查询全部: ?123456789101112131415161718 import
paramiko
private_key
=
paramiko.RSAKey.from_private_key_file(
‘/home/auto/.ssh/id_rsa‘
)
# 创建SSH对象
ssh
=
paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname
=
‘192.168.11.200‘
, port
=
22
, username
=
‘hetan‘
, key
=
private_key)
# 执行命令
stdin, stdout, stderr
=
ssh.exec_command(
‘df‘
)
# 获取命令结果
result
=
stdout.read()
print
(result.decode())
# 关闭连接
ssh.close()
12345678910111213 import
paramiko
private_key
=
paramiko.RSAKey.from_private_key_file(
‘/home/auto/.ssh/id_rsa‘
)
transport
=
paramiko.Transport((
‘192.168.11.200‘
,
22
))
transport.connect(username
=
‘hetan‘
, pkey
=
private_key)
ssh
=
paramiko.SSHClient()
ssh._transport
=
transport
stdin, stdout, stderr
=
ssh.exec_command(
‘df‘
)
transport.close()
123456789101112 import
paramiko
transport
=
paramiko.Transport((
‘192.168.11.200‘
,
22
))
transport.connect(username
=
‘hetan‘
,password
=
‘123456‘
)
sftp
=
paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put(
‘/tmp/location.py‘
,
‘/tmp/test.py‘
)
# 将remove_path 下载到本地 local_path
sftp.get(
‘remove_path‘
,
‘local_path‘
)
transport.close()
1234567891011121314 import
paramiko
private_key
=
paramiko.RSAKey.from_private_key_file(
‘/home/auto/.ssh/id_rsa‘
)
transport
=
paramiko.Transport((
‘192.168.11.200‘
,
22
))
transport.connect(username
=
‘hetan‘
, pkey
=
private_key )
sftp
=
paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put(
‘/tmp/location.py‘
,
‘/tmp/test.py‘
)
# 将remove_path 下载到本地 local_path
sftp.get(
‘remove_path‘
,
‘local_path‘
)
transport.close()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 #!/usr/bin/env python
import
paramiko
import
uuid
class
Haproxy(
object
):
def
__init__(
self
):
self
.host
=
‘192.168.11.200‘
self
.port
=
22
self
.username
=
‘hetan‘
self
.pwd
=
‘123456‘
self
.__k
=
None
def
create_file(
self
):
file_name
=
str
(uuid.uuid4())
with
open
(file_name,
‘w‘
) as f:
f.write(
‘sb‘
)
return
file_name
def
run(
self
):
self
.connect()
self
.upload()
self
.rename()
self
.close()
def
connect(
self
):
transport
=
paramiko.Transport((
self
.host,
self
.port))
transport.connect(username
=
self
.username,password
=
self
.pwd)
self
.__transport
=
transport
def
close(
self
):
self
.__transport.close()
def
upload(
self
):
# 连接,上传
file_name
=
self
.create_file()
sftp
=
paramiko.SFTPClient.from_transport(
self
.__transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put(file_name,
‘/home/hetan/tttttttttttt.py‘
)
def
rename(
self
):
ssh
=
paramiko.SSHClient()
ssh._transport
=
self
.__transport
# 执行命令
stdin, stdout, stderr
=
ssh.exec_command(
‘mv /home/hetan/tttttttttttt.py /home/hetan/ooooooooo.py‘
)
# 获取命令结果
result
=
stdout.read()
ha
=
Haproxy()
ha.run()
1234567891011121314 import
pymysql
conn
=
pymysql.connect(host
=
‘192.168.11.200‘
,user
=
‘hetan‘
,passwd
=
‘123456‘
,db
=
‘mydb‘
)
cur
=
conn.cursor()
reCount
=
cur.execute(
‘insert into students(name,sex,age,tel) values(%s,%s,%s,%s)‘
,(
‘liuyao‘
,
‘man‘
,
‘20‘
,
‘1235‘
))
conn.commit()
cur.close()
conn.close()
print
(reCount)
123456789101112131415161718 import pymysql
conn = pymysql.connect(host=
‘192.168.11.200‘
,user=
‘hetan‘
,passwd=
‘123456‘
,db=
‘mydb‘
)
cur = conn.cursor()
li = [
(
‘alex‘
,
‘man‘
,
18
,
‘1515151‘
),
(
‘wupeiqi‘
,
‘man‘
,
18
,
‘1551515‘
)
]
reCount = cur.executemany(
‘insert into students(name,sex,age,tel) values(%s,%s,%s,%s)‘
,li)
conn.commit()
cur.close()
conn.close()
print(reCount)
1234567891011121314 import
pymysql
conn
=
pymysql.connect(host
=
‘192.168.11.200‘
,user
=
‘hetan‘
,passwd
=
‘123456‘
,db
=
‘mydb‘
)
cur
=
conn.cursor()
reCount
=
cur.execute(
‘delete from students where id=%s‘
,(
‘1‘
,))
conn.commit()
cur.close()
conn.close()
print
(reCount)
1234567891011121314 import
pymysql
conn
=
pymysql.connect(host
=
‘192.168.11.200‘
,user
=
‘hetan‘
,passwd
=
‘123456‘
,db
=
‘mydb‘
)
cur
=
conn.cursor()
reCount
=
cur.execute(
‘update students SET name=%s WHERE id=%s‘
,(
‘hetan‘
,
‘2‘
,))
conn.commit()
cur.close()
conn.close()
print
(reCount)
1234567891011121314151617181920 import
pymysql
conn
=
pymysql.connect(host
=
‘192.168.11.200‘
,user
=
‘hetan‘
,passwd
=
‘123456‘
,db
=
‘mydb‘
)
cur
=
conn.cursor()
reCount
=
cur.execute(
‘select * from students‘
)
print
(cur.fetchone())
print
(cur.fetchone())
cur.scroll(
-
1
,mode
=
‘relative‘
)
print
(cur.fetchone())
print
(cur.fetchone())
cur.scroll(
0
,mode
=
‘absolute‘
)
print
(cur.fetchone())
print
(cur.fetchone())
cur.close()
conn.close()
print
(reCount)
12345678910111213 import
pymysql
conn
=
pymysql.connect(host
=
‘192.168.11.200‘
,user
=
‘hetan‘
,passwd
=
‘123456‘
,db
=
‘mydb‘
)
cur
=
conn.cursor()
reCount
=
cur.execute(
‘select * from students‘
)
print
(cur.fetchall())
cur.close()
conn.close()
print
(reCount)