Powershell script to retreive Jumpbox VM from a list of VMs, which VMs are all have jumpbox connection. How to differentiate Jumpbox VM from other VMs. The output will be like this VM is Jumpbox VM.
I have tried to retrive ip addresses, security rules, tags associated with it, But all are same with other VMs, So I am not getting efficient output.I need to find unique configuration jumpbox vm has from other vms.
The output will be like this "This VM is Jumpbox VM".
I deployed one Jumpbox VM with its NIC connected to the Private IP securely and ran this PowerShell command to get the Jumpbox VM's.
Thanks #Fabricio godboy for the script.
Reference- How to write powershell script to identify whether the Azure Virtual machine has Jumpbox or bastionhost or other private vms - Microsoft Q&A
I created one Jumpbox VM and ran the Powershell script like below:-
Script:-
#
Connect-AzAccount
# Specify the resource group name and VM name
$resourceGroupName = "jumpboxvm"
$vmName = "jumpboxvm12"
# Get the VM object
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
# Get the VM's network interface object
$nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces[0].Id
# Get the VM's private IP address
$privateIpAddress = $nic.IpConfigurations.PrivateIpAddress
# Check if the VM is connected to a jumpbox
$jumpbox = Get-AzVM -ResourceGroupName $resourceGroupName -Name "jumpboxvm12"
if ($jumpbox) {
$jumpboxNic = Get-AzNetworkInterface -ResourceId $jumpbox.NetworkProfile.NetworkInterfaces[0].Id
$jumpboxPrivateIpAddress = $jumpboxNic.IpConfigurations.PrivateIpAddress
if ($privateIpAddress -eq $jumpboxPrivateIpAddress) {
Write-Host "This VM is connected to a jumpbox"
exit
}
}
And got the output like below:-
The following connection attempt works!
sqlcmd -S SOMEOTHERPC\SQLEXPRESS,1433 -Q "SELECT * WHERE Lol=1" -d "mydb"
the following does not work, it times out after 10 seconds and fails
sqlcmd -S SOMEOTHERPC\SQLEXPRESS -Q "SELECT * WHERE Lol=1" -d "mydb"
given that 1433 is the default port for sql server, why is it that I have to provide it? used to work without it?
many applications are getting this connection time out issue And i feel it is due to this?
had the default port been changes somewhere in sqlserver config?
EDIT:
Microsoft SQL Server 2014
Windows Embedded Standard 32bit (6.1 Build 7601)
I have one server with the sql database and manager studio and another server from where I want to execute the bcp command from. Could I use (possibly a .bat file) to generate a .txt file on the same server? Or does anyone have any other suggestions? It is for a person who does not have any knowledge of BD and wants to execute at any time, so I'm not using a job scheduled.
I tried the following, it does not work on the second server but it shows the expected result on the first.
DECLARE #SQL VARCHAR(255), #bcpc VARCHAR(8000)
SET #SQL = 'select * from BaseDatos.dbo.tabla'
SET #bcpc = 'bcp "' + #SQL + '" queryout'
SET #bcpc = #bcpc + ' c:\test\archivo.txt -c -t"|" -Usa -P'
EXEC master..xp_cmdshell #bcpc
print #bcpc
The bcp command you are building does not include the -S option to specify which SQL Server to get data from. I believe, if it's working on your "Server A" this is because BCP must default to the local default instance. However, when you try to run the BCP command on "Server B" you no longer have the same default SQL Server instance (if you have one at all). Try adding the -S option to specify the name of the "Server A" into the BCP command.
Imma double check on the "default" server value if -S is not used.
Yep...
-S server_name [\instance_name] Specifies the instance of SQL Server to which to connect. If no server is specified, the bcp utility
connects to the default instance of SQL Server on the local computer.
This option is required when a bcp command is run from a remote
computer on the network or a local named instance. To connect to the
default instance of SQL Server on a server, specify only server_name.
To connect to a named instance of SQL Server, specify
server_name\instance_name.
How about
EXECUTE (master.xp_cmdshell#bcpc) AT server2
If you are getting no result:
1) Is the Basedatos.dbo.tabla on server 2? If not, create a linked server so that you can reference server1.basedatos.dbo.tabla in order for BCP running on server 2 to get the data from Server 1
2) Check all permissions on server 2.
I have a connection between my localhost and a remote server using putty SSH tunnel.
Thats fine.
Now I need a command to get the sql file on my local machine i.e. c:\folder\test.sql and import it into mysql on the remote server
I thought maybe...
mysql -u prefix_username -p testpass -h localhost -P 3307 prefix_testdb
then do a command like
mysql -p testpass -u prefix_username prefix_testdb < c:\folder\test.sql
this command did not work.
How can I acheive this?
You should run this command
mysql -h host -u user_name -pPassword database < file.sql > output.log
file.sql contains the sql queries to run and output.log makes sense only when you have a query that returns something (like a select)
The only thing different I can see in your code is the blank space between the -p option and the password. If you use the -p option, you must write the password without leaving any blank space. Or you just can user the option --password=Password
I hope you can solve the problem
You will need to ssh to the remote machine with the mysql command appended:
ssh remote_user#remote_server mysql -p testpass -u username testdb < c:\folder\test.sql
1. mysql -h xxx -uxxx -pxxx . //login to the remote mysql
2. use DATABASE. //assign which db to import
3. source path/to/file.sql //the path can be your local sql file path.
Reference: Import SQL file into mysql
Use 'scp' to copy and mysql to insert to you local machine.
Syntax:
scp remote_user#remove_server:/path/to/sql/file.sql ~/path/to/local/directory
after you transfered the file use:
mysql -uYouUserName -p name_of_database_to_import_to < ~/path/to/local/directory/file.sql
mysql {mydbname} --host {server}.mysql.database.azure.com --user {login} --password={password} < ./{localdbbackupfile}.sql
As managed services, DevOps, and CI/CD workflows have become more popular by this point, most providers of those managed services want to remove the human error part of getting the connection strings correct. If you happen to be using Azure, AWS, GCP, etc, There usually is a page or terminal command that shows you these strings to help you easily integrate. Don't forget to check their docs if you're using something like that. They are auto generated, so they are most likely 'best practice' with spot-on correct syntax for the db version you may be using.
The above command is from "connection strings" on the product details page of my Azure Managed Mysql DB Server instance.
Not necessarily asked, but an fyi, a lot of those services auto generate templates for use in a lot of common connection scenarios:
{
"connectionStrings": {
"ado.net": "Server={server}.mysql.database.azure.com; Port=3306; Database=mytestdb; Uid={login}; Pwd={password};",
"jdbc": "jdbc:mysql://{server}.mysql.database.azure.com:3306/mytestdb?user={login}&password={password}",
"jdbc Spring": "spring.datasource.url=jdbc:mysql://{server}.mysql.database.azure.com:3306/mytestdb spring.datasource.username={login} spring.datasource.password={password}",
"mysql_cmd": "mysql mytestdb --host {server}.mysql.database.azure.com --user {login} --password={password}",
"node.js": "var conn = mysql.createConnection({host: '{server}.mysql.database.azure.com', user: '{login}', password: {password}, database: mytestdb, port: 3306});",
"php": "$con=mysqli_init(); [mysqli_ssl_set($con, NULL, NULL, {ca-cert filename}, NULL, NULL);] mysqli_real_connect($con, '{server}.mysql.database.azure.com', '{login}', '{password}', 'mytestdb', 3306);",
"python": "cnx = mysql.connector.connect(user='{login}', password='{password}', host='{server}.mysql.database.azure.com', port=3306, database='mytestdb')",
"ruby": "client = Mysql2::Client.new(username: '{login}', password: '{password}', database: 'mytestdb', host: '{server}.mysql.database.azure.com', port: 3306)"
}
}
You can use pscp to upload file to the server. Go to your command line and type this
pscp.exe c:\folder\test.sql usernameoftheserver#websitename.com:/serverpath
I'm trying to connect Redhat Linux to a Microsoft SQL Server 2008. I already had trouble setting it up on windows (my test machine) but now I need to deploy it on the Linux machine where it will be in production.
So I've installed unixODBC and FreeTDS (with a lot of effort, not even sure if it was installed correctly :S), and the outcome of that is that I have 3 files in /usr/local/etc:
odbc.ini
odbcinst.ini
freetds.conf
I then edited the freetds.conf file and this is what I added:
[sqlServer]
host = servername
port = 4113
instance = sqlServer
tds version = 8.0
client charset = UTF-8
I had to find out the port number from my DBA, as it is set to dynamic in SQL Server 2008.
My odbcinst.ini file looks like this:
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsS.so
CPTimeout =
CPReuse =
FileUsage = 1
and my odbc.ini files looks like this:
[sqlServer]
Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = 1
Servername = sqlServer
Database = RubyApp
So now I tried connecting to see if there is any connection by using
tsql -S sqlServer -U test -P test,
however that only gives me the following error:
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
Error 20013 (severity 2):
Unknown host machine name.
There was a problem connecting to the server
When I tried using isql, doing isql -v sqlServer test test, that spat out the following error:
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Unknown host machine name.
[ISQL]ERROR: Could not SQLConnect
Any ideas what I could be doing wrong?
If you cannot connect with tsql, then there is a connection issue with your SQL server or in freetds.conf.
First verify the port and named instance using tsql -LH [SQL server IP]
# tsql -LH 127.0.0.1
ServerName HOME
InstanceName INSTANCE1
IsClustered No
Version 10.50.2500.0
tcp 1434
If this doesn't work, there is a connection issue between your server and the SQL server.
If it does work, then set your port to match tcp above in freetds.conf.
[TDS]
host = 127.0.0.1
port = 1434
tds version = 7.0
Instance is set in your odbc.ini
[MSSQLExample]
Description = Example server
Driver = FreeTDS
Trace = No
Server = 127.0.0.1\INSTANCE1
Database = MyDatabase
port = 1434
And if all else fails, try using osql to get some feedback on which part is not working.
# osql -S MSSQLExample -U USERNAME -P PASSWORD
And for some good info on tsql error messages: http://freetds.schemamania.org/userguide/confirminstall.htm
Won't you need the SQL Server hostname or IP address in the freetds.conf file --
[sqlServer]
host = 192.168.0.99
: