Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example and database operation of fortress machine

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Before learning the fortress machine, let's learn a core module paramiko. The common batch management server tools such as Fabric and ansible are based on the paramiko module, or the paramiko module is encapsulated and then improved. The commonly used functions are SSHClient and SFTPClient and their corresponding methods:

First, introduce SSHClient, which is a class under the paramiko module, which is used to create a ssh connection object. Ssh can be logged in through password-based authentication or secret key-based login authentication. Next, there are two scenarios:

① password-based login authentication

A ssh object is created

Ssh = paramiko.SSHClient ()

Allow hosts that are not in the local host kown_hosts file to connect

Ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ())

Create a ssh connection to the host based on the ssh protocol

Ssh.connect (hostname='10.0.0.13',port=22,username='opuser',password='123456')

Execute remote command

Stdin,stdout,stderr = ssh.exec_command ('df')

Get the execution result

Result = stdout.read ()

Print result

Close the ssh connection

Ssh.close ()

Example 1:

#! / usr/bin/env python

#-*-coding:utf8-*-

# Import paramiko module

Import paramiko

# instantiate an object ssh through class SSHClient

Ssh = paramiko.SSHClient ()

# allow hosts whose local host kown_hosts does not exist to log in

Ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ())

# Open a ssh connection

Ssh.connect (hostname='10.0.0.13',port=22,username='opuser',password='123456')

# execute the shell command df-Th

Stdin,stdout,stderr = ssh.exec_command ('df-Th')

# read the returned result and print it to the screen

Print stdout.read ()

# close ssh connection

Ssh.close ()

Login authentication of ② public key

Define the specified public key

Private_key = paramiko.RSAKey.from_private_key_file ('/ home/opuser/.ssh/id_rsa')

A ssh object is created

Ssh = paramiko.SSHClient ()

Allow hosts that are not in the local host kown_hosts file to connect

Ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ())

Create a ssh connection to the host based on the ssh protocol

Ssh.connect (hostname='10.0.0.13',port=22,username='opuser',pkey=private_key)

Execute remote command

Stdin,stdout,stderr = ssh.exec_command ('df')

Get the execution result

Result = stdout.read ()

Print result

Close the ssh connection

Ssh.close ()

[note: the implementation of login authentication based on public key needs to copy the local public key to .ssh / under the corresponding user's home directory of the remote host, and then can realize password-free login]

Example 2:

#-*-coding:utf8-*-

Import paramiko # Import paramiko module

# define the specified public key

Private_key = paramiko.RSAKey.from_private_key_file ('/ home/opuser/.ssh/id_rsa')

# create SSH object

Ssh = paramiko.SSHClient ()

# allow connections to hosts that are not in the know_hosts file

Ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ())

# Connect to the server

Ssh.connect (hostname='10.0.0.13', port=22, username='opuser',pkey=private_key)

# execute command

Stdin, stdout, stderr = ssh.exec_command ('df-Th')

# read command results

Result = stdout.read ()

# output the obtained results to the screen

Print result

From the above introduction, it can be found that the SSHClient class is used to log in to the remote host, so what is the SFTPClient class used to do? From the literal meaning, it is not difficult to find that it has something to do with sftp. Under Linux, sftp is a ftp program, which is equivalent to ftp under windows, so SFTPClinet should also be able to achieve the corresponding ftp function, while what are the ftp functions? what we usually use most is the upload and download of files, so we will introduce the upload and download functions of SFTPClinet. Like SSHClinet, SFTPClinet has two types of login authentication: password and key

① password-based authentication

# Import paramiko module

Import paramiko

# instantiate to create a tranport object channel (note that the parameter in parentheses is a tuple)

Transport = paramiko.Transport ('10.0.0.13, 22)

# pass a user name and password in the transport channel to create a ssh connection

Transport.connect (username='opuser', password='123456')

# create a sfp connection

Sftp = paramiko.SFTPClient.from_transport (transport)

# define local files and paths

Localfile='/lcsourece/localtion.py'

# define the storage path of remote files

Refile='/tmp/refile.py'

# upload the local file localtion.py to the remote / tmp/refile.py

Sftp.put (localfile,refile)

# download remote files locally

Sftp.get ('remoute_path','local_path')

# close ssh connection

Transport.close ()

② authentication based on secret key

# Import paramiko module

Import paramiko

# define private key address

Private_key = paramiko.RSAKey.from_private_key_file ('/ home/opuser/.ssh/id_rsa')

# instantiate to create a tranport object channel (note that the parameter in parentheses is a tuple)

Transport = paramiko.Transport ('10.0.0.13, 22)

# pass a user name and secret key in the transport channel to create a ssh connection

Transport.connect (username='opuser', pkey=private_key)

# establish a sftp connection

Sftp=paramiko.SFTPClient.from_transport (transport)

# upload location.py to server / tmp/test.py

Sftp.put ('/ tmp/location.py','/ tmp/test.py')

# download remove_path to local local_path

Sftp.get ('remove_path',' local_path')

# turn off sftp

Transport.close ()

Object-oriented encapsulates multiple remote operations

# look at the above paramiko, there are two ways to execute commands, and there is one way to transfer files! And here when the command is connected and then closed, when the file is transferred, it is closed after the transfer, which is not very good! Then we can connect and write two methods to execute the command and upload the file. In addition, when executing commands remotely, it is actually very fast, but the time they spend is basically on establishing a connection, so we have to write it as if the connection was closed after all the last command execution or file upload was done.

#! / usr/bin/env python

#-*-coding:utf8-*-

Import paramiko

Import uuid

Class Haproxy (): #-> create the class Haproxy

Def _ init__ (self):

Self.host='10.0.0.13'

Self.port=22

Self.username='opuser'

Self.password='123456'

Def create_file (self): # File creation method

File_name=str (uuid.uuid4 ()) # reads the file name generated by the uuid method 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): # set the connection method

Transport = paramiko.Transport ((self.host,self.port)) # create a connection object

Transport.connect (username=self.username,password=self.password) # calls the connection method in the transport object

Self.__transport = transport# assign transport to _ _ transport

Def close (self): # close the connection

Self.__transport.close ()

Def upload (self): # upload file method

File_name = self.create_file ()

Sftp = paramiko.SFTPClient.from_transport (self.__transport)

Sftp.put (file_name,'/home/opuser/ttttttt.py')

Def rename (self): # execute the method of changing the name

Ssh = paramiko.SSHClient () # create ssh object

Ssh._transport = self.__transport# replace the ssh_transport field with self.__transport

Stdin,stdout,stderr = ssh.exec_command ('mv / home/opuser/ttttttt.py / home/opuser/oooooo.py') # execute the shell command mv to change the file name

Print stdout.read () # reads the result of the executed command and returns it to the screen

Ha=Haproxy ()

Ha.run ()

The realization of Fortress Machine

Fortress machine execution process:

The administrator creates an account on the server for the user (place the public key on the server, or use the username and password)

Users log in to the fortress machine, enter the user name and password of the fortress machine, and the list of servers managed by the current user.

The user selects the server and logs in automatically

Perform actions and record user actions at the same time

[note: if you want to login, you can execute the script automatically after login is defined in the configuration file .bashrc: for example: / usr/bin/python / home/opuser/menu.py]

① realizes user login

Import getpass user = raw_input ('username:') pwd = getpass.getpass (' password') if user = 'alex' and pwd = =' 123 login: print 'login successful' else: print 'login failed'

② obtains the list of relevant servers based on the user

Dic = {'alex': [' 10.0.0.10, '10.0.0.11,' 10.0.0.12,], 'eric': [' 10.0.0.14,]} host_list = dic ['alex'] print' please select:'for index, item in enumerate (host_list, 1): print index Item inp = raw_input ('your select (No):') inp = int (inp) hostname = host_ list [inp-1] port = 22

③ logs in to the server based on user name and private key

Tran = paramiko.Transport ((hostname, port,)) tran.start_client () default_path = os.path.join (os.environ ['HOME'],' .ssh', 'id_rsa') key = paramiko.RSAKey.from_private_key_file (default_path) tran.auth_publickey (' opuser' Key) # Open a channel chan = tran.open_session () # get a terminal chan.get_pty () # Activator chan.invoke_shell () # use sys.stdin to wantonly perform operations # users enter content at the terminal And send the content to the remote server # remote server to execute the command, and return the result to # user terminal display content # chan.close () tran.close ()

Get a Linux terminal

#! / usr/bin/env python#-*- coding:utf-8-*-import paramikoimport osimport sysimport selectimport sockettran = paramiko.Transport (('10.0.0.13, 22,) tran.start_client ()' # use key authentication default_path = os.path.join (os.environ ['opuser'],' .ssh', 'id_rsa') key = paramiko.RSAKey.from_private_key_file (default_path) tran.auth_publickey (' opuser') Key)''tran.auth_password (' opuser', '123456') # through password authentication chan = tran.open_session () # Open a channel chan.get_pty () # get a terminal chan.invoke_shell () # Activator''# use sys.stdin to wantonly perform operations # user input content at the terminal And send the content to the remote server # remote server execute the command and return the result # user terminal display content''while True: # monitor user input and server return data # sys.stdin handles user input # chan is the previously created channel Used to receive server return information readable, writeable, error = select.select ([chan, sys.stdin,], [], [], 1) # strong chen and terminal # as long as it changes Chan or stdin or both changes if chan in readable: # capture try after a change at the remote end: X = chan.recv (1024) # ssh connection, he also sends and receives data through socket if len (x) = = 0: print'\ r\ nnotify * EOF\ r\ n' Break sys.stdout.write (x) # input the content to the terminal sys.stdout.flush () except socket.timeout: pass if sys.stdin in readable: # when the input is captured by the terminal, inp = sys.stdin.readline () # enter the user's line into chan.sendall (inp) # send a life To remote chan.close () tran.close ()

# in the above example, after the line of command (string) we entered when capturing the output, sys.stdin captured it. This is the default terminal, and we can open a file to record all the commands and operations of the user.

#! / usr/bin/env python#-*- coding:utf-8-*-import paramikoimport osimport sysimport selectimport sockettran = paramiko.Transport (('10.0.0.13, 22,) tran.start_client ()' # use key authentication default_path = os.path.join (os.environ ['opuser'],' .ssh', 'id_rsa') key = paramiko.RSAKey.from_private_key_file (default_path) tran.auth_publickey (' opuser') Key)''tran.auth_password (' opuser', '123456') # through password authentication chan = tran.open_session () # Open a channel chan.get_pty () # get a terminal chan.invoke_shell () # Activator''# use sys.stdin to wantonly perform operations # user input content at the terminal And send the content to the remote server # remote server executes the command and return the result # user terminal display content''log = open (' record','ab') # Open a file to record user input while True: # Monitor user input and server return data # sys.stdin handles user input # chan is a previously created channel Used to receive server return information readable, writeable, error = select.select ([chan, sys.stdin,], [], [], 1) # strong chen and terminal # as long as it changes Chan or stdin or both changes if chan in readable: # capture try after a change at the remote end: X = chan.recv (1024) # ssh connection after he sends and receives data if len (x) = = 0: log.close () # closes the file print'\ r\ n * * EOF *\ r\ n' Break sys.stdout.write (x) # input the content to the terminal sys.stdout.flush () except socket.timeout: pass if sys.stdin in readable: # when the terminal has input captured, inp = sys.stdin.readline () # enter the user's line into log.write (inp) # record Command chan.sendall (inp) # sends a command to the remote chan.close () tran.close ()

Another example is that when we enter a command at the terminal, we often forget all the characters of the command.

# default line wrapping, special handling for special characters, such as Ctrl+c

# change the default of the terminal from line + enter-> stdin to a character-- > stdin

The first thing we need to do is to modify the terminal mode: change the original default from "enter" line feeds and special characters to enter a character to capture and record to the corresponding log file, but do not record the tab key

#! / usr/bin/env python#-*- coding:utf-8-*-import paramikoimport osimport sysimport selectimport socketimport termiosimport ttytran = paramiko.Transport (('10.0.0.13, 22,) tran.start_client ()' # use key authentication default_path = os.path.join (os.environ ['opuser'],' .ssh', 'id_rsa') key = paramiko.RSAKey.from_private_key_file (default_path) tran.auth_publickey (' opuser') Key)''tran.auth_password (' opuser', '123456') # through password authentication chan = tran.open_session () # Open a channel chan.get_pty () # get a terminal chan.invoke_shell () # Activator''# use sys.stdin to wantonly perform operations # user input content at the terminal And send the content to the remote server # remote server to execute the command, and return the result to # user terminal display content''# get the original tty attribute oldtty = termios.tcgetattr (sys.stdin) try: # set the new property for tty # default current tty device property: # enter one line enter, execute # CTRL+C process exit, encounter special characters, special treatment. # this is for the original mode, does not recognize all special symbols # place special characters applied to the current terminal, so set Send all user input to the remote server tty.setraw (sys.stdin.fileno ()) # change the remote end to LINUX raw mode chan.settimeout (0.0) while True: # monitor user input and remote server return data (socket) # blocking Until the handle is readable r, w, e = select.select ([chan, sys.stdin], [], [], 1) if chan in r: try: X = chan.recv (1024) if len (x) = 0: print'\ r\ ndeclare * EOF\ r\ n' Break sys.stdout.write (x) sys.stdout.flush () except socket.timeout: pass if sys.stdin in r: X = sys.stdin.read (1) if len (x) = 0: break chan.send (x) ) finally: # reset the terminal property termios.tcsetattr (sys.stdin Termios.TCSADRAIN, oldtty) chan.close () tran.close ()

Final revised version:

#! / usr/bin/env python#-*- coding:utf-8-*-import paramikoimport osimport sysimport selectimport socketimport termiosimport ttytran = paramiko.Transport (('10.0.0.13, 22,) tran.start_client ()' # use key authentication default_path = os.path.join (os.environ ['opuser'],' .ssh', 'id_rsa') key = paramiko.RSAKey.from_private_key_file (default_path) tran.auth_publickey (' opuser') Key)''tran.auth_password (' opuser', '123456') # through password authentication chan = tran.open_session () # Open a channel chan.get_pty () # get a terminal chan.invoke_shell () # Activator''# use sys.stdin to wantonly perform operations # user input content at the terminal And send the content to the remote server # remote server to execute the command, and return the result # user terminal display content''# get the original tty attribute oldtty = termios.tcgetattr (sys.stdin) # Open file try: # set new properties for tty # default current tty device property: # enter one line enter, execute # CTRL+C process exit, encounter special characters, special treatment # this is for the original mode, does not recognize all special symbols # place special characters applied to the current terminal, so set Send all user input to the remote server tty.setraw (sys.stdin.fileno ()) # change the remote end to LINUX raw mode chan.settimeout (0.0) user_log = open ('terminalnew_log','ab') while True: # monitor user input and remote server return data (socket) # blocking Until the handle is readable r, w, e = select.select ([chan, sys.stdin], [], [], 1) if chan in r: try: X = chan.recv (1024) if len (x) = 0: user_log.close () print'\ r\ nreaders * EOF\ r\ n' Break sys.stdout.write (x) sys.stdout.flush () except socket.timeout: pass if sys.stdin in r: X = sys.stdin.read (1) if len (x) = 0: break if x = ='\ Tab: # determine whether the user is tab if it is tab, pass else: user_log.write (x) # if the command entered by the user is saved to the log chan.send (x) finally: # reset the terminal attribute termios.tcsetattr (sys.stdin Termios.TCSADRAIN, oldtty) chan.close () tran.close ()

Open the terminal under Windows

#! / usr/bin/env python#-*- coding:utf-8-*-import paramikoimport sysimport threadingtran = paramiko.Transport (('10.0.0.13, 22,) tran.start_client ()' # use key authentication default_path = os.path.join (os.environ ['opuser'],' .ssh', 'id_rsa') key = paramiko.RSAKey.from_private_key_file (default_path) tran.auth_publickey (' opuser') Key)''tran.auth_password (' opuser', '123456') # through password authentication chan = tran.open_session () # Open a channel chan.get_pty () # get a terminal chan.invoke_shell () # Activator''# use sys.stdin to wantonly perform operations # user input content at the terminal The content is sent to the remote server # remote server to execute the command, and the result is returned to # user terminal to display the content 'sys.stdout.write ("Line-buffered terminal emulation. Press F6 or ^ Z to send EOF.\ r\ n\ r\ n ") def writeall (sock): while True: data = sock.recv''SSH also sends data through socket, so we can use socket to get the data sent back by the remote machine. The while loop receives data all the time, and sock.recv (256) is blocked and will continue to go only when the data comes in. '' if not data: sys.stdout.write ('\ r\ nclients * EOF * *\ r\ n\ r\ n') sys.stdout.flush () break sys.stdout.write (data) sys.stdout.flush () writer = threading.Thread (target=writeall, args= (chan,)) # created a thread to execute the writeall method The parameter is chan (established SSH connection) writer.start () try: while True: # main thread loop d = sys.stdin.read (1) # always monitor the user's input, enter one to send an if not d: break chan.send (d) except EOFError: # user hit ^ Z or F6 passchan.close () tran.close ()

Basic operation of database

Installation of MySQL module for Python operation

Linux:

Yum install MySQL-python

Windows:

Http://files.cnblogs.com/files/wupeiqi/py-mysql-win.zip

Basic use of SQL

1. Database operation

Show databases

Use [databasename]

Create database [name]

2. Table operation of database

Show tables

Create table students

(

Id int not null auto_increment primary key

Name char (8) not null

Sex char (4) not null

Age tinyint unsigned not null

Tel char (13) null default "-

);

Sample code:

CREATE TABLE `wb_ blog` (

`id`smallint (8) unsigned NOT NULL

`catid` smallint (5) unsigned NOT NULL DEFAULT'0'

`title`varchar (80) NOT NULL DEFAULT''

`content` text NOT NULL

PRIMARY KEY (`id`)

UNIQUE KEY `catename` (`catid`)

)

3. Data operation

Insert into students (name,sex,age,tel) values ('alex','man',18,'151515151')

Delete from students where id = 2

Update students set name = 'sb' where id = 1

Select * from students

4. Other

Primary key

Foreign key

Left and right connection

Python MySQL API

Insert data

Import MySQLdb

Conn = MySQLdb.connect (host='127.0.0.1',user='root',passwd='1234',db='mydb')

Cur = conn.cursor ()

ReCount = cur.execute ('insert into UserInfo (Name,Address) values (% s)', ('alex','usa'))

# reCount = cur.execute ('insert into UserInfo (Name,Address) values (% (id) s,% (name) s)', {'id':12345,'name':'opuser'})

Conn.commit ()

Cur.close ()

Conn.close ()

Print reCount

Batch insert

Import MySQLdb

Conn = MySQLdb.connect (host='127.0.0.1',user='root',passwd='1234',db='mydb')

Cur = conn.cursor ()

Li = [

('alex','usa')

('sb','usa')

]

ReCount = cur.executemany ('insert into UserInfo (Name,Address) values (% s)', li)

Conn.commit ()

Cur.close ()

Conn.close ()

Print reCount

Note: cur.lastrowid

2. Delete data

Import MySQLdb

Conn = MySQLdb.connect (host='127.0.0.1',user='root',passwd='1234',db='mydb')

Cur = conn.cursor ()

ReCount = cur.execute ('delete from UserInfo')

Conn.commit ()

Cur.close ()

Conn.close ()

Print reCount

Fourth, check the data

# fetchone/fetchmany (num) # #

Import MySQLdb

Conn = MySQLdb.connect (host='127.0.0.1',user='root',passwd='1234',db='mydb')

Cur = conn.cursor ()

ReCount = cur.execute ('select * from UserInfo')

Print cur.fetchone ()

Print cur.fetchone ()

Cur.scroll (- 1)

Print cur.fetchone ()

Print cur.fetchone ()

Cur.scroll (0thecontrolling)

Print cur.fetchone ()

Print cur.fetchone ()

Cur.close ()

Conn.close ()

Print reCount

# fetchall # #

Import MySQLdb

Conn = MySQLdb.connect (host='127.0.0.1',user='root',passwd='1234',db='mydb')

# cur = conn.cursor (cursorclass = MySQLdb.cursors.DictCursor)

Cur = conn.cursor ()

ReCount = cur.execute ('select Name,Address from UserInfo')

NRet = cur.fetchall ()

Cur.close ()

Conn.close ()

Print reCount

Print nRet

For i in nRet:

Print i [0], i [1]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report