I'm trying to open a connection to a MySql database which is on AWS behind SSH but having major issues.
static void Main(string[] args)
{
try
{
var sshServer = "11.111.11.11";
var sshUserName = "shane";
var sshPassword = "";
var databaseServer = #"hello202006662000000666.abcdefghijk.ap - southeast - 2.rds.amazonaws.com";
var databaseUserName = "shane";
var databasePassword = "password";
var SSHPrivateKeyFile = #"C:\Users\sshane\myprivatekey.ppk";
var SSHPassPhrase = "sdkypfihsblkdcbsadl;kvgsiudgf";
var databasename = "production";
var (sshClient, localPort) = ConnectSsh(sshServer, sshUserName, sshPassword, SSHPrivateKeyFile, SSHPassPhrase, 22, databaseServer, 3306);
using (sshClient)
{
Console.WriteLine(sshClient);
Console.WriteLine(localPort);
MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder
{
Server = "localhost",
Port = localPort,
Database = "production",
UserID = "shane",
Password = "Jopassword"
};
using (var connection = new MySqlConnection(csb.ConnectionString))
{
Console.WriteLine(connection.Ping());
connection.Open();
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public static (SshClient SshClient, uint Port) ConnectSsh(string sshHostName, string sshUserName, string sshPassword = null,
string sshKeyFile = null, string sshPassPhrase = null, int sshPort = 22, string databaseServer = "localhost", int databasePort = 3306)
{
// check arguments
if (string.IsNullOrEmpty(sshHostName))
throw new ArgumentException($"{nameof(sshHostName)} must be specified.", nameof(sshHostName));
if (string.IsNullOrEmpty(sshHostName))
throw new ArgumentException($"{nameof(sshUserName)} must be specified.", nameof(sshUserName));
if (string.IsNullOrEmpty(sshPassword) && string.IsNullOrEmpty(sshKeyFile))
throw new ArgumentException($"One of {nameof(sshPassword)} and {nameof(sshKeyFile)} must be specified.");
if (string.IsNullOrEmpty(databaseServer))
throw new ArgumentException($"{nameof(databaseServer)} must be specified.", nameof(databaseServer));
// define the authentication methods to use (in order)
var authenticationMethods = new List<AuthenticationMethod>();
if (!string.IsNullOrEmpty(sshKeyFile))
{
authenticationMethods.Add(new PrivateKeyAuthenticationMethod(sshUserName,
new PrivateKeyFile(sshKeyFile, string.IsNullOrEmpty(sshPassPhrase) ? null : sshPassPhrase)));
}
// connect to the SSH server
var sshClient = new SshClient(new ConnectionInfo(sshHostName, sshPort, sshUserName, authenticationMethods.ToArray()));
sshClient.Connect();
// forward a local port to the database server and port, using the SSH server
var forwardedPort = new ForwardedPortLocal("127.0.0.1", databaseServer, (uint)databasePort);
sshClient.AddForwardedPort(forwardedPort);
forwardedPort.Start();
return (sshClient, forwardedPort.BoundPort);
}
The error I'm getting is 'reading from the stream has failed'.
The SSH seems to be OK, but I can't establish a connection to the MySql db. Have been trying for a couple of days now, and I'm at a loss. Any help would be appreciated.
Related
I am trying to use FluentDocker to run the tests against MongoDB, but I cannot connect to it, see the code below.
[Fact]
public async Task TestMongoDbConnection3()
{
const string root = "root";
const string secret = "secret";
using (
var container =
new Builder().UseContainer()
.UseImage("mongo")
.WithEnvironment($"MONGO_INITDB_ROOT_USERNAME:{root}")
.WithEnvironment($"MONGO_INITDB_ROOT_PASSWORD:{secret}")
.ExposePort(27018)
.WaitForPort("27018/tcp", 30000 /*30s*/)
.Build()
.Start())
{
var config = container.GetConfiguration(true);
Assert.Equal(ServiceRunningState.Running, config.State.ToServiceState());
const string connectionString = "mongodb://root:secret#localhost:27018";
// insert data
const string collectionName = "Users";
var mongoClient = new MongoClient(connectionString);
var database = mongoClient.GetDatabase ("Users");
var collection = database.GetCollection<User>(collectionName);
const int id = 1;
var data = new Fixture().Build<User>()
.With(x => x.Id, id)
.Create();
// delete if exists, but got timeout after 30000 ms
await collection.DeleteOneAsync(x => x.Id == data.Id);
...
}
}
I tried to replace the localhost for the IP from config (config.NetworkSettings.IPAddress), but still getting the timeout.
Any idea?
Many thanks,
here is the fix var ep = container.ToHostExposedEndpoint("27017/tcp"); which works when running locally on my macos.
here is the full code. It still falling when it is running on a docker-in-docker image (timeout).
[Fact]
public void Test_Run_PingCommand_returns_True()
{
const string root = "root";
const string secret = "secret";
using (
var container =
new Builder().UseContainer()
.UseImage("mongo")
.WithEnvironment(new []{$"MONGO_INITDB_ROOT_USERNAME={root}",
$"MONGO_INITDB_ROOT_PASSWORD={secret}"})
.ExposePort(27017)
.WaitForPort("27017/tcp", 30000 /*30s*/)
.Build()
.Start())
{
var config = container.GetConfiguration(true);
Assert.Equal(ServiceRunningState.Running, config.State.ToServiceState());
var ep = container.ToHostExposedEndpoint("27017/tcp");
var connectionString =
$"mongodb://{root}:{secret}#{ep}";
var setting = new UsageDbSettings();
var mongoClient = new MongoClient(connectionString);
var database = mongoClient.GetDatabase(setting.DatabaseName);
var isMongoLive = database.RunCommandAsync((Command<BsonDocument>)"{ping:1}").Wait(30000);
Assert.True(isMongoLive);
}
}
I have developed a website asp.net-mvc-4in Visual Studio-2010 which gets data from another sql data server that is placed on another location. I used port 443 to make connection and user login to get data. Website works fine in debug mode and it extracts data from the data server but when I upload the website over windows IIS it does not make any connection with the data server and hence doest not get data. It fails at the point
if (c.TryLogOn(AccountData.LogInUserName, AccountData.PW))
complete method is
try {
using (Client c = AccountData.c)
//using (TcpClient c = new TcpClient())
{
if (c.TryLogOn(AccountData.LogInUserName, AccountData.PW))
{
imc.Search.Base.Query.Query q = new imc.Search.Base.Query.Query();
AddAttributeListForFahrzeug(q);
AttributeValuesCollection values = c.FindInstances(q);
if (tempVehicleType == "all")
{
foreach (object[] av in values)
{
Fahrzeug d = NewFahrzeug(av);
d.VehicleType = tempVehicleType;
returnValue.Add(d);
}
}
else
{
foreach (object[] av in values)
{
Fahrzeug d = NewFahrzeug(av);
if (d.FzgBeschreibung == tempVehicleType)
{
d.VehicleType = tempVehicleType;
returnValue.Add(d);
}
//d.VehicleType = tempVehicleType;
}
}
c.LogOff();
}
}
} catch ( Exception ){
}
Connection to the client
static public AccountSettings GetAccountData()
{
AccountSettings AccountData = new AccountSettings();
System.Security.SecureString Pw = new System.Security.SecureString();
string PwWord = "**********";
string LogInUserName = "username";
foreach (char ch in PwWord)
{
Pw.AppendChar(ch);
}
//AccountData.c = new Client("abc.xyz.zhg.com", 443, true, ConnectionMode.Internet);
AccountData.c = new Client("abc.xyz.zhg.com", 1199, false, ConnectionMode.Remote);
AccountData.PW = Pw;
AccountData.LogInUserName = LogInUserName;
return (AccountData);
}
Then
AccountSettings AccountData = GetAccountData();
I am trying to connect with c#.
Here is the class that submits hive queries successfully to my remote HDInsight cluster. what do i need to change here to connect to the local emulator
public class HadoopImporter : IImporter
{
public static readonly Logger log = LogManager.GetCurrentClassLogger();
public void Import(string _query)
{
try
{
log.Warn("Inside Hive submission method");
var store = new X509Store();
store.Open(OpenFlags.ReadOnly);
var cert =
store.Certificates.Cast<X509Certificate2>()
.First(item => item.Thumbprint == "MYCERTTUMBPRINT");
if (cert == null)
log.Error("no cert found");
log.Warn(cert.FriendlyName);
log.Warn("got the cert with thumbprint ", cert.Thumbprint.ToString())
;
log.Warn("trying to create credentials from cert");
var creds = new JobSubmissionCertificateCredential(new Guid("MYSUBSCRIPTIONID"),
cert, "MYSTORAGECONTAINER");
log.Warn("trying to connect with cert");
var jobClient = JobSubmissionClientFactory.Connect(creds);
log.Warn("Setting Hive job parameters");
var hiveJob = new HiveJobCreateParameters()
{
Query = _query,
StatusFolder = "/samplequeryoutput"
};
var jobResults = jobClient.CreateHiveJob(hiveJob);
log.Warn("Executing wait for jhive results");
WaitForJobCompletion(jobResults, jobClient);
using (var stream = jobClient.GetJobOutput(jobResults.JobId))
{
var reader = new StreamReader(stream);
var res = reader.ReadToEnd();
log.Warn("trying to get the job results " + res.ToString());
}
}
catch (Exception exp)
{
log.Error(exp);
}
}
private static void WaitForJobCompletion(JobCreationResults jobDetails, IJobSubmissionClient client)
{
var jobInProgress = client.GetJob(jobDetails.JobId);
while (jobInProgress.StatusCode != JobStatusCode.Completed && jobInProgress.StatusCode != JobStatusCode.Failed)
{
log.Warn("Inside the while loop waiting for hive job to complete");
jobInProgress = client.GetJob(jobInProgress.JobId);
Thread.Sleep(TimeSpan.FromSeconds(10));
}
log.Trace("HIVE Job has Imported " + jobDetails.JobId);
}
}
You should be able to connect to a local one-box using the REST implementation of the client.
You're looking for the WebHCatHttpClient interface. The code below runs a basic query against my local one-box.
var httpClient = new WebHCatHttpClient(new Uri("http://localhost:50111/"), "username", "password");
string outputDir = "basichivejob";
var task = httpClient.CreateHiveJob(#"select * from iris;", null, null, outputDir, null);
task.Wait();
var response = task.Result;
var output = response.Content.ReadAsAsync<JObject>();
output.Wait();
response.EnsureSuccessStatusCode();
string id = output.Result.GetValue("id").ToString();
httpClient.WaitForJobToCompleteAsync(id).Wait();
See the SDK docs for more info.
I use DotNetOpenAuth.
So.. I am getting looking good response which has state Authenticated.
That is fine.
Now I want to get user profile info but always getting NULL.
Here is the code.
private ServiceProviderDescription GetServiceDescription()
{
string ValidateTokenEndPoint = ConfigurationManager.AppSettings["identityOAuthValidateTokenEndPointUrl"];
string ValidateAuthorizationHeaderEndPoint = ConfigurationManager.AppSettings["identityOAuthValidateAuthorizationHeaderEndPointUrl"];
string AccessTokenEndPoint = ConfigurationManager.AppSettings["identityOAuthAccessTokenURL"];
bool UseVersion10A = Convert.ToBoolean(ConfigurationManager.AppSettings["identityOAuthUseVersion10a"]);
string RequestTokenStr = ConfigurationManager.AppSettings["identityOAuthRequestTokenURL"];
string UserAuthStr = ConfigurationManager.AppSettings["identityOAuthAuthorizeUserURL"];
string AccessTokenStr = ConfigurationManager.AppSettings["identityOAuthAccessTokenURL"];
string InvalidateTokenStr = ConfigurationManager.AppSettings["identityOAuthRequestInvalidateTokenURL"];
return new ServiceProviderDescription
{
AccessTokenEndpoint = new MessageReceivingEndpoint(AccessTokenStr, HttpDeliveryMethods.PostRequest),
RequestTokenEndpoint = new MessageReceivingEndpoint(RequestTokenStr, HttpDeliveryMethods.PostRequest),
UserAuthorizationEndpoint = new MessageReceivingEndpoint(UserAuthStr, HttpDeliveryMethods.PostRequest),
TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },
ProtocolVersion = DotNetOpenAuth.OAuth.ProtocolVersion.V10a
};
}
void GetUserProfile()
{
var tokenManager = TokenManagerFactory.GetTokenManager(TokenManagerType.InMemoryTokenManager);
tokenManager.ConsumerKey = ConfigurationManager.AppSettings["identityOAuthConsumerKey"];
tokenManager.ConsumerSecret = ConfigurationManager.AppSettings["identityOAuthConsumerSecret"];
var serviceDescription = GetServiceDescription();
var consumer = new WebConsumer(serviceDescription, tokenManager);
var result = consumer.ProcessUserAuthorization(response);
if (result != null) // It is always null
{
}
Well I checked 10 times and I am pretty sure that all URLs to create ServiceProviderDescription are correct.
Any clue?
Well
finally check your web.config app keys
add key="identityOAuthConsumerKey" value="put here correct data!!!"
add key="identityOAuthConsumerSecret" value="put here correct data!!!"
and if you use hosts file you have to put correct sitename as well
127.0.0.1 site1.host1.com
I have create a DNS request using C# and PCAP. I checked the request using the wireshark. but there are not response.
I have compared DNS request which have a response. The flags and DNS query values are same.
I cant figure out why the dns resolver is not sending the response. Please help me.
Thank you.
My packet generating method:
private Packet getPacket(string s, string d,string domain)
{
Random r = new Random();
EthernetLayer ethernetLayer =
new EthernetLayer
{
Source = new MacAddress("00:0C:29:E5:FA:36"),
Destination = new MacAddress("00:0c:29:e5:fa:36"),
EtherType = EthernetType.None, // Will be filled automatically.
};
IpV4Layer ipV4Layer =
new IpV4Layer
{
Source = new IpV4Address(s),
CurrentDestination = new IpV4Address(d),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null, // Will be filled automatically.
Identification = 123,
Options = IpV4Options.None,
Protocol = null, // Will be filled automatically.
Ttl = 100,
TypeOfService = 0,
};
UdpLayer udpLayer =
new UdpLayer
{
SourcePort =ushort.MaxValue,
DestinationPort = 53,
Checksum = null, // Will be filled automatically.
CalculateChecksumValue = true,
};
DnsLayer dnsLayer =
new DnsLayer
{
Id = ushort.Parse(r.Next(0,99999).ToString()),
IsResponse = false,
OpCode = DnsOpCode.Query,
IsAuthoritativeAnswer = false,
IsTruncated = false,
IsRecursionDesired = true,
IsRecursionAvailable = false,
FutureUse = false,
IsAuthenticData = false,
IsCheckingDisabled = false,
ResponseCode = DnsResponseCode.NoError,
Queries = new[]
{
new DnsQueryResourceRecord(new DnsDomainName("col.stc.s-msn.com"),
DnsType.A,
DnsClass.Internet),
},
Answers = null,
Authorities = null,
Additionals = null,
DomainNameCompressionMode = DnsDomainNameCompressionMode.All,
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, dnsLayer);
return builder.Build(DateTime.Now);
}
}
This is my packet sending function:
private static void performRequest(LivePacketDevice device)
{
using (PacketCommunicator communicator = device.Open(100,PacketDeviceOpenAttributes.Promiscuous,1000))
{
for (int i = 0; i < threadCount; i++)
{
Thread requester= new Thread(() =>
{
try
{
Program p = new Program();
Random r = new Random();
string resolve = resolvers[r.Next(0, resolvers.Count-1)].ToString();
communicator.SendPacket(p.getPacket(destinationIP.ToString(), resolve, domainName));
p = null;
r = null;
}
catch (Exception ex) { Console.WriteLine(ex.Message); }
});
requester.Start();
Thread.Sleep(1000);
}
}
}
I checked your "getPacket" method but have not found obvious problem, so I just tried it, of course, changed mac addresses and IP addresses, I did get response.
But your packet sending method seems wrong, what is the "DestinationIP", it should source IP, in other words, local IP address of the selected device.