SQL cmdshell to execute putty command - sql-server

I am trying to run a sh file in putty ssh from windows which was done successfully using putty and cmd prompt. When trying the same in xp_cmdshell or through sp_start_job in mssql the query is running for longtime without end.
SQL Query:
exec xp_cmdshell 'D:\path\run.bat'
batch file:
"C:\Program Files\Putty\putty.exe" -ssh root#10.10.10.1 -pw mypass -m D:\path\runcmd.txt
Is the way I am trying correct?

Could you please try this?
EXEC master..xp_CMDShell '"D:\path\run.bat"'
details referred from below link,
Executing a bat file inside a Stored Procedure using SQL server 2005

Related

PsExec command working on cmd doesn't work inside .bat file nor via SQL xp_cmdshell

When I execute the following command on machine1's cmd it works fine:
C:\PSTools\psexec64.exe \\machine2 -u domain\user -p pass -i -d -accepteula C:\folder\file.bat
However, if I try to run it via xp_cmdshell (sql server is also on machine1) as bellow it will fail:
exec xp_cmdshell 'C:\PSTools\psexec64.exe \\machine2 -u domain\user -p pass -i -d -accepteula C:\folder\file.bat'
Here is the output:
NULL
PsExec v2.4 - Execute processes remotely
Copyright (C) 2001-2022 Mark Russinovich
Sysinternals - www.sysinternals.com
NULL
Connecting to xx.xxx.xxx.xx... Starting PSEXESVC service on xx.xxx.xxx.xx... Could not start PSEXESVC service on xx.xxx.xxx.xx:
Access is denied.
Connecting to xx.xxx.xxx.xx... Starting PSEXESVC service on xx.xxx.xxx.xx...
I also tried to save the command as a bat file to see if the problem is exclusive to the SQL server, but it won't work either.
Here is the output:
PsExec could not start C:\folder\file.bat on xx.xxx.xxx.xx:
The user name or password is incorrect.
The exact same command works fine on cmd, same machine.

Batch file that runes SQL commands

I want to create a batch file that runs some SQL commands for example:
I'm signing in using these commands:
Sqlcmd -S <Server Location> -U <user> -P <password>
Next is going to be a backup for some database
BACKUP DATABASE <data base> TO DISK=<Location>
GO
EXIT
But after starting the .bat file, log in is successfully but the backup database is not working, I still need to type the commands.
There is a way to actually run those commands without typing in the commands?
Or should I try using something else, like Windows PowerShell?

Why database creation command fails in SQLCMD in SQL Server docker for linux?

I use docker-compose.yml to load my SQL Server image inside a container.
After it's up and running, I create a command.sh shell and try to run it to create a database.
# command.sh
echo 'creating database from ->' $ModuleName
export query="'create database $ModuleName'"
echo $query
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P my_strong_password -Q $query
And it gives me this error:
Sqlcmd: 'database': Unknown Option. Enter '-?' for help.
Please note that I can't use -i switch to use an input .sql file, because I'm creating my queries programmatically in shell based on environment variables.
The output of sqlcmd -? shows how to use then -Q option. On windows this says [-Q "cmdline query" and exit].
But Windows and Linux differ (or are not consistent) in the use of single- or double quotes.
First option is to try: sqlcmd -Q "\"create database $ModuleName\""
Second option is:
Create a temporary file (i.e. /tmp/tmp.sql), and put the SQL statement in that script.
Use -i /tmp/tmp.sql to execute that script.

SQL Server Express backup script

I was able to perform backup for my SQL Server Express using this command at the command prompt:
SQLCMD -E -S testing\SQLEXPRESS –Q "BACKUP DATABASE testing3 TO DISK='C:\Users\Administrator\Desktop\test.bak'"
When I want to create a batch script using the command above in order to create a task scheduler to perform auto backup, it shows error as below:
Here is the batch code:
echo off
c: \
CD "C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn"
SQLCMD -E -S testing\SQLEXPRESS –Q "BACKUP DATABASE testing3 TO DISK='C:\Users\Administrator\Desktop\test.bak';"
I didn't put exit at bottom of it because I want to verify whether the command successfully run or not.
ERROR:
Sqlcmd: 'ûQ "BACKUP DATABASE testing3 TO DISK='C:\Users\Administrator\Desktop\test.bak' ': Unexpected argument. Enter '-?' for help.
Does anyone know how to fix this?
EDIT : I solve it by giving a semi column at the end and type it one by one instead of copy paste it

export MySql database with cmd

I'm using MySql 5.1 version and I'm trying to export mydatabase by cmd ,when I wrote this command :
C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqdump -uroot -ppassword mydatabase >mydatabase.sql
I get an Error
Access is denied
I don't Know what the problem
Help Pleas.....
What's your permissions on the directory you're running this command from? It may be trying to write the mydatabase.sql file to a directory you don't have permissions to.
First check if your command line recognizes mysql command. If not go to command and type in
set path=c:\wamp\bin\mysql\mysql5.1.36\bin
Then use this command to export your database
Go to directory where you have mysql. -u - for username -p - to prompt the password
C:\xampp\mysql\bin>mysqldump -u Username -pPassword DatabaseName > DatabaseName.sql
Note:It is better to use full path of the the sql file file.sql
To export database from dump file (in this case called filename.sql) use: mysql -u username -p password database_name > filename.sql
If you are on Windows you will need to open CMD and go to directory where mysql.exe is installed. If you are using WAMP server then this is usually located in: C:\wamp\bin\mysql\mysql5.1.36\bin (*note the version of mysql might be different)
So you will: cd C:\wamp\bin\mysql\mysql5.1.36\bin
and then execute one of the above commands.

Resources