Java ssh client - java

I'm trying to create a web app which will check several service status, server stats etc. I found this http://www.jcraft.com/jsch/ seems to be pretty nice ssh java implementation. But every time I log in in to server I'm prompted to confirm RSA key fingerprint like this :
How can I override this, to always confirm yes without any prompts? I want to remove the whole swing part, I want to make this without any interaction, like this example code I took from the examples available on jscraft.com :
http://www.jcraft.com/jsch/examples/Exec.java
I'm not so familiar with swing and with java in general.

public class ManoUserInfo implements UserInfo {
String passwd;
public void setPassword(String pass) {
passwd = pass;
}
#Override
public String getPassphrase() {
return null;
}
public String getPassword() {
return passwd;
}
public boolean promptPassword(String arg0) {
return true;
}
public boolean promptPassphrase(String arg0) {
return true;
}
//this method responsible for that message, so just make it return true
public boolean promptYesNo(String arg0) {
// Object[] options = {"yes", "no"};
/* int foo = JOptionPane.showOptionDialog(null,
arg0,
"Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,
null, options, options[0]);*/
return true;
}
public void showMessage(String message) {
JOptionPane.showMessageDialog(null, message);
}

You have to understand why this message pops up.
SSH is a secure service, meaning that the identity of the client and the identity of the server are guaranteed. To guarantee that you actually reached the server you want to reach (for how it is possible that you connect to the wrong server without your knowledge, google "DNS cache poisoning"), the client displays the recieved server name and fingerprint. These values identify the server. You are supposed to look if this is in fact the fingerprint you generated on the server by comparing the fingerprint through a secure channel (via telephone with the server admin, for instance).
Having said that, usual SSH clients save your decision to accept the fingerprint/server name and do not ask again. It seems that your client does not. So you either have the option to change the source code (if it's licensed under an open license) or find a way to automatically press "Yes" whenever this question pops up (this can be achieved with toolkits like Autoit 3 with a very short script).

check out http://sourceforge.net/projects/sshtools/

Related

Not getting sessin value in vaadin framework

Not getting sessin value in vaadin framework
Used below :
private void setCurrentUsername(String username){
VaadinService.getCurrentRequest().getWrappedSession().setAttribute("LOGGED_IN_AS_USER",username);
userSubMenu.setText(username);
}
public static String getCurrentUsername() {
//log.info("User:::::::::::::::::" + (String) VaadinService.getCurrentRequest().getWrappedSession().getAttribute("LOGGED_IN_AS_USER"));
return (String) VaadinService.getCurrentRequest().getWrappedSession().getAttribute("LOGGED_IN_AS_USER");
}
getting value as null when flow going to other class
You could try using VaadinSession.getCurrent().getSession() instead?
iIf you are using this for a user interface i.e a user logging in. Don't forget that when your app first builds/ when the user first enters it they will of course have a null value(including any sub pages).
Try adding a ViewChangeListener to the page that the user now enters (apologies for any errors in coding i'm not currently able to gain access to my machine) into:
#Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
this.username = VaadinSession.getCurrent().getAttribute("LOGGED_IN_AS_USER");
}

Detect user when joining chat room

so i am using the PircBot to create an IRC chat bot for twitch strictly with java. I want to add a welcome message when a user connect to my chat which i thought the onJoin() method did but sadly did not. The onJoin() method only responded when the bot itself joined a channel and not when all other users joined. Any help?
Ex: "John has joined the channel." - "Bob has joined the channel"
public void onJoin(String channel, String sender, String login, String hostname)
{
//check to see if another user joines
}
onJoin() is an abstract method, meaning you should not use it directly. Just add an implementation in your bot class.
For example, mine is
public void onJoin(String channel, String sender, String login, String hostname)
{
if (sender.equalsIgnoreCase(NICK))
sendMessage(channel, "Connected to Channel");
}
Also you should edit your post to show your code. People can't help much if they can't see what you're doing.

It is possible to determine if the OS user is administrator using Applets?

It is possible to determine if the OS user is administrator using Applets (inside a Java Applet)? How?
I need to determine whether or not the user has administrator rights, primarily for Windows but I would also get this information for Linux and OSX.
I know I can get some information via "System.getProperty (" XXX ")", but found nothing as to whether or not the user is a system administrator.
Others informations I am get via Javascript (OS, Browser and etc.)
Thanks in Advance.
Not the solution I was looking for (cross plataform), I believe it could be better ... but the solution for most cases.
private static boolean isAdminWin()
{
String groups[] = (new com.sun.security.auth.module.NTSystem()).getGroupIDs();
for (String group : groups) {
if (group.equals("S-1-5-32-544"))
return true;
}
return false;
}
public static String getOSName()
{
return System.getProperty("os.name");
}
private static boolean isAdminLinux()
{
return getUserName.equalsIgnoreCase("root");
}
public static boolean isAdmin()
{
if (getOSName().toUpperCase().startsWith("WINDOWS")){
return isAdminWin();
} else {
return isAdminLinux();
}
}
Any better solution is welcome!

JoptionPane Validation

I have a Swing GUI where I am restricting the user registration so that the username and the password cannot be the same. I am using JoptionPane for the task with the following code:
public void actionPerformed(ActionEvent e) {
String username = tuser.getText();
String password1 = pass1.getText();
String password2 = pass2.getText();
String workclass = wclass.getText();
Connection conn = null;
try {
if(username.equalsIgnoreCase(password1)) {
JOptionPane.showMessageDialog(null,
"Username and Password Cannot be the same. Click OK to Continue",
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
...
The problem is that I had to use System.exit(0); without it, the next code was getting executed. Even after the JOptionPane poped up, the registration was succeeding. I do not need the system to exit, but I need the user to be kept on the registration page after the validation. What is the best way to do this? Is there other convenient ways of doing this rather than using the JOptionPane?
Replace
System.exit(0);
with
return;
if you do not want the rest of the method to be performed
You need to place your code within endless loop, and break it upon successful result. Something like:
while(true)
{
// get input from user
if(vlaidInput) break;
}
place that next code into else part may be it works
if(username.equalsIgnoreCase(password1))
{
JOptionPane.showMessageDialog(null, "Username and Password Cannot be the same. Click OK to Continue","Error",JOptionPane.ERROR_MESSAGE);
}
else
{
//place that next code
// username and password not equals, then it will execute the code
}
First of all, it is best if the UI and business logic (in this case, validation) are separated. Have them separate sort of suggest a better way of handling interaction on its own. Thus, it makes sense to create a separate class UserValidation with method boolean isValid(). Something like this:
public class UserValidation {
private final String name;
private final String passwd;
private final String passwdRpt;
public UserValidation(final String name, final String passwd, final String passwdRpt) {
this.name = name;
this.passwd = passwd;
this.passwdRpt = passwdRpt;
}
public boolean isValid() {
// do your validation logic and return true if successful, or false otherwise
}
}
Then the action code would look like this:
public void actionPerformed(ActionEvent e) {
if (new UserValidation(tuser.getText(), pass1.getText(), pass2.getText()).isValid()) {
// do everything needed is validation passes, which should include closing of the frame of dialog used for entering credentials.
}
// else update the UI with appropriate error message -- the dialog would not close by itself and would keep prompting user for a valid entry
}
The suggested approach gives you a way to easily unit test the validation logic and use it in different situations. Please also note that if the logic in method isValid() is heavy than it should be executed by a SwingWorker. The invocation of SwingWorker is the responsibility of the action (i.e. UI) logic.

Yes/No dialog in Java ME

I'm looking for a simple solution for a yes/no dialog to use in a Java ME midlet. I'd like to use it like this but other ways are okey.
if (YesNoDialog.ask("Are you sure?") == true) {
// yes was chosen
} else {
// no was chosen
}
You need an Alert:
An alert is a screen that shows data to the user and waits for a certain period of time before proceeding to the next Displayable. An alert can contain a text string and an image. The intended use of Alert is to inform the user about errors and other exceptional conditions.
With 2 commands ("Yes"/"No" in your case):
If there are two or more Commands present on the Alert, it is automatically turned into a modal Alert, and the timeout value is always FOREVER. The Alert remains on the display until a Command is invoked.
These are built-in classes supported in MIDP 1.0 and higher. Also your code snippet will never work. Such an API would need to block the calling thread awaiting for the user to select and answer. This goes exactly in the opposite direction of the UI interaction model of MIDP, which is based in callbacks and delegation. You need to provide your own class, implementing CommandListener, and prepare your code for asynchronous execution.
Here is an (untested!) example class based on Alert:
public class MyPrompter implements CommandListener {
private Alert yesNoAlert;
private Command softKey1;
private Command softKey2;
private boolean status;
public MyPrompter() {
yesNoAlert = new Alert("Attention");
yesNoAlert.setString("Are you sure?");
softKey1 = new Command("No", Command.BACK, 1);
softKey2 = new Command("Yes", Command.OK, 1);
yesNoAlert.addCommand(softKey1);
yesNoAlert.addCommand(softKey2);
yesNoAlert.setCommandListener(this);
status = false;
}
public Displayable getDisplayable() {
return yesNoAlert;
}
public boolean getStatus() {
return status;
}
public void commandAction(Command c, Displayable d) {
status = c.getCommandType() == Command.OK;
// maybe do other stuff here. remember this is asynchronous
}
};
To use it (again, untested and on top of my head):
MyPrompter prompt = new MyPrompter();
Display.getDisplay(YOUR_MIDLET_INSTANCE).setCurrent(prompt.getDisplayable());
This code will make the prompt the current displayed form in your app, but it won't block your thread like in the example you posted. You need to continue running and wait for a commandAction invocation.
I dont have programed in Java ME, but i found in it's reference for optional packages the
Advanced Graphics and User Interface API, and it's used like the Java SE API to create these dialogs with the JOptionPane Class
int JOptionPane.showConfirmDialog(java.awt.Component parentComponent, java.lang.Object >message, java.lang.String title, int optionType)
Return could be
JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION...

Categories

Resources