• Post Reply Bookmark Topic Watch Topic
  • New Topic

Restriction on input with System.in  RSS feed

 
lalitkumar suthar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a program which takes inputstream System.in to read from user.

input from user is given to a remote server using JSCH for shell.

the problem is that, this approach allows user to input anything (any command on server), but i want to restrict user to few known commands only

can anyone assist somehow to achieve this
 
Deepak Rao
Ranch Hand
Posts: 36
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lalitkumar

You can check this programatically as soon as you have got the input.
For example you can put the valid commands into a hash and check if the input is valid against it. If not then display a message that it is not valid.
Or if the commands are few, then you can even print them as options and the user can select the command.

Thanks
Deepak
 
lalitkumar suthar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks deepak for reply

thats what is to be done but how do i do it
following is the code for assigning standard inputstream to shell channel

channel.setInputStream(System.in);

i need to put conditions on this input stream if the command entered shall be given to this function or not
 
Deepak Rao
Ranch Hand
Posts: 36
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lalitkumar,

There are many ways to do this. These are the steps you could follow.

1. Get the string from user ; say inputedString //through bufferreader System.in
2. Check if the inputedString is a valid command// maybe by using if - else , or comparing it against a list/hash containing all the commands
3. If the inputedString is a command pass it to the channel.// if it accepts string pass the string. Or if it accepts a stream convert using
new ByteArrayInputStream(inputedString.getBytes());
4. Else display that it is not a valid string asking the user to enter a valid command.

Thanks
Deepak
 
lalitkumar suthar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepak,

i had tried this approach

e.g.

channel.setInputStream(new ByteArrayInputStream("cd cfg/tsf\n".getBytes("UTF-8")));
channel.setInputStream(new ByteArrayInputStream("cd abhishek\n".getBytes("UTF-8")));


but the problem is that channel accepts only the last inputstream. for above case, only "cd abhishek" get executed and not both the commands.

so what we need is like a inputstream which we can check before passing to channel


once again thanks for your time & reply
 
Deepak Rao
Ranch Hand
Posts: 36
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But are'nt they 2 seperate commands?
This check will happen for each command.

There is one more thing you can try? Using Scanner. It takes the input stream as input and methods like next() and hasNext() etc. Its possible to run regex on these.



Scanner sc = new Scanner(System.in);
sc.hasNext("command");

Thanks
Deepak
 
lalitkumar suthar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepak,

user may give any number of commands.

but the problen is inputstream to channel can be assigned only once, previously assigned stream is ignored as explained in previous reply
 
lalitkumar suthar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone please suggest
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!