bat/cmd batch connection SQL Server database query script _dos/bat

Source: Internet
Author: User
Tags exit in goto in python

It is rare to rush home 9 o'clock this evening, and the recent draft box also pressed a lot of "wet goods", pick a post out of the good! However, before sending the article before the first spit on that from yesterday to attack the Zhanggo blog to now continue the boring egg pain!

Even if the cloud to speed up the full cache is not able to carry, because the static mixed with dynamic request, such as browsing count. Fortunately brainwave, to these dynamic do a little bit of hands and feet, now basically no pressure! A rough analysis of this attack blog villain, no outside is 2 kinds of people:

①, the recent Zhang Gobo passenger traffic rose, estimated to be jealousy or people to fend off;
②, to Zhang Ge blog Change friend chain is rejected or neglected people, grudge.

For ①: As the saying goes, no envy is mediocre, so no envy of the site is not a good site. In fact, you only smell my perfume, but did not see my sweat, you only see my ranking, but did not see my intentions.

Build the station more than a year, more or less to SEO have some own opinion. Combined with the development of Zhanggo blog, I think the real user experience, put yourself in the thinking of the user is the best "farmyard", rather than those who can see the effect of "fertilizer" in the short term! When writing an article I will always think about whether the user can understand, how to write to make users easier? In short, if the article is just affixed to your test process, then this blog is really just the impression notes!

So, if you are jealous and attack me, then go on, do not Zhanggo blog to kill you is a coward.

For ②: How to say, the previous article has said the Zhanggo blog friend chain requirements, but still have a lot of friends enthusiastic request exchange friend chain. But your website is really not enough to see, can not reach my request, how do you want me to do? If all agree, then my home page will become a navigation? If it is because of the rejection of the chain of friends and attack me, then please continue, but do not let me know is which site, otherwise you know ...

Well, that's enough nonsense, and here's the thing.

As I said earlier, most of my new company servers are Windows Server environments, all of which were written in Python with a monitoring script, and recently came across a need to add a Zabbix monitoring project, and I was thinking about using the bat batch I was doing my best to finish the project.

No surprises, but with the help of the SQL Server command-line tool, the code is as follows:

 @echo off title Zabbix database query Bat script:: Name: Zabbix Monitoring Script Batch version:: Description: Query the database by using the osql command-line tool.  To do some custom monitoring:: Support: Need Osql.exe and MSVCR71.DLL support, can be placed to the script sibling:: Time: 2015-03-20:: Enter script current directory CD "%~dp0":: Get parameter Set "usg=%1":: Judge parameter if "%usg%" = "" Goto Example:: Initialize the connection parameter set host= "10.172.10.80" set db= "dbname" set user= "Dbuser" set pd= "password":: According to different Parameter executes different query codes (self tuning) if "%usg2%" = = "Monitor1" (:: Monitor 1 set sql= "SQL statement 1") Else if "%usg%" = "Monitor2" (:: Monitor 2 set sql= "SQL" Sentence 2 ") Else if"%usg% "= =" Monitor3 "(:: Monitoring 3 set sql=" SQL Statement 3 ") Else if"%usg% "=" Monitor4 "(:: Monitoring 4 set sql=" SQL Statement 4 "):: Lian Connect to the database and execute the query (for this is the query to the results of the extraction, so you need to do according to the actual situation.) ) for/f "skip=2 delims="%%i in (' Osql.exe-s%host%-u%user%-P%pd%-D%db%-Q%sql% ') do (set result=%%i goto:r  Esult):: Direct execution without parameters will prompt usage: Example echo example:echo= echo%~nx0 Conditions echo= echo------------------------------Echo Script'll auto Exit in 5s ... ping-n 5 127.1>nul exit:: Auto exit after output: result echo%result% 

How to use:

①, verify execution: Execute "script. bat + monitor name" in cmd, for example: D:\>monitor1.bat Monitor1
②, association Zabbix:zabbix How to set I do not repeat, note that the Zabbix profile zabbix_command.conf the following add:

userparameter=newmonitor[*],cmd/k C:\zabbix\NewMonitor.bat Monitor1

Ps: It is important to note that you need to add a cmd/k front, otherwise the Zabbix can not be executed, also can not get data.

Finally, attach a few osql.exe command of the practical parameters and the relevant download address:

-u login_id 
 user login ID. The login ID is case-sensitive. - 
 
p password 
 is a user-specified password. If the-P option is not used, osql prompts for a password. If you use the-P option at the end of the command prompt with no password, osql uses the default password (NULL). Passwords are case-sensitive.

-S Server_name[\instance_name] 
 specifies the Microsoft®sql server™2000 instance to connect to. Specify server_name on this server to connect to the default instance of SQL Server. Specify server_name\instance_name on this server to connect to an instance of a named SQL Server 2000. If you do not specify a server, osql connects to the default instance of SQL Server on the local computer. This option is required when performing osql from a remote computer on the network. 

-D db_name 
 emits a use DB_NAME statement when starting osql.

-Q "Query" 
 executes the query and exits osql immediately. Enclose the query in double quotes to enclose any content embedded in the query in single quotes.

-Q "Query" 
 executes the query when it starts osql, but does not exit osql when the query completes. (Note that the query statement should not contain go). If you are issuing a query from a batch file, use either%variables or the environment%variables%.
For example: 
 SET table = sysobjects 
 osql/q "Select * from%table%"

SQL Server command line connection query tool osql download Address: http://www.jb51.net/database/308702.html

Reprint Please specify: bat/cmd batch process connection SQL Server database query script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.