jenkins:集成sonar代码扫描+发送邮件

前提:

Jenkins

JDK

目录:

1、安装sonar插件:SonarQube Scanner for Jenkins

2、安装SonarQube

3、安装sonar-scanner

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1.安装sonar插件:SonarQube Scanner for Jenkins

2、安装SonarQube,并配置mysql数据库

2.1 下载

下载地址:https://www.sonarqube.org/downloads/

  2.2 解压

下载后放在/usr/local目录下,并解压

  1. unzip sonarqube7.4.zip

 2.3 配置环境变量

  1. vi /etc/profile

里面写如下

  1. #set sonarqube
  2. export SONAR_HOME=/usr/local/sonarqube7.4
  3. PATH=$PATH:$SONAR_HOME/bin

使配置生效

  1. source /etc/profile

 2.4 启动并测试

进入到启动目录

  1. /usr/local/sonarqube7.4/bin/linuxx8664

执行启动命令

  1. ./sonar.sh start

启动时要查看es.log和sonar.log日志,还有web.log,关于数据库配置的错误会在此处显示

  1. tail f /usr/local/sonarqube6.7.6/logs/es.log
  1. tail f /usr/local/sonarqube6.7.6/logs/sonar.log
  1. tail f /usr/local/sonarqube6.7./logs/web.log

用root启动会报错,请参考我的另一篇文章:https://www.cnblogs.com/gcgc/p/10239590.html

所有问题解决完后启动,并测试,访问ip+port,OK了

如果默认的9000端口被占用了,则修改默认端口 vi /usr/local/sonarqube-7.4/conf/sonar.properties

注意sonar中还集成了elastic search默认端口为9001,所以要查看9001否也被占用lsof -i:9001,如果被占用,也是修改上面的文件

  2.5 sonar配置mysql数据库

sonar自带测试数据库,但是具体应用还是的换其他数据,这里我们使用mysql,

创建sonar数据库

  1. DROP DATABASE sonar;
  2.  
  3. CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
  4.  
  5. CREATE USER ‘sonar’ IDENTIFIED BY ‘sonar’;
  6.  
  7. GRANT ALL ON sonar.* TO ‘sonar’@‘%’ IDENTIFIED BY ‘sonar’;
  8.  
  9. GRANT ALL ON sonar.* TO ‘sonar’@‘localhost’ IDENTIFIED BY ‘sonar’;
  10.  
  11. FLUSH PRIVILEGES;

2.6 修改配置文件

修改vi /usr/local/sonarqube-7.4/conf/sonar.properties文件,如下的蓝色加粗字体

  1. # Property values can:
  2. # – reference an environment variable, for example sonar.jdbc.url= ${env:SONAR_JDBC_URL}
  3. # – be encrypted. See https://redirect.sonarsource.com/doc/settings-encryption.html
  4.  
  5. #————————————————————————————————–
  6. # DATABASE
  7. #
  8. # IMPORTANT:
  9. # – The embedded H2 database is used by default. It is recommended for tests but not for
  10. # production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer.
  11. # – Changes to database connection URL (sonar.jdbc.url) can affect SonarSource licensed products.
  12.  
  13. # User credentials.
  14. # Permissions to create tables, indices and triggers must be granted to JDBC user.
  15. # The schema must be created first.
  16. #sonar.jdbc.username=
  17. #sonar.jdbc.password=
  18. sonar.jdbc.username=sonar
  19. sonar.jdbc.password=sonar
  20. sonar.sorceEncoding=UTF8
  21. sonar.login=admin
  22. sonar.password=admin
  23.  
  24. #—– Embedded Database (default)
  25. # H2 embedded database server listening port, defaults to
  26. #sonar.embeddedDatabase.port=
  27.  
  28. #—– DEPRECATED
  29. #—– MySQL >=5.6 && <8.0
  30. # Support of MySQL is dropped in Data Center Editions and deprecated in all other editions
  31. # Only InnoDB storage engine is supported (not myISAM).
  32. # Only the bundled driver is supported. It can not be changed.
  33. sonar.jdbc.url=jdbc:mysql://192.168.207.160:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
  34.  
  35. #—– Oracle 11g/12c
  36. # The Oracle JDBC driver must be copied into the directory extensions/jdbc-driver/oracle/.
  37. # Only the thin client is supported, and only the versions 11.2.x or 12.2.x must be used. See
  38. # https://jira.sonarsource.com/browse/SONAR-9758 for more details.
  39. # If you need to set the schema, please refer to http://jira.sonarsource.com/browse/SONAR-5000
  40. #sonar.jdbc.url=jdbc:oracle:thin:@localhost:/XE
  41.  
  42. #—– PostgreSQL 9.3 or greater
  43. # By default the schema named “public” is used. It can be overridden with the parameter “currentSchema”.
  44. #sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube?currentSchema=my_schema
  45.  
  46. #—– Microsoft SQLServer / and SQL Azure
  47. # A database named sonar must exist and its collation must be case-sensitive (CS) and accent-sensitive (AS)
  48. # Use the following connection string if you want to use integrated security with Microsoft Sql Server
  49. # Do not set sonar.jdbc.username or sonar.jdbc.password property if you are using Integrated Security
  50. # For Integrated Security to work, you have to download the Microsoft SQL JDBC driver package from
  51. # https://www.microsoft.com/en-us/download/details.aspx?id=55539
  52. # and copy sqljdbc_auth.dll to your path. You have to copy the bit or bit version of the dll
  53. # depending upon the architecture of your server machine.
  54. #sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar;integratedSecurity=true
  55.  
  56. # Use the following connection string if you want to use SQL Auth while connecting to MS Sql Server.
  57. # Set the sonar.jdbc.username and sonar.jdbc.password appropriately.
  58. #sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar
  59.  
  60. #—– Connection pool settings
  61. # The maximum number of active connections that can be allocated
  62. # at the same time, or negative for no limit.
  63. # The recommended value is 1.2 * max sizes of HTTP pools. For example if HTTP ports are
  64. # enabled with default sizes (, see property sonar.web.http.maxThreads)
  65. # then sonar.jdbc.maxActive should be 1.2 * = .
  66. #sonar.jdbc.maxActive=
  67.  
  68. # The maximum number of connections that can remain idle in the
  69. # pool, without extra ones being released, or negative for no limit.
  70. #sonar.jdbc.maxIdle=
  71.  
  72. # The minimum number of connections that can remain idle in the pool,
  73. # without extra ones being created, or zero to create none.
  74. #sonar.jdbc.minIdle=
  75.  
  76. # The maximum number of milliseconds that the pool will wait (when there
  77. # are no available connections) for a connection to be returned before
  78. # throwing an exception, or <= to wait indefinitely.
  79. #sonar.jdbc.maxWait=
  80.  
  81. #sonar.jdbc.minEvictableIdleTimeMillis=
  82. #sonar.jdbc.timeBetweenEvictionRunsMillis=
  83.  
  84. #————————————————————————————————–
  85. # WEB SERVER
  86. # Web server is executed in a dedicated Java process. By default heap size is 512Mb.
  87. # Use the following property to customize JVM options.
  88. # Recommendations:
  89. #
  90. # The HotSpot Server VM is recommended. The property -server should be added if server mode
  91. # is not enabled by default on your environment:
  92. # http://docs.oracle.com/javase/8/docs/technotes/guides/vm/server-class.html
  93. #
  94. # Startup can be long if entropy source is short of entropy. Adding
  95. # -Djava.security.egd=file:/dev/./urandom is an option to resolve the problem.
  96. # See https://wiki.apache.org/tomcat/HowTo/FasterStartUp#Entropy_Source
  97. #
  98. #sonar.web.javaOpts=-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError
  99.  
  100. # Same as previous property, but allows to not repeat all other settings like -Xmx
  101. #sonar.web.javaAdditionalOpts=
  102.  
  103. # Binding IP address. For servers with more than one IP address, this property specifies which
  104. # address will be used for listening on the specified ports.
  105. # By default, ports will be used on all IP addresses associated with the server.
  106. #sonar.web.host=0.0.0.0
  107.  
  108. # Web context. When set, it must start with forward slash (for example /sonarqube).
  109. # The default value is root context (empty value).
  110. #sonar.web.context=
  111. # TCP port for incoming HTTP connections. Default value is .
  112. sonar.web.port=9000
  113.  
  114. # The maximum number of connections that the server will accept and process at any given time.
  115. # When this number has been reached, the server will not accept any more connections until
  116. # the number of connections falls below this value. The operating system may still accept connections
  117. # based on the sonar.web.connections.acceptCount property. The default value is .
  118. #sonar.web.http.maxThreads=
  119.  
  120. # The minimum number of threads always kept running. The default value is .
  121. #sonar.web.http.minThreads=
  122.  
  123. # The maximum queue length for incoming connection requests when all possible request processing
  124. # threads are in use. Any requests received when the queue is full will be refused.
  125. # The default value is .
  126. #sonar.web.http.acceptCount=
  127.  
  128. # By default users are logged out and sessions closed when server is restarted.
  129. # If you prefer keeping user sessions open, a secret should be defined. Value is
  130. # HS256 key encoded with base64. It must be unique for each installation of SonarQube.
  131. # Example of command-line:
  132. # echo -n “type_what_you_want” | openssl dgst -sha256 -hmac “key” -binary | base64
  133. #sonar.auth.jwtBase64Hs256Secret=
  134.  
  135. # The inactivity timeout duration of user sessions, in minutes. After the configured
  136. # period of time, the user is logged out.
  137. # The default value is set to days ( minutes)
  138. # and cannot be greater than months. Value must be strictly positive.
  139. #sonar.web.sessionTimeoutInMinutes=
  140.  
  141. # A passcode can be defined to access some web services from monitoring
  142. # tools without having to use the credentials of a system administrator.
  143. # Check the Web API documentation to know which web services are supporting this authentication mode.
  144. # The passcode should be provided in HTTP requests with the header “X-Sonar-Passcode”.
  145. # By default feature is disabled.
  146. #sonar.web.systemPasscode=
  147.  
  148. #————————————————————————————————–
  149. # SSO AUTHENTICATION
  150.  
  151. # Enable authentication using HTTP headers
  152. #sonar.web.sso.enable=false
  153.  
  154. # Name of the header to get the user login.
  155. # Only alphanumeric, ‘.’ and ‘@’ characters are allowed
  156. #sonar.web.sso.loginHeader=X-Forwarded-Login
  157.  
  158. # Name of the header to get the user name
  159. #sonar.web.sso.nameHeader=X-Forwarded-Name
  160.  
  161. # Name of the header to get the user email (optional)
  162. #sonar.web.sso.emailHeader=X-Forwarded-Email
  163.  
  164. # Name of the header to get the list of user groups, separated by comma (optional).
  165. # If the sonar.sso.groupsHeader is set, the user will belong to those groups if groups exist in SonarQube.
  166. # If none of the provided groups exists in SonarQube, the user will only belong to the default group.
  167. # Note that the default group will always be set.
  168. #sonar.web.sso.groupsHeader=X-Forwarded-Groups
  169.  
  170. # Interval used to know when to refresh name, email and groups.
  171. # During this interval, if for instance the name of the user is changed in the header, it will only be updated after X minutes.
  172. #sonar.web.sso.refreshIntervalInMinutes=
  173.  
  174. #————————————————————————————————–
  175. # LDAP CONFIGURATION
  176.  
  177. # Enable the LDAP feature
  178. # sonar.security.realm=LDAP
  179.  
  180. # Set to true when connecting to a LDAP server using a case-insensitive setup.
  181. # sonar.authenticator.downcase=true
  182.  
  183. # URL of the LDAP server. Note that if you are using ldaps, then you should install the server certificate into the Java truststore.
  184. # ldap.url=ldap://localhost:10389
  185.  
  186. # Bind DN is the username of an LDAP user to connect (or bind) with. Leave this blank for anonymous access to the LDAP directory (optional)
  187. # ldap.bindDn=cn=sonar,ou=users,o=mycompany
  188.  
  189. # Bind Password is the password of the user to connect with. Leave this blank for anonymous access to the LDAP directory (optional)
  190. # ldap.bindPassword=secret
  191.  
  192. # Possible values: simple | CRAM-MD5 | DIGEST-MD5 | GSSAPI See http://java.sun.com/products/jndi/tutorial/ldap/security/auth.html (default: simple)
  193. # ldap.authentication=simple
  194.  
  195. # See :
  196. # * http://java.sun.com/products/jndi/tutorial/ldap/security/digest.html
  197. # * http://java.sun.com/products/jndi/tutorial/ldap/security/crammd5.html
  198. # (optional)
  199. # ldap.realm=example.org
  200.  
  201. # Context factory class (optional)
  202. # ldap.contextFactoryClass=com.sun.jndi.ldap.LdapCtxFactory
  203.  
  204. # Enable usage of StartTLS (default : false)
  205. # ldap.StartTLS=true
  206.  
  207. # Follow or not referrals. See http://docs.oracle.com/javase/jndi/tutorial/ldap/referral/jndi.html (default: true)
  208. # ldap.followReferrals=false
  209.  
  210. # USER MAPPING
  211.  
  212. # Distinguished Name (DN) of the root node in LDAP from which to search for users (mandatory)
  213. # ldap.user.baseDn=cn=users,dc=example,dc=org
  214.  
  215. # LDAP user request. (default: (&(objectClass=inetOrgPerson)(uid={login})) )
  216. # ldap.user.request=(&(objectClass=user)(sAMAccountName={login}))
  217.  
  218. # Attribute in LDAP defining the user’s real name. (default: cn)
  219. # ldap.user.realNameAttribute=name
  220.  
  221. # Attribute in LDAP defining the user’s email. (default: mail)
  222. # ldap.user.emailAttribute=email
  223.  
  224. # GROUP MAPPING
  225.  
  226. # Distinguished Name (DN) of the root node in LDAP from which to search for groups. (optional, default: empty)
  227. # ldap.group.baseDn=cn=groups,dc=example,dc=org
  228.  
  229. # LDAP group request (default: (&(objectClass=groupOfUniqueNames)(uniqueMember={dn})) )
  230. # ldap.group.request=(&(objectClass=group)(member={dn}))
  231.  
  232. # Property used to specifiy the attribute to be used for returning the list of user groups in the compatibility mode. (default: cn)
  233. # ldap.group.idAttribute=sAMAccountName
  234.  
  235. #————————————————————————————————–
  236. # COMPUTE ENGINE
  237. # The Compute Engine is responsible for processing background tasks.
  238. # Compute Engine is executed in a dedicated Java process. Default heap size is 512Mb.
  239. # Use the following property to customize JVM options.
  240. # Recommendations:
  241. #
  242. # The HotSpot Server VM is recommended. The property -server should be added if server mode
  243. # is not enabled by default on your environment:
  244. # http://docs.oracle.com/javase/8/docs/technotes/guides/vm/server-class.html
  245. #
  246. #sonar.ce.javaOpts=-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError
  247.  
  248. # Same as previous property, but allows to not repeat all other settings like -Xmx
  249. #sonar.ce.javaAdditionalOpts=
  250.  
  251. #————————————————————————————————–
  252. # ELASTICSEARCH
  253. # Elasticsearch is used to facilitate fast and accurate information retrieval.
  254. # It is executed in a dedicated Java process. Default heap size is 512Mb.
  255. #
  256. # ————————————————–
  257. # Word of caution for Linux users on 64bits systems
  258. # ————————————————–
  259. # Please ensure Virtual Memory on your system is correctly configured for Elasticsearch to run properly
  260. # (see https://www.elastic.co/guide/en/elasticsearch/reference/5.5/vm-max-map-count.html for details).
  261. #
  262. # When SonarQube runs standalone, a warning such as the following may appear in logs/es.log:
  263. # “max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]”
  264. # When SonarQube runs as a cluster, however, Elasticsearch will refuse to start.
  265. #
  266.  
  267. # JVM options of Elasticsearch process
  268. #sonar.search.javaOpts=-Xms512m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError
  269.  
  270. # Same as previous property, but allows to not repeat all other settings like -Xmx
  271. #sonar.search.javaAdditionalOpts=
  272.  
  273. # Elasticsearch port. Default is . Use to get a free port.
  274. # As a security precaution, should be blocked by a firewall and not exposed to the Internet.
  275. sonar.search.port=9002
  276.  
  277. # Elasticsearch host. The search server will bind this address and the search client will connect to it.
  278. # Default is loopback address.
  279. # As a security precaution, should NOT be set to a publicly available address.
  280. #sonar.search.host=
  281.  
  282. #————————————————————————————————–
  283. # UPDATE CENTER
  284.  
  285. # Update Center requires an internet connection to request https://update.sonarsource.org
  286. # It is enabled by default.
  287. #sonar.updatecenter.activate=true
  288.  
  289. # HTTP proxy (default none)
  290. #http.proxyHost=
  291. #http.proxyPort=
  292. # HTTPS proxy (defaults are values of http.proxyHost and http.proxyPort)
  293. #https.proxyHost=
  294. #https.proxyPort=
  295.  
  296. # NT domain name if NTLM proxy is used
  297. #http.auth.ntlm.domain=
  298.  
  299. # SOCKS proxy (default none)
  300. #socksProxyHost=
  301. #socksProxyPort=
  302.  
  303. # Proxy authentication (used for HTTP, HTTPS and SOCKS proxies)
  304. #http.proxyUser=
  305. #http.proxyPassword=
  306.  
  307. #————————————————————————————————–
  308. # LOGGING
  309.  
  310. # SonarQube produces logs in logs files located in the same directory (see property sonar.path.logs below),
  311. # one per process:
  312. # Main process (aka. App) logs in sonar.log
  313. # Web Server (aka. Web) logs in web.log
  314. # Compute Engine (aka. CE) logs in ce.log
  315. # Elasticsearch (aka. ES) logs in es.log
  316. #
  317. # All files follow the same rolling policy (see sonar.log.rollingPolicy and sonar.log.maxFiles) but it applies
  318. # individually (eg. if sonar.log.maxFiles=, there can be at most of each files, ie. files in total).
  319. #
  320. # All files have logs in the same format:
  321. #
  322. # |—————–| |—| |-|——————–||——————————| |——————————————————————————————————————————|
  323. # 2016.11. :: INFO ce[AVht0dNXFcyiYejytc3m][o.s.s.c.t.CeWorkerCallableImpl] Executed task | project=org.sonarqube:example-java-maven | type=REPORT | id=AVht0dNXFcyiYejytc3m | submitter=admin | time=1699ms
  324. #
  325. # : timestamp. Format is YYYY.MM.DD HH:MM:SS
  326. # YYYY: year on digits
  327. # MM: month on digits
  328. # DD: day on digits
  329. # HH: hour of day on digits in hours format
  330. # MM: minutes on digits
  331. # SS: seconds on digits
  332. # : log level.
  333. # Possible values (in order of descending criticality): ERROR, WARN, INFO, DEBUG and TRACE
  334. # : process identifier. Possible values: app (main), web (Web Server), ce (Compute Engine) and es (Elasticsearch)
  335. # : SQ thread identifier. Can be empty.
  336. # In the Web Server, if present, it will be the HTTP request ID.
  337. # In the Compute Engine, if present, it will be the task ID.
  338. # : logger name. Usually a class canonical name.
  339. # Package names are truncated to keep the whole field to characters max
  340. # : log payload. Content of this field does not follow any specific format, can vary in length and include line returns.
  341. # Some logs, however, will follow the convention to provide data in payload in the format ” | key=value”
  342. # Especially, log of profiled pieces of code will end with ” | time=XXXXms”.
  343.  
  344. # Global level of logs (applies to all processes).
  345. # Supported values are INFO (default), DEBUG and TRACE
  346. #sonar.log.level=INFO
  347.  
  348. # Level of logs of each process can be controlled individually with their respective properties.
  349. # When specified, they overwrite the level defined at global level.
  350. # Supported values are INFO, DEBUG and TRACE
  351. #sonar.log.level.app=INFO
  352. #sonar.log.level.web=INFO
  353. #sonar.log.level.ce=INFO
  354. #sonar.log.level.es=INFO
  355.  
  356. # Path to log files. Can be absolute or relative to installation directory.
  357. # Default is <installation home>/logs
  358. #sonar.path.logs=logs
  359.  
  360. # Rolling policy of log files
  361. # – based on time if value starts with “time:”, for example by day (“time:yyyy-MM-dd”)
  362. # or by month (“time:yyyy-MM”)
  363. # – based on size if value starts with “size:”, for example “size:10MB”
  364. # – disabled if value is “none”. That needs logs to be managed by an external system like logrotate.
  365. #sonar.log.rollingPolicy=time:yyyy-MM-dd
  366.  
  367. # Maximum number of files to keep if a rolling policy is enabled.
  368. # – maximum value is on size rolling policy
  369. # – unlimited on time rolling policy. Set to zero to disable old file purging.
  370. #sonar.log.maxFiles=
  371.  
  372. # Access log is the list of all the HTTP requests received by server. If enabled, it is stored
  373. # in the file {sonar.path.logs}/access.log. This file follows the same rolling policy as other log file
  374. # (see sonar.log.rollingPolicy and sonar.log.maxFiles).
  375. #sonar.web.accessLogs.enable=true
  376.  
  377. # Format of access log. It is ignored if sonar.web.accessLogs.enable=false. Possible values are:
  378. # – “common” is the Common Log Format, shortcut to: %h %l %u %user %date “%r” %s %b
  379. # – “combined” is another format widely recognized, shortcut to: %h %l %u [%t] “%r” %s %b “%i{Referer}” “%i{User-Agent}”
  380. # – else a custom pattern. See http://logback.qos.ch/manual/layouts.html#AccessPatternLayout.
  381. # The login of authenticated user is not implemented with “%u” but with “%reqAttribute{LOGIN}” (since version 6.1).
  382. # The value displayed for anonymous users is “-“.
  383. # The SonarQube’s HTTP request ID can be added to the pattern with “%reqAttribute{ID}” (since version 6.2).
  384. # If SonarQube is behind a reverse proxy, then the following value allows to display the correct remote IP address:
  385. #sonar.web.accessLogs.pattern=%i{X-Forwarded-For} %l %u [%t] “%r” %s %b “%i{Referer}” “%i{User-Agent}” “%reqAttribute{ID}”
  386. # Default value (which was “combined” before version 6.2) is equivalent to “combined + SQ HTTP request ID”:
  387. #sonar.web.accessLogs.pattern=%h %l %u [%t] “%r” %s %b “%i{Referer}” “%i{User-Agent}” “%reqAttribute{ID}”
  388.  
  389. #————————————————————————————————–
  390. # OTHERS
  391.  
  392. # Delay in seconds between processing of notification queue. Default is seconds.
  393. #sonar.notifications.delay=
  394.  
  395. # Paths to persistent data files (embedded database and search index) and temporary files.
  396. # Can be absolute or relative to installation directory.
  397. # Defaults are respectively <installation home>/data and <installation home>/temp
  398. #sonar.path.data=data
  399. #sonar.path.temp=temp
  400.  
  401. # Telemetry – Share anonymous SonarQube statistics
  402. # By sharing anonymous SonarQube statistics, you help us understand how SonarQube is used so we can improve the product to work even better for you.
  403. # We don’t collect source code or IP addresses. And we don’t share the data with anyone else.
  404. # To see an example of the data shared: login as a global administrator, call the WS api/system/info and check the Statistics field.
  405. #sonar.telemetry.enable=true
  406.  
  407. #————————————————————————————————–
  408. # DEVELOPMENT – only for developers
  409. # The following properties MUST NOT be used in production environments.
  410.  
  411. # Elasticsearch HTTP connector
  412. #sonar.search.httpPort=-

重新启动sonar,好了测试一下

3、安装sonar-scanner

3.1 下载

https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner

3.2 解压

下载后放在/usr/local目录下,并解压

  1. unzip sonarscannercli3.2.0.1227linux.zip

3.3 配置环境变量

和上面一样修改/etc/profile文件,修改完记得 source /etc/profile

  1. #set sonarqube
  2. export SONAR_HOME=/usr/local/sonarqube7.4
  3. export SONAR_SCANNER_HOME=/usr/local/sonarscanner3.2.0.1227linux
  4. PATH=$PATH:$SONAR_HOME/bin:$SONAR_SCANNER_HOME/bin

修改完记得 source /etc/profile

3.4 测试

进入/usr/local/sonar-scanner-3.2.0.1227-linux/bin执行:

  1. ./sonarscanner h

如果出现如下就说明安装好了

  1. [root@iZbp1bb2egi7w0ueys548qZ bin]# ./sonarscanner h
  2. INFO:
  3. INFO: usage: sonarscanner [options]
  4. INFO:
  5. INFO: Options:
  6. INFO: D,–define <arg> Define property
  7. INFO: h,–help Display help information
  8. INFO: v,–version Display version information
  9. INFO: X,–debug Produce execution debug output

4、在Jenkins中配置sonar

SonarScanner和SonarQube的关系类似于客户端与服务端,由于SonarScanner工具需要把扫描的代码及结果发送到SonarQube服务器上,所以需要配置SonarQube服务地址。

在Jenkins系统配置>>系统设中配置如下:

5、在Jenkins中配置sonar-scanner

全局工具配置中配置如下:

6、增加构建步骤

Analysis properties 内容如下

  1. sonar.projectKey=eauth
  2. sonar.projectName=电商用户服务-auth
  3. sonar.projectVersion=1.0
  4. sonar.sources=./
  5. sonar.language=java
  6. sonar.sourceEncoding=UTF
  7. sonar.java.binaries=./
  8. sonar.login=admin
  9. sonar.password=admin

7、构建一下

8、添加自定义邮件模板

在jenkins所在机器上/root/.jenkins目录下,新建/sonar_script文件夹,里面放如下脚本:sonar.py和table.html

sonar.py内容如下

  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3.  
  4. import pymysql,os,sys
  5. from jinja2 import FileSystemLoader,Environment
  6.  
  7. def select_project_uuid(project_name):
  8. db = pymysql.connect(host=“192.168.207.160”, port=3306, user=“sonar”, passwd=“sonar”, db=“sonar”)
  9. cursor = db.cursor()
  10. select_p_uuid=“SELECT project_uuid,kee FROM projects WHERE `name`= ‘%s'” %(project_name)
  11. cursor.execute(select_p_uuid)
  12. result = cursor.fetchone()
  13. p_uuid = result[0]
  14. projectKey = result[1]
  15. db.close()
  16. return(p_uuid, projectKey)
  17.  
  18. def select_total_info(p_uuid):
  19. total_info=[]
  20. # 使用cursor()方法获取操作游标
  21. db = pymysql.connect(host=“192.168.207.160”, port=3306, user=“sonar”, passwd=“sonar”, db=“sonar”)
  22. cursor = db.cursor()
  23.  
  24. select_p_links = “SELECT text_value FROM project_measures WHERE text_value LIKE ‘java=%’ and component_uuid=” + “\'” + p_uuid + “\'”
  25. cursor.execute(select_p_links)
  26. p_links = cursor.fetchone()[0].split(“=”)[1]
  27.  
  28. sql_info = “SELECT count(*) FROM issues WHERE project_uuid=’%s’ and issue_type =%s”
  29. for leak in [2,3,1]:
  30. search_data = sql_info %(p_uuid, leak)
  31. cursor.execute(search_data)
  32. total_info.append(cursor.fetchone()[0])
  33. db.close()
  34. return p_links,total_info
  35.  
  36. def select_bugs(p_uuid):
  37. bugs=[]
  38. db = pymysql.connect(host=“192.168.207.160”, port=3306, user=“sonar”, passwd=“sonar”, db=“sonar”)
  39. cursor = db.cursor()
  40.  
  41. sql_info = “SELECT count(*) FROM issues WHERE project_uuid=’%s’ and issue_type =2 AND severity =’%s'”
  42. for leak in [‘BLOCKER’,‘CRITICAL’,“MAJOR”,‘MINOR’,‘INFO’]:
  43. search_data=sql_info % (p_uuid,leak)
  44. cursor.execute(search_data)
  45. bugs.append(cursor.fetchone()[0])
  46. db.close()
  47. return bugs
  48.  
  49. def select_leaks(p_uuid):
  50. leaks=[]
  51. db = pymysql.connect(host=“192.168.207.160”, port=3306, user=“sonar”, passwd=“sonar”, db=“sonar”)
  52. cursor = db.cursor()
  53.  
  54. sql_info = “SELECT count(*) FROM issues WHERE project_uuid=’%s’ and issue_type =3 AND severity =’%s'”
  55. for leak in [‘BLOCKER’,‘CRITICAL’,“MAJOR”,‘MINOR’,‘INFO’]:
  56. search_data=sql_info % (p_uuid,leak)
  57. cursor.execute(search_data)
  58. leaks.append(cursor.fetchone()[0])
  59. db.close()
  60. return leaks
  61.  
  62. def select_bad_tastes(p_uuid):
  63. tastes=[]
  64. db = pymysql.connect(host=“192.168.207.160”, port=3306, user=“sonar”, passwd=“sonar”, db=“sonar”)
  65. cursor = db.cursor()
  66.  
  67. sql_info=“SELECT count(*) FROM issues WHERE project_uuid=’%s’ and issue_type =1 AND severity =’%s'”
  68. for leak in [‘BLOCKER’,‘CRITICAL’,“MAJOR”,‘MINOR’,‘INFO’]:
  69. search_data=sql_info % (p_uuid,leak)
  70. cursor.execute(search_data)
  71. tastes.append(cursor.fetchone()[0])
  72. return tastes
  73. db.close()
  74.  
  75. curpath = os.getcwd()
  76. table_tem_name=“table.html”
  77. def generate_errmsg_table(s_lines=“”, total_data=[], bugs=[],leaks=[],tastes=[],report_url=“”):
  78. env = Environment(loader=FileSystemLoader(curpath, ‘utf-8’)) # 创建一个包加载器对象
  79. template = env.get_template(table_tem_name)
  80. html_content = (template.render(lins=s_lines,total_data=total_data, bugs=bugs,leaks = leaks,tastes=tastes,report_url=report_url))
  81. fh = open(report_html_path, ‘w’)
  82. fh.write(html_content)
  83. fh.close()
  84.  
  85. project_name = sys.argv[1]
  86. report_html_path=“report\\”+project_name+“.html”
  87. p_uuid, projectKey=select_project_uuid(project_name)
  88. s_lines,total_data=select_total_info(p_uuid)
  89. bugs=select_bugs(p_uuid)
  90. leaks=select_leaks(p_uuid)
  91. tastes=select_bad_tastes(p_uuid)
  92. report_url=“http://192.168.207.140:9000/dashboard?id=%s” %(projectKey)
  93. generate_errmsg_table(s_lines,total_data,bugs,leaks,tastes,report_url)

 table.html内容:

  1. <!DOCTYPE html>
  2. <html lang=“en”>
  3. <head>
  4. <meta charset=“GBK”>
  5. <body>
  6. <p style=fontweight:bold;>一、总体情况:</p>
  7. <ul>
  8. <li style=fontweight:bold;>整体运行情况:扫描代码行数:<span style=color:blue>{{lins}}</span>, bugs:<span style=color:red>{{total_data[0]}}</span>, 漏洞:<span style=color:red>{{total_data[1]}}</span>, 坏味道:<span style=color:red>{{total_data[2]}}</span></li>
  9. <li style=fontweight:bold;>URL地址:<a style=fontweight:bold; href={{report_url}} >{{report_url}}</a></li>
  10. </ul>
  11. <p style=fontweight:bold;>二、错误信息详情:</p>
  12. <table border=“1” cellpadding=“10” width=“540” height=“120”>
  13. <tr ><th></th><th>阻断</th><th>严重</th><th>主要</th><th>次要</th><th>提示</th><th>总数</th></tr>
  14. <tr bgcolor=#ECFFFF><td>bugs</td><td align=“center”>{{bugs[0]}}</td><td align=“center”>{{bugs[1]}}</td><td align=“center”>{{bugs[2]}}</td><td align=“center”>{{bugs[3]}}</td><td align=“center”>{{bugs[4]}}</td><td align=“center” style=color:red>{{total_data[0]}}</td></tr>
  15. <tr bgcolor=#D2E9FF><td>漏洞</td><td align=“center”>{{leaks[0]}}</td><td align=“center”>{{leaks[1]}}</td><td align=“center”>{{leaks[2]}}</td><td align=“center”>{{leaks[3]}}</td><td align=“center”>{{leaks[4]}}</td><td align=“center” style=color:red>{{total_data[1]}}</td></tr>
  16. <tr bgcolor=#ECFFFF><td>坏味道</td><td align=“center”>{{tastes[0]}}</td><td align=“center”>{{tastes[1]}}</td><td align=“center”>{{tastes[2]}}</td><td align=“center”>{{tastes[3]}}</td><td align=“center”>{{tastes[4]}}</td><td align=“center” style=color:red>{{total_data[2]}}</td></tr>
  17. </table>
  18. <br></br>
  19. </body>
  20. </html>

9、添加执行自定义生成邮件模板步骤

前提:由于要执行sonar.py脚本,所以jenkins所在机器要有python3环境,且安装了pymysql、jinja2,

     centos安装python3:

进入到sonar.py所在目录,执行命令:python3 sonar.py 项目名

执行完成后会在/root/.jenkins/sonar_script目录下生成html文件,

10、配置发送邮件

10.1 安装插件:Email Extension

10.2 在系统管理》》系统设置中设置

11、发送邮件

在job中添加步骤

内容选择HTML,打开高级选项

增加触发器,并打开高级选项

输入发送邮箱列表,以英文逗号分隔,和邮件内容,html就是上面步骤生成的

最终的邮件如下图:

python+minicap的使用

说起Minicap,不得不提到STF,STF (Smartphone Test Farm) 是一个开源的web架构应用,用户可通过浏览器远程操作Android设备、调试Android应用、在设备上进行测试,实现真正意义云端使用、调试、测试、管理真机器。STF出现以后,国内几个大互联网公司也纷纷跟进效仿,出现了类似的真机调试、管理平台,较为知名的有腾讯Wetest、阿里MQC、百度MTCTestIn等。可见远程真机调试在移动研发领域的作用还是受到了比较高的重视,也能为公司以及用户带来比较直接的收益。下面是STF官方的介绍动画。

STF

minicap简介

minicap属于STF框架的一个工具,由STF团队自身开发,属于较为核心的一部分,minicap运行于android设备端,负责设备屏幕视频的实时采集并通过socket接口发送,github下载地址:https://github.com/openstf/minicapmicicap。minicap采集屏幕的原理很简单:通过ndk的截屏接口不停的截屏并通过socket接口实时发送,这样客户端便可以得到一序列的图片流,图片流合成后就成为视频;

构建minicap

micicap由Android ndk开发,包含一个可执行的二进制文件以及一个so文件,运行minicap前,需要通过adb命令将设备对应CPU架构以及设备对应SDK版本的minicap文件拷贝到设备后,再执行。由于github上并没有上传编译完成后的产物,因此我们需要自行编译。

编译依赖环境:

1)、NDK;

2)、make;

3)、git;

环境依赖较为简单,如果没有NDK以及make环境的,可自行百度安装;

构建过程:

1)、通过git下载minicamp源码:

git clone https://github.com/openstf/minicap.git

2)、micicap项目还依赖于libjpeg-turbo,首先我们需要在minicap引入libjpeg-turbo项目源码:

git submodule init

git submodule update

3)、执行ndk-build,构建完成后,minicap编译后的产物将会在libs目录下找到;

ndk-build

运行minicap

1)、获取设备CPU支持的ABI,minicap针对4种不同的ABI构建了不同的so文件和可执行文件,分别是:x86_64/x86/arm64-v8a/armeabi-v7a;

ABI=$(adb shell getprop ro.product.cpu.abi|tr -d’\r’)

2)、拷贝对应ABI版本的文件到设备,这里使用的是adb push;

adb push libs/$ABI/minicap /data/local/tmp/

3)、获取设备对应的SDK版本;

SDK=$(adb shell getprop ro.build.version.sdk|tr -d’\r’)

4)、只有可执行文件是不够的,我们还需要拷贝对应sdk版本的共享库到设备;

adb push jni/minicap-shared/aosp/libs/android-$SDK/$ABI/minicap.so /data/local/tmp/

5)、每次启动minicap,我们都需要设置LD_LIBRARY_PATH,不然会提示找不到公共库,-P后面的参数为:{RealWidth}x{RealHeight}@{VirtualWidth}x{VirtualHeight}/{Orientation},可以指定采集的实际大小、虚拟大小以及屏幕方向,实际大小一般设置成设备物理分辨率大小,虚拟大小是通过socket接口发送的大小,屏幕实际窗口大小我们可以通过adb命令获取;

adb shell dumpsys window | grep -Eo ‘init=\d+x\d+’ | head -1 | cut -d= -f 2

6)、启动minicap,下面我们假设获取到的实际屏幕大小是1080×1920,需要发送的虚拟窗口大小是540×960,采集的屏幕方向是纵向;

adb shell LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/minicap -P 1080×1920@1080×1920/0

7)、端口转发,通过adb forward命令,可以把minicap端口映射到我们PC指定的端口,localabstract:minicap是UNIX域名的SOCKET名称,把minicap的socket端口映射到PC的1313端口,这样我们就可以在PC通过连接1313端口获取到设备的实时视频流;

adb forward tcp:1313 localabstract:minicap

minicap协议解析

minicap启动并用adb forward命令映射端口后,我们就可以通过socket与minicap建立连接。

1)、Global header

minicap协议是一种简单的二进制流推送流协议,一旦与minicap建立连接,minicap首先会推送长度为24字节的global header,global header只会推送一次,后续推送的数据不会再包括global header,而是不断的推送实时图片流数据,直到客户端关闭socket连接。

Global header binary format

Global header说包含了基本的一些信息,如minicap的版本信息、头长度、实际大小以及虚拟大小、设备方向等,这些信息我们可以保存起来,方便后面使用,这里我使用python解析了Global header,代码参考如下:

2)、Frame binary format

接下来,minicap会不断的推送一帧一帧的图片流,每一帧都包含两部分信息:0-3字节,表示这一帧图片的长度n,由4个字节的32位整型小端格式存储;4-(n+4)字节,是具体的图片数据,由JPG格式存储,这部分才是我们想要的最关键数据;

Frame binary format

至此,我们完成了minicap协议的解析,并获取到了minicap推送过来的每一帧图片。需要注意的是,由于minicap是实时推送流,因此流的数据可能会比较大,客户端获取的buffer需要尽可能的大,不然我们在渲染每一帧的时候,可能会出现卡顿的现象,具体多大合适,我们可以稍微推算一下,一张由minicap推送过来的1080×1920大小的png图片,大概是100-200KB,minicap宣称帧率可以达到20 FPS左右,因此我们的buffer可以设置为:200KB * 20 = 4096000字节,每隔一秒recv()一次;

PyQt实时渲染

获取到图片流数据后,我们可以使用PyQt中的paintEvent进行渲染,下面的refreshFrame()方法,关联了获取图片线程中的一个信号槽,一旦获取图片线程从minicap解析到一帧的图片,便会通知refreshFrame()中的self.update()方法,self.update()方法则会调用paintEvent进行界面的刷新:

CentOS7 安装远程桌面

Xrdp是Microsoft远程桌面协议(RDP)的一个开源实现,它允许以图形方式控制远程系统。使用RDP,您可以登录到远程计算机并创建一个真正的桌面会话,就像您登录到本地计算机一样。
系统环境
  • 服务端:Centos7.7 Minimal
  • 客户端:Windows10
安装桌面环境

本实验中安装的系统没有安装桌面环境,我们需要自己安装,如果已经安装桌面了清跳过这一步。Centos7提供了”Cinnamon Desktop”,”MATE Desktop”,”GNOME Desktop”,”KDE Plasma Workspaces”,”LXQt Desktop”,”Xfce”让我们安装。

下面的命令列出可用环境组:

[root@localhost ~]# yum grouplist
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * epel: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Available Environment Groups:
   Minimal Install
   Compute Node
   Infrastructure Server
   File and Print Server
   Cinnamon Desktop
   MATE Desktop
   Basic Web Server
   Virtualization Host
   Server with GUI
   GNOME Desktop
   KDE Plasma Workspaces
   Development and Creative Workstation
Available Groups:
   Cinnamon
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Educational Software
   Electronic Lab
   Fedora Packager
   General Purpose Desktop
   Graphical Administration Tools
   Haskell
   LXQt Desktop
   Legacy UNIX Compatibility
   MATE
   Milkymist
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
   TurboGears application framework
   Xfce
Done

我们可以选择自己喜欢的桌面环境,在这里选择安装Xfce桌面:

[root@localhost ~]# yum -y install epel-release && yum groupinstall Xfce
安装Xrdp
[root@localhost ~]# yum -y install xrdp

安装完成之后,设置开机启动并启动xrdp

[root@localhost ~]# systemctl start xrdp && systemctl enable xrdp

创建~/.Xclients,设置默认启动xfce4桌面

[root@localhost ~]# echo "xfce4-session" > ~/.Xclients
[root@localhost ~]# chmod +x .Xclients
在客户端远程连接

Centos7安装Xrdp远程桌面服务Centos7安装Xrdp远程桌面服务

Centos7安装Xrdp远程桌面服务Centos7安装Xrdp远程桌面服务

Centos7安装Xrdp远程桌面服务Centos7安装Xrdp远程桌面服务

CentOS7.5安装Redis问题:fatal error: jemalloc/jemalloc.h: No such file or directory

cd src && make all

make[1]: Entering directory `/root/redis-4.0.14/src’

CC Makefile.dep

make[1]: Leaving directory `/root/redis-4.0.14/src’

make[1]: Entering directory `/root/redis-4.0.14/src’

CC adlist.o

In file included from adlist.c:34:0:

zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory

#include <jemalloc/jemalloc.h>

^

compilation terminated.

make[1]: *** [adlist.o] Error 1

make[1]: Leaving directory `/root/redis-4.0.14/src’

make: *** [all] Error 2

上面抛出 jemalloc 的原因是 jemalloc 重载了 Linux 下的 ANSI C 的 malloc 和 free 函数。解决办法:make时添加参数,如下:

make MALLOC=libc

struct redisServer’ has no member named ‘maxmemory’

  1. #升级到 5.3及以上版本
  2. yum -y install centos-release-scl
  3. yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
  4. scl enable devtoolset-9 bash
  5. #注意:scl命令启用只是临时的,推出xshell或者重启就会恢复到原来的gcc版本。
  6. #如果要长期生效的话,执行如下:
  7. echo “source /opt/rh/devtoolset-9/enable” >>/etc/profile

shell条件判断

1.判断语法

a)  test 条件表达式

b)  [ 条件表达式 ]

c)  [[ 条件表达式 ]]        可匹配正则 =~

d)  判断后执行多个命令

[  条件1 ] && {
          命令1
          命令2
}

例如:

[ 1 -eq 2 ]&&{
    echo aa;
    echo bb;
}||{
    echo cc
    echo dd
}

 

man test去查看,很多的参数都用来进行条件判断

 

2.文件类型判断

-e 是否存在 不管是文件还是目录,只要存在,条件就成立
-f 是否为普通文件
-d 是否为目录
-S socket
-p pipe
-c character
-b block
-L 软link

 

3.文件内容判断

-s 是否为非空文件

! -s 表示空文件

 

4.文件权限判断

-r  当前用户对其是否可读
-w 当前用户对其是否可写
-x 当前用户对其是否可执行
-u 是否有suid
-g 是否sgid
-k 是否有t位

 

5.两个文件的比较判断

file1 -nt file2	 比较file1是否比file2新
file1 -ot file2	 比较file1是否比file2旧
file1 -ef file2	 比较是否为同一个文件,或者用于判断硬连接,是否指向同一个inode

 

6.整数之间的判断

-eq	 相等
-ne	 不等
-gt	 大于
-lt  	 小于
-ge	 大于等于
-le	小于等于

 

7.字符串之间的判断

-z  是否为空字符串 字符串长度为0,就成立
-n  是否为非空字符串 只要字符串非空,就是成立
string1 = string2    是否相等           --等号两边要有空格
string1 != string2   不等
! 结果取反

 

注意

[ “$1” = “abc” ]       —判断字符是否相等,变量也要用双引号

 

8.多重条件判断

逻辑判断符号:

-a 和 && 逻辑与

只有两个条件都成立,整个大条件才成立

[ 1 -eq 1 ] && [ 2 -ne 3 ]

[ 1 -eq 1 -a 2 -ne 3 ]

 

-o 和 || 逻辑或

只要其中一个条件成立,整个大条件就成立

[ 1 -eq 1 -o 2 -ne 2 ]

[ 1 -eq 1 ] || [ 2 -ne 2 ]

 

 

! 逻辑非 优先级最低

-a 优先级 比 -o 优先级要高

[[]] 和(())中可以使用&& || 不能使用 -a -o

9.实例

# [ $(id -u) -eq 0 ] && echo “当前是超级用户”            –id -u  username  可以查看用户id

$ [ $UID -eq 0 ] && echo “当前是超级用户” || echo “you不是超级用户”

# ((1==2));echo $?      –C语言风格的数值比较

# ((1>=2));echo $?

 

 

题目:输入一个路径,判断路径是否存在,而且输出是文件还是目录,如果是链接文件,还得输出是有效的连接还是无效的连接

思路:链接文件也属于文件,所以先判断链接文件,再去判断是否为文件

 #!/bin/bash
read -p "Input your path:" path
if [ -e $path -a -L $path ];then 
    echo "$0 is link file"
elif [ ! -e $path ] && [ -L $path ];then
    echo "$0 is not effctive link file"
elif [ -d $path ];then
    echo "$0 is a dirctory"
elif [ -f $path ];then
    echo " $0 is file"
else
    echo "$0 is other file"
fi

ls -L  dir                   -L可以判断软链接

–无效链接用-e判断会不存在

-L   如果遇到性质为符号链接的文件或目录,直接列出该链接所指向的原始文件或目录;

Linux sed 命令

Linux sed 命令是利用脚本来处理文本文件。

sed 可依照脚本的指令来处理、编辑文本文件。

Sed 主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。

语法

sed [-hnV][-e<script>][-f<script文件>][文本文件]

参数说明

  • -e<script>或–expression=<script> 以选项中指定的script来处理输入的文本文件。
  • -f<script文件>或–file=<script文件> 以选项中指定的script文件来处理输入的文本文件。
  • -h或–help 显示帮助。
  • -n或–quiet或–silent 仅显示script处理后的结果。
  • -V或–version 显示版本信息。

动作说明

  • a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
  • c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
  • d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
  • i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
  • p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
  • s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

实例

在testfile文件的第四行后添加一行,并将结果输出到标准输出,在命令行提示符下输入如下命令:

sed -e 4a\newLine testfile 

首先查看testfile中的内容如下:

$ cat testfile #查看testfile 中的内容  
HELLO LINUX!  
Linux is a free unix-type opterating system.  
This is a linux testfile!  
Linux test 

使用sed命令后,输出结果如下:

$ sed -e 4a\newline testfile #使用sed 在第四行后添加新字符串  
HELLO LINUX! #testfile文件原有的内容  
Linux is a free unix-type opterating system.  
This is a linux testfile!  
Linux test  
newline 

以行为单位的新增/删除

将 /etc/passwd 的内容列出并且列印行号,同时,请将第 2~5 行删除!

[root@www ~]# nl /etc/passwd | sed '2,5d'
1 root:x:0:0:root:/root:/bin/bash
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
.....(后面省略).....

sed 的动作为 ‘2,5d’ ,那个 d 就是删除!因为 2-5 行给他删除了,所以显示的数据就没有 2-5 行罗~ 另外,注意一下,原本应该是要下达 sed -e 才对,没有 -e 也行啦!同时也要注意的是, sed 后面接的动作,请务必以 ” 两个单引号括住喔!

只要删除第 2 行

nl /etc/passwd | sed '2d'

要删除第 3 到最后一行

nl /etc/passwd | sed '3,$d'

在第二行后(亦即是加在第三行)加上『drink tea?』字样!

[root@www ~]# nl /etc/passwd | sed '2a drink tea'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
drink tea
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....(后面省略).....

那如果是要在第二行前

nl /etc/passwd | sed '2i drink tea'

如果是要增加两行以上,在第二行后面加入两行字,例如 Drink tea or …..drink beer?

[root@www ~]# nl /etc/passwd | sed '2a Drink tea or ......\
> drink beer ?'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea or ......
drink beer ?
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....(后面省略).....

每一行之间都必须要以反斜杠『 \ 』来进行新行的添加喔!所以,上面的例子中,我们可以发现在第一行的最后面就有 \ 存在。

以行为单位的替换与显示

将第2-5行的内容取代成为『No 2-5 number』呢?

[root@www ~]# nl /etc/passwd | sed '2,5c No 2-5 number'
1 root:x:0:0:root:/root:/bin/bash
No 2-5 number
6 sync:x:5:0:sync:/sbin:/bin/sync
.....(后面省略).....

透过这个方法我们就能够将数据整行取代了!

仅列出 /etc/passwd 文件内的第 5-7 行

[root@www ~]# nl /etc/passwd | sed -n '5,7p'
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

可以透过这个 sed 的以行为单位的显示功能, 就能够将某一个文件内的某些行号选择出来显示。

数据的搜寻并显示

搜索 /etc/passwd有root关键字的行

nl /etc/passwd | sed '/root/p'
1  root:x:0:0:root:/root:/bin/bash
1  root:x:0:0:root:/root:/bin/bash
2  daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3  bin:x:2:2:bin:/bin:/bin/sh
4  sys:x:3:3:sys:/dev:/bin/sh
5  sync:x:4:65534:sync:/bin:/bin/sync
....下面忽略

如果root找到,除了输出所有行,还会输出匹配行。

使用-n的时候将只打印包含模板的行。

nl /etc/passwd | sed -n '/root/p'
1  root:x:0:0:root:/root:/bin/bash

数据的搜寻并删除

删除/etc/passwd所有包含root的行,其他行输出

nl /etc/passwd | sed  '/root/d'
2  daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3  bin:x:2:2:bin:/bin:/bin/sh
....下面忽略
#第一行的匹配root已经删除了

数据的搜寻并执行命令

搜索/etc/passwd,找到root对应的行,执行后面花括号中的一组命令,每个命令之间用分号分隔,这里把bash替换为blueshell,再输出这行:

nl /etc/passwd | sed -n '/root/{s/bash/blueshell/;p;q}'    
1  root:x:0:0:root:/root:/bin/blueshell

最后的q是退出。

数据的搜寻并替换

除了整行的处理模式之外, sed 还可以用行为单位进行部分数据的搜寻并取代。基本上 sed 的搜寻与替代的与 vi 相当的类似!他有点像这样:

sed 's/要被取代的字串/新的字串/g'

先观察原始信息,利用 /sbin/ifconfig 查询 IP

[root@www ~]# /sbin/ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
.....(以下省略).....

本机的ip是192.168.1.100。

将 IP 前面的部分予以删除

[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'
192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

接下来则是删除后续的部分,亦即: 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

将 IP 后面的部分予以删除

[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
192.168.1.100

多点编辑

一条sed命令,删除/etc/passwd第三行到末尾的数据,并把bash替换为blueshell

nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/'
1  root:x:0:0:root:/root:/bin/blueshell
2  daemon:x:1:1:daemon:/usr/sbin:/bin/sh

-e表示多点编辑,第一个编辑命令删除/etc/passwd第三行到末尾的数据,第二条命令搜索bash替换为blueshell。

直接修改文件内容(危险动作)

sed 可以直接修改文件的内容,不必使用管道命令或数据流重导向! 不过,由於这个动作会直接修改到原始的文件,所以请你千万不要随便拿系统配置来测试! 我们还是使用文件 regular_express.txt 文件来测试看看吧!

regular_express.txt 文件内容如下:

[root@www ~]# cat regular_express.txt 
runoob.
google.
taobao.
facebook.
zhihu-
weibo-

利用 sed 将 regular_express.txt 内每一行结尾若为 . 则换成 !

[root@www ~]# sed -i 's/\.$/\!/g' regular_express.txt
[root@www ~]# cat regular_express.txt 
runoob!
google!
taobao!
facebook!
zhihu-
weibo-

:q:q

利用 sed 直接在 regular_express.txt 最后一行加入 # This is a test:

[root@www ~]# sed -i '$a # This is a test' regular_express.txt
[root@www ~]# cat regular_express.txt 
runoob!
google!
taobao!
facebook!
zhihu-
weibo-
# This is a test

由於 $ 代表的是最后一行,而 a 的动作是新增,因此该文件最后新增 # This is a test

sed 的 -i 选项可以直接修改文件内容,这功能非常有帮助!举例来说,如果你有一个 100 万行的文件,你要在第 100 行加某些文字,此时使用 vim 可能会疯掉!因为文件太大了!那怎办?就利用 sed 啊!透过 sed 直接修改/取代的功能,你甚至不需要使用 vim 去修订!

在Linux下重启Oracle数据库及监听器

方法1:

用root以ssh登录到linux,打开终端输入以下命令:

cd $ORACLE_HOME #进入到oracle的安装目录

dbstart #重启服务器

lsnrctl start #重启监听器

cd $ORACLE_HOME #进入到oracle的安装目录

dbstart #重启服务器

lsnrctl start #重启监听器

———————————–

方法2:

Sql代码

cd $ORACLE_HOME/bin #进入到oracle的安装目录

./dbstart #重启服务器

./lsnrctl start #重启监听器

———————————–

方法3:

(1) 以oracle身份登录数据库,命令:su -oracle

(2) 进入Sqlplus控制台,命令:sqlplus /nolog

(3) 以系统管理员登录,命令:connect / as sysdba

(4) 启动数据库,命令:startup

(5) 如果是关闭数据库,命令:shutdown immediate

(6) 退出sqlplus控制台,命令:exit

(7) 进入监听器控制台,命令:lsnrctl

(8) 启动监听器,命令:start

(9) 退出监听器控制台,命令:exit

二、重启实例:

(1) 切换需要启动的数据库实例:export ORACLE_SID=C1

(2) 进入Sqlplus控制台,命令:sqlplus /nolog

(3) 以系统管理员登录,命令:connect / as sysdba

(4) 如果是关闭数据库,命令:shutdown abort

(5) 启动数据库,命令:startup

(6) 退出sqlplus控制台,命令:exit

 

 

监听文件修改

 

# listener.ora Network Configuration File: /home/oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
# Generated by Oracle configuration tools.

SID_LIST_LISTENER=
  (SID_LIST=
    (SID_DESC=
      (GLOBAL_DBNAME= orcl)
      (ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_1)
      (SID_NAME=orcl)
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.42)(PORT = 1521))
    )
  )

ADR_BASE_LISTENER = /home/oracle/app

nginx配置快捷启动service

1,/etc/init.d/目录下缺少nginx默认启动脚本
vim /etc/init.d/nginx
新建脚本文件,然后把下面的代码复制进去

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  http://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid

case "$1" in
start)
    echo -n "Starting $NAME... "

    if netstat -tnpl | grep -q nginx;then
        echo "$NAME (pid `pidof $NAME`) already running."
        exit 1
    fi

    $NGINX_BIN -c $CONFIGFILE

    if [ "$?" != 0 ] ; then
        echo " failed"
        exit 1
    else
        echo " done"
    fi
    ;;

 stop)
    echo -n "Stoping $NAME... "

    if ! netstat -tnpl | grep -q nginx; then
        echo "$NAME is not running."
        exit 1
    fi

    $NGINX_BIN -s stop

    if [ "$?" != 0 ] ; then
        echo " failed. Use force-quit"
        exit 1
    else
        echo " done"
    fi
    ;;

 status)
    if netstat -tnpl | grep -q nginx; then
        PID=`pidof nginx`
        echo "$NAME (pid $PID) is running..."
    else
        echo "$NAME is stopped"
        exit 0
    fi
    ;;

 force-quit)
    echo -n "Terminating $NAME... "

    if ! netstat -tnpl | grep -q nginx; then
        echo "$NAME is not running."
        exit 1
    fi

    kill `pidof $NAME`

    if [ "$?" != 0 ] ; then
        echo " failed"
        exit 1
    else
        echo " done"
    fi
    ;;

 restart)
    $0 stop
    sleep 1
    $0 start
    ;;

 reload)
    echo -n "Reload service $NAME... "

    if netstat -tnpl | grep -q nginx; then
        $NGINX_BIN -s reload
        echo " done"
    else
        echo "$NAME is not running, can't reload."
        exit 1
    fi
    ;;

 configtest)
    echo -n "Test $NAME configure files... "

    $NGINX_BIN -t
    ;;

 *)
    echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
    exit 1
    ;;

esac

2,切换目录,修改权限

cd /etc/init.d

chmod +x nginx

/sbin/chkconfig --level 345 nginx on

service nginx start

 

Mac下oh-my-zsh的安装配置和卸载

装完iterm2我的第一感觉是除了黑了点,跟Mac自带终端一样丑。。。于是想装一下oh-my-zsh

1. 安装 oh-my-zsh

1.1 安装命令

curl -L http://install.ohmyz.sh | sh
主题在线网址

1.2 配置 ~/.zshrc
  • vim ~/.zshrc
  • 设置主题为agnoster: 编辑而非命令 ZSH_THEME = "agnoster"
  • 重启配置否则不生效 source ~/.zshrc
1.3 下载字体包

Solarized主题需要使用 Meslo 字体方能显示某些特定符号,字体地址如下:https://github.com/powerline/fonts/raw/master/Meslo%20Slashed/Meslo%20LG%20M%20Regular%20for%20Powerline.ttf
打开 iTerm -> Preferences -> Profiles -> Text -> Change Font,选择Meslo

1.4 安装高亮语法插件
1. git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
2. vim ~/.zshrc
3. # zsh-syntax-highlighting 官方推荐放在最后面 各插件之间用空格隔开
   添加 plugins=(zsh-syntax-highlighting) 如下图
4. source ~/.zshrc
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

1.5 不推荐安装自动提示插件

可能是我没用到家,自动提示补全很难用,因为很多时候提示或者补全的往往不是我想要的,然后还要自己删一遍,而且粘贴以后会发生一些不可描述的事情…

2. 隐藏冗余的用户名和主机名

2.1 编辑 .zshrc文件

vim ~/.zshrc

2.2 添加命令

DEFAULT_USER="songtang" (添加你的默认主机名字)

2.3 查看当前用户

whoami

3. 卸载 oh-my-zsh

进入到.oh-my-zsh/tools目录

  • #cd .oh-my-zsh/tools
  • chmod +x uninstall.sh
  • ./uninstall.sh
  • rm -rif .zshrc