In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use ShutIt, the shell automation framework based on Python, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
ShutIt is an easy-to-use shell-based automation framework. It wraps the python-based expect library (pexpect). You can think of it as "expect without pain points". It can be installed through pip.
Hello World
Let's start with the simplest example. Create a file named example.py:
Import shutit session = shutit.create_session ('bash') session.send (' echo Hello World', echo=True)
Run this file:
Python example.py
Output:
Python example.py echo "Hello World" echo "Hello World" Hello World Ians-MacBook-Air.local:ORIGIN_ENV:RhuebR2T#
The * * parameters of the "send" function are the commands to be run. The parameters of "echo" will be output to the terminal. By default, ShutIt is silent.
Log in to the server
If you want to log in to a server and execute the commands on the server. You can change example.py to:
Import shutit session = shutit.create_session ('bash') session.login (' ssh you@example.com', user='you', password='mypassword') session.send ('hostname', echo=True) session.logout ()
The program will log in to this server and output the hostname.
Hostname hostname example.com example.com:cgoIsdVv:heDa77HB#
Obviously, it's not safe! You can run this:
Import shutit session = shutit.create_session ('bash') password= session.get_input (', ispass=True) session.login ('ssh you@example.com', user='you', password=password) session.send (' hostname', echo=True) session.logout ()
It will ask you to enter your password:
Input Secret: hostname hostname example.com example.com:cgoIsdVv:heDa77HB#
Similarly, the "login" method changes the prompt after logging in. You give ShutIt a login command with a user name and password (if necessary), and ShutIt does the rest.
"logout" is responsible for terminating "login" and outputting any changes to the screen.
Log in to multiple servers
Suppose you have a cluster of two servers and want to log in to both servers at the same time. You only need to create two sessions and run similar login and send commands:
Import shutit session1 = shutit.create_session ('bash') session2 = shutit.create_session (' bash') password1 = session1.get_input ('Password for server1', ispass=True) password2 = session2.get_input (' Password for server2', ispass=True) session1.login ('ssh you@one.example.com', user='you', password=password1) session2.login (' ssh you@two.example.com', user='you', password=password2) session1.send ('hostname', echo=True) session2.send (' hostname') Echo=True) session1.logout () session2.logout ()
The result will be output as follows:
$python example.py Password for server1 Input Secret: Password for server2 Input Secret: hostname hostname one.example.com one.example.com:Fnh3pyFj:qkrsmUNs# hostname hostname two.example.com two.example.com:Gl2lldEo:D3FavQjA#
Example: monitoring multiple servers
We can turn the above code into a simple monitoring tool by adding some code logic to check the output of the command:
Import shutit capacity_command= "" df / | awk'{print $5}'| tail-1 | sed s / [^ 0-9] / / "" session1 = shutit.create_session ('bash') session2 = shutit.create_session (' bash') password1 = session.get_input ('Password for server1', ispass=True) password2 = session.get_input (' Password for server2', ispass=True) session1.login ('ssh you@one.example.com', user='you') Password=password1) session2.login ('ssh you@two.example.com', user='you', password=password2) capacity = session1.send_and_get_output (capacity_command) if int (capacity)
< 10: print('RUNNING OUT OF SPACE ON server1!') capacity = session2.send_and_get_output(capacity_command) if int(capacity) < 10: print('RUNNING OUT OF SPACE ON server2!') session1.logout() session2.logout() 在这里,我们用了"sendandget_output"方法来获取capacity_command命令的输出。 还有很多更加优雅的方法可以完成上面的操作,但这取决于你想要Python有多聪明。 更复杂的IO – Expecting 假设你需要跟一个命令行程序进行交互,并且要实现自动化操作。在这里,我们使用telnet来举一个简单的例子: import shutit session = shutit.create_session('bash') session.send('telnet', expect='elnet>', echo=True) session.send (' open google.com 80, expect='scape character', echo=True) session.send ('GET /', echo=True, check_exit=False) session.logout ()
Note the parameters of "expect". You only need to give a subset of the telnet prompt to match.
Pay attention to the parameter "check_exit", which we will talk about later. The above code will output:
$python example.py telnet telnet > open google.com 80 Trying 216.58.214.14... Connected to google.com. Escape character is'^]'. GET / HTTP/1.0 302 Found Cache-Control: private Content-Type: text/html; charset=UTF-8 Referrer-Policy: no-referrer Location: http://www.google.co.uk/?gfe_rd=cr&ei=huczWcj3GfTW8gfq0paQDA Content-Length: 261 Date: Sun, 04 Jun 2017 10:57:10 GMT 302 Moved 302 Moved The document has moved here. Connection closed by foreign host.
Now come back to "checkexit = false". Since the telnet command returns an incorrect exit code (1), we don't want the script execution to fail, and the "checkexit = false" here lets ShutIt know that you don't care about the exit code.
If you don't pass in this parameter, ShutIt will give you an interactive prompt if you have terminal access. This is called a "pause point".
Pause point
You can set a "pause point" at any time by calling the following method.
[...] Session.pause_point ('This is a pause point') [...]
When the script reaches the pause point, press "Ctrl" and "]" at the same time to allow the script to continue execution. This is useful for debugging: add a pause point, look around, and then continue. Try this:
Import shutit session = shutit.create_session ('bash') session.pause_point (' Have a look circles') Session.send ('echo "Did you enjoy your pause point?", echo=True)
Program output:
$python example.py Have a look around! Ians-Air.home:ORIGIN_ENV:I00LA1Mq# bash imiell@Ians-Air:/space/git/shutit ⑂ master + CTRL-] caught, continuing with run... 2017-06-05 15 INFO 12 INFO: 33577 INFO: Sending: exit 2017-06-05 15 Swiss 12 charge 33633 INFO: Output (squashed): exitexitIans-Air.home:ORIGIN_ENV:I00LA1Mq# [...] Echo "Did you enjoy your pause point?" Echo "Did you enjoy your pause point?" Did you enjoy your pause point? Ians-Air.home:ORIGIN_ENV:I00LA1Mq#
More complex IO-Backgrounding
Go back to our example of "monitoring multiple servers" above. Imagine that we want to run a long-running task on each server. By default, ShutIt runs continuously for a long time. But we can run tasks in the background to speed up ShutIt.
Here, you can use the simple command "sleep 60" to try an example.
Import shutit import time long_command= "" sleep 60 "" session1 = shutit.create_session ('bash') session2 = shutit.create_session (' bash') password1 = session1.get_input ('Password for server1', ispass=True) password2 = session2.get_input (' Password for server2', ispass=True) session1.login ('ssh you@one.example.com', user='you', password=password1) session2.login (' ssh you@two.example.com', user='you') Password=password2) start = time.time () session1.send (long_command, background=True) session2.send (long_command, background=True) print ('That took:' + str (time.time ()-start) + 'seconds to fire') session1.wait () session2.wait () print (' That took:'+ str (time.time ()-start) + 'seconds to complete')
My laptop says that it only takes half a second to run these two commands, while the script ends a minute later (using the 'wait' method).
Although this example may seem trivial, imagine that if you have hundreds of such servers to manage, you can see the power of these lines of code and a python import.
After reading the above, do you have any further understanding of how to use ShutIt, the Python-based shell automation framework? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.