Jump to content

Recommended Posts

Posted

Boas,

Alguem ja fez um codigo parecido ao que se pretende neste?

O trabalho é sobre um shell monitor em que se pretende personalizar a prompt com “@myshell>”

Depois com um comandos personalizados:

cd  [directoria]  –

exit -

listBG

![-n]

__________________________________________________________________________________

O que se pretende é :

  Listar todas as shells activas;

  O monitor de shells deve guardar todos os comandos introduzidos pelos utilizadores os

respectivos resultados produzidos (output) em todas as shells de utilização activas;

  Monitorizar em tempo real uma determinada shell de utilização;

  Mandar terminar uma shell de utilização;

  Bloquear  e  desbloquear  utilizadores  que  pretendam  utilizar  shells  de  utilização.  Os

utilizadores em lista negra (blacklist) não poderão utilizar shells de utilização

Alguem ajuda com algum codigo?

Posted

Universidade do Minho?

Eu também estou a fazer isso mas não estou a reconhecer o que tu queres fazer. Que guião é esse? Eu acabei à pouco o 5 e não vou pegar no 6 até à próxima aula (que é sexta) e já agora. Estás em LCC ou LEI?

Posted

Vou deixar algum código que fiz o semestre passado. Pelo que vi não vai chegar para o que queres, o que é bom, assim tens de trabalhar. E também não vai compilar sem uma implementação de uma lista. 😄

#include <stdio.h>		// --------------------------------------------
#include <stdlib.h>		// fluSH (flushOS Shell)
#include <string.h>		// by triton <http://www.triton.com.pt>

#include <unistd.h>		// Features:
#include <sys/types.h>	//	* Basic fork() & exec()
#include <sys/wait.h>	//	* Change working directory (cd)
#include <sys/param.h>	//	* History and pattern-based match execution
					//	* Easily extensible and understood
#include "list.h"		// --------------------------------------------

#define min(a,b) (a>b) ? b : a

#define CMD(cmd, fn) if(strcmp(parsebuf[0], cmd) == 0) { fn(); return; }
#define CMD_C(c, fn) if(parsebuf[0][0] == c) { fn(); return; }
#define IMPLEMENT_CMD(command, fn) void cmd_##command() { fn; }
#define VT(esc) "27["esc

const char VT_CLEAR[] = "2J", VT_RESET[] = "0m", VT_FG_GREEN[] = "32m";
const int MAX_HOSTNAME = 255, MAX_EXECUTE = 32; void match_cmmds();

#define MAX_BUFFER 255+1
char inputbuf[MAX_BUFFER]; 		// input buffer
char* parsebuf[MAX_BUFFER];		// parser buffer
char execbuf[MAX_BUFFER];		// exec path buffer
char wdbuf[MAXPATHLEN]; 		// working dir buffer
list history;					// history info

char help_str[] = "fluSH 0.02a by triton\n\n"
"  available commands:\n"
"    cd - change working directory\n"
"    history - lists previously typed commands\n"
"    !# - executes last issued command starting with #\n"
"    help - lists all available shell commands\n"
"    exit - exits the shell";

void get_input()
{
gethostname(execbuf, MAX_HOSTNAME);
printf("%s\n%s@%s", VT(VT_FG_GREEN), getlogin(), execbuf);
printf("%s[%s]\n $ ", VT(VT_RESET), getwd(wdbuf)); 
fflush(stdout); memset(inputbuf, '\0', MAX_BUFFER);
fgets(inputbuf, MAX_BUFFER, stdin);
inputbuf[strlen(inputbuf)-1] = '\0';	
}

void parse_input()
{
char* ptr; int i = 0;
ptr = strtok(inputbuf, " ");
while(ptr) {
	parsebuf[i++] = ptr;
	ptr = strtok(NULL, " ");
} parsebuf[i] = '\0';
}

int spawn_process(char str[])
{
pid_t pid = fork();
if(pid < 0) {
	perror("fork error");
	return -1;
} if (pid) { // parent
	int status;
	waitpid(pid, &status, 0);
	return status;
} else { 	// child
	strcpy(execbuf, "/usr/bin/"); 
	strcat(execbuf, str ? str : parsebuf[0]);
	if(execv(execbuf, parsebuf) < 0)
		perror("exec error");
	return -1;
}
}

void save_history()
{
if(!strlen(inputbuf)) return;
char* ptr = malloc(strlen(inputbuf)+1);
strcpy(ptr, inputbuf);
insert(&history, (void*) ptr);
}

IMPLEMENT_CMD(exit, exit(0))
IMPLEMENT_CMD(help, printf("%s\n", help_str))

void cmd_cd()
{
if(chdir(parsebuf[1]) < 0)
	perror("chdir error");
}

void cmd_history()
{
item* it = history.first; int i = 0;
for(; i < history.nitems-1; it = it->next)
	printf("%d %s\n", i++, (char*)it->ptr);
}

void cmd_execute()
{
item* cmd = NULL; item* it = history.first;
while(it) {
	int m = min(strlen(inputbuf+1), MAX_EXECUTE);
	if(strncmp(inputbuf+1, (char*)it->ptr, m) == 0)
		{ cmd = it; } it = it->next;
} if(!cmd) { printf("command not found in history.\n"); return; }
strcpy(inputbuf, (char*)cmd->ptr);
parse_input(); match_cmmds();
}

void match_cmmds()
{	
if(!parsebuf[0]) return;

CMD("cd", cmd_cd)
CMD("help", cmd_help)
CMD("exit", cmd_exit)
CMD("history", cmd_history)
CMD_C('!', cmd_execute)

spawn_process(NULL);
}

int main()
{
init(&history); VT100(VT_CLEAR)

while (1) {
	get_input(); 	// get input
	save_history();	// save history
	parse_input(); 	// parse input		
	match_cmmds();	// match commands
} clear(&history);

return 0;
}

<3 life

Posted

Desculpem so responder agora mas é que estive sem net.

O trabalho que me foi proposto fazer foi o seguinte:

http://img196.imageshack.us/img196/5240/trabso.gif

Obrigado a todos pelas respostas, o que so mostra que este forum é de qualidade.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.