SCP in Java with URIs without password and username - java

How do I SCP a file from a RHEL linux box to another RHEL linux box without the password and without the username using URIs in Java using SCPClient? I know how to do a passwordless ssh, and I can SCP a file without the password, but I'm having trouble getting it to work without the username.
Here's my guess:
Source: file:///home/username/temp.txt
Destination: scp://#192.168.1.1:/home/username/

Have you tried scp://username#192.168.1.1:/home/username/ ?

Based off of the SCPClient page I'm going to say that it's not possible. So, the best method is to do
SCPClient scpc = new SCPClient
scpc.setRemoteHost( "192.168.1.1" );
scpc.getValidator().setHostValidationEnabled( false );
scpc.setAuthentication( "/home/username/.ssh/id_rsa", System.getProperty("user.name"), "" );
scpc.connect();
scpc.put( "file:///home/username/temp.txt", "scp://" + System.getProperty("user.name") + "#192.168.1.1:/home/username/" );
For my purpose its safe to assume the remote username is the same as the local username.

Related

How to convert Azure DB URL to localhost DB URL?

I've got an Azure SQL Server database that I'm connecting to via JDBC, but want to connect instead to my SQL Server "localhost". In SSMS, I connect to localhost without needing a password. So, do I still need to enter a password in Java?
I have a code like this :
String connectionUrl =
"jdbc:sqlserver://etcetc.database.windows.net:1433;"
+ "database=med;"
+ "user=windersan#salemimed;"
+ "password=********;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
// + "hostNameInCertificate=*.database.windows.net;"
+ "loginTimeout=30;";
How do I change this to connect instead to localhost?
Just replace the etcetc.database.windows.net by localhost and replace the port number 1433 by the number that you are using.
I have used SQLServerDataSource class to make the work easier. You can also create a string URL and set it in the DriverManger.getConnection().
Try with this code :
SQLServerDataSource dataSource = new SQLServerDataSource();
dataSource.setUser("windersan#salemimed");
dataSource.setPassword("********");
dataSource.setServerName("localhost");
// set the port number of your system below.
dataSource.setPortNumber(1433);
dataSource.setDatabaseName("med");
dataSource.setEncrypt(true);
dataSource.setHostNameInCertificate("*.database.windows.net");
dataSource.setTrustServerCertificate(false);
Connection connection = dataSource.getConnection();
Please refer to this links down below for more info.
Microsoft Docs - ISQLServerDataSource Interface - This contains the list of methods that you can use to set the various properties in the datasource.
Microsoft Docs - How to work with the connection - This contains examples of the possible ways to connect to a SQL Server database.
the first line of your concatenated string contains the url etcetc.database.windows.net:1433 this is the location of the database server, and the bit you should change.
Also, it might be worth doing a google search on connecting to SqlServer with JDBC to see if there are any examples out there.

LDAP. Java Application without Authentication

This application will be run on clients that are already authenticated in Active Directory.
Problem: the LDAP protocol (or Active Directory settings) seem to require username and password.
Goal: query Active Directory using LDAP in Java without having to authenticate (ask for username and password).
Gist: all clients who run this application have already logged in. Thus, they are already authenticated (into)/ by Active Directory.
Now that they are logged in and have access to AD outside the application, isn't it possible to "mooch" off of the fact that they are already authenticated and run my LDAP queries in my application?
Errors: while trying to maneuver past authentication; I have become accustomed to binding errors, log4j errors; and almost everything recommended on Stack Overflow, Oracle and Apache.
Methods tried: I have tried anonymous binding, Ldap api's, nada!!
Questions:
Is it possible to query Active Directory without authentication?
Is it possible to query Active Directory by telling the server that "hey, I am already logged into AD, proceed with my queries?" without prompting the user for Username and password?
Is it possible to query active directory without authentication?
I think no, you cannot as this will violate security. Another way might be to use Single sign on utilities that lets you sign in and then they will provide you the details.
Is it possible to query active directory by telling the server that
"hey, I am already logged into AD, proceed with my queries?" without
prompting the user for Username and password?
You can try http://spnego.sourceforge.net/ or http://jcifs.samba.org/src/docs/ntlmhttpauth.html to use NTLM
The following solution (or at least a very similar one) was used to solve this question:
import com4j.Variant;
import com4j.typelibs.ado20.ClassFactory;
import com4j.typelibs.ado20._Command;
import com4j.typelibs.ado20._Connection;
import com4j.typelibs.ado20._Recordset;
public static void queryADForComputers() throws Exception {
String query = "cn,sn,givenName,department";
String filter = "(&(objectclass=user)(objectcategory=person))";
String namingContext = "OU=Desktops,OU=Workstations,OU=HO,DC=win";
_Connection conn = ClassFactory.createConnection();
conn.provider("ADsDSOObject");
conn.open("Active Directory Provider","","",-1);
_Command cmd = ClassFactory.createCommand();
cmd.activeConnection(conn);
cmd.commandText("<LDAP://" + namingContext + ">;" + filter + ";" + query + ";subTree");
_Recordset rs = cmd.execute(null, Variant.getMissing(), -1);
System.out.println("Found " + rs.recordCount() + " users/computers/whatever i was looking for");
//Then here you can use a while loop while(!rs.eof())
//in which you can get each value as rs.fields().item(i).value();
//in my case, i did rs.fields().item(i).value().toString()
//or you can check for its type and go from there.
}
I worked on this a while ago and don't currently have an active directory to test and verify. but this should get you started.

System.getenv() returns null

I'm trying through a Java applet to connect to a Mysql database set up on OpenShift.
String dbHost = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
String dbPort = System.getenv("OPENSHIFT_MYSQL_DB_PORT");
both of those strings return NULL when the application is online.
If I connect through ssh to the database everything seems to be working fine, I can see the database and I can work on it.
What am I missing?
1) what does echo $OPENSHIFT_MYSQL_DB_HOST give if you connect via ssh? You might use this as a default (for starters)
dbHost = System.getenv("OPENSHIFT_MYSQL_DB_HOST") != null
System.getenv("OPENSHIFT_MYSQL_DB_HOST") : "defaultvalue";
2) try to export OPENSHIFT_MYSQL_DB_HOST=yourhostname

Check if FTP Server supports MLSD

I am using apache.commons.net.FTPClient to get some files from customers but I found that there is a ftpserver that doesn't process the command list as it should because it always return the current directory instead of the one that I indicate.
I found out that mlistFile (MLSD) works great on that machine!
My question is:
Is there any way to find out with apache.commons.net if there I should use the MLDS or LIST commnad?
Any flag or something like that?
Thanks
Here is what I found (maybe too late?): you can invoke the function listHelp() on a FTPClient object to get the commands recognized by the remote server. You get an output such as
214-The following commands are recognized.
ABOR ACCT ALLO APPE CDUP CWD DELE EPRT EPSV FEAT HELP LIST MDTM MKD
MODE NLST NOOP OPTS PASS PASV PORT PWD QUIT REIN REST RETR RMD RNFR
RNTO SITE SIZE SMNT STAT STOR STOU STRU SYST TYPE USER XCUP XCWD XMKD
XPWD XRMD
You just have to check if MLSD is in the list.
You can use "FEAT" command.
It shows commands you can use.

What is the easiest way to create a HostAndPort-Instance if the hostname is "localhost"?

I want to use the class HostAndPort from guava, to store a host and a port. What is the easiest way to create a valid HostAndPort-Instance, if the host is "localhost" and not "127.0.0.1" ?
It tried HostSpecifier.isValid(String) to validate the host, before i create a HostAndPort-Instance, but it returns false for "localhost". So in my case i can't use HostSpecifier, except i transform "localhost" to "127.0.0.1".
Is there an other way to validate a host name without a DNS-lookup?
java.net.InetAddress.getByName(String hostname) is the one. http://docs.oracle.com/javase/6/docs/api/java/net/InetAddress.html
The local address lookups are done via lmhosts on windows if enabled and on linux/unix using name service switch config(/etc/nsswitch.conf) in order to check where to lookup first - files,dns,nis.... and so on (man nsswitch.conf). The java api call will resolve it depending on a system configuration.
EDIT:
you probably want to take a look at this library too
http://www.xbill.org/dnsjava/
hope this helps abit.

Categories

Resources