File: shell.pl

package info (click to toggle)
thp 0.4.6-9
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 260 kB
  • sloc: perl: 1,088; sh: 154; makefile: 56
file content (123 lines) | stat: -rw-r--r-- 3,180 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use POSIX qw(strftime);

sub shell {
  $thpath = "$homedir";
  my $lsfile = "/usr/share/thpot/ls.txt";
  if ($thpath =~ m/^.*\/([^\/]+)/){
      $pathsuffix = $1;
  }
  if ($hostname =~ m/^([^\.]+)\./){
      $shortname = $1;
  }
  $prompt = "[root\@${shortname} ${pathsuffix}]# ";
  if ( $greeting ne "" ) {
  #TODO: This should check wether or not the command succeeds!
	  open(GREETING, "$greetbin|");
	  while(<GREETING>) {
		  print STDERR $_;
	  }
	  close(GREETING);
  }

  print STDERR "$prompt";

  while (my $commandline = <STDIN>) {
  
    open(LOG, ">>$sesslog");
    select LOG;
    $|=1;
    print LOG $commandline;
    chomp $commandline;
    @commandline = (split ";", $commandline);

    while (@commandline){
        $commands = shift (@commandline) ;
    
        @command=split /\s+/,($commands);

        shift @command if ($command[0] =~ /^(\s|$)/);

        if ($command[0] =~ /\buname\b/){
            if ($command[1] =~ /-[amsv]\b/){
            print STDERR $shellhash{$command[0]}{$command[1]}, "\n";
            }else{
            print STDERR $shellhash{$command[0]}{-s}, "\n";
            }

        } elsif ($command[0] =~ /\bcd\b/){
            changedir("$command[1]");  

        } elsif ($command[0] =~ /\bpwd\b/){
            print STDERR "$thpath\n";

        } elsif ($command[0] =~ /\b(whoami|w|id|wget)\b/){
            print STDERR "$shellhash{$command[0]}\n";

        } elsif ($command[0] =~ /\b(exit|logout)\b/){
            close(LOG);
            return;

        } elsif ($command[0] =~ /\bls\b/) {
		if ( -f "$lsfile" ) {
			open (LS,"<$lsfile");
			while (<LS>){
				print STDERR  $_;
			}
			close(LS);
		} else {
			$DATE = strftime "%a %b %e %H:%M:%S %Y", localtime;
			print ERRLOG "$DATE\tCannot find directory listing (in file $lsfil
				e)\n";
			print STDERR "ls: $commands[1]: No such file or directory\n;"
        }
      }
	    print STDERR "$prompt";
    }
  }
}

sub changedir{
      $elements = $_[0] or $elements = "$homedir";
      if ($elements =~ /^\/.*/){
          $elements =~ s/\///;
          @thpath = ();
      } else {
      @thpath = (split /\//, $thpath);
      }
      @elements = (split /\//, $elements);
      foreach $element (@elements){
          if ($element eq ".."){
             pop @thpath;
          } else {
             push @thpath, $element;
          }
      }
      $thpath = "/" . (join "/", @thpath);
      $thpath =~ s/^\/\//\//;
      $pathsuffix = pop @thpath;
      push @thpath, $pathsuffix;
      $pathsuffix = "/" unless $pathsuffix;
      $prompt = "[root\@$shortname $pathsuffix]# ";
  }

sub hostname{

}

%shellhash = (
	uname	=>	{
		-a	=>	"Linux localhost 2.2.17 #4 Mon Apr 7 09:04:33 EDT 2001 i686 unknown unknown GNU/Linux",
		-m 	=>	"i686",
		-s 	=>	"Linux",
		-v 	=>	"#4 Mon Apr 7 09:04:33 EDT 2001",
	},
	whoami	=>	"root",
	w	=>	  "3:32am  up  7:45, 10 users,  load average: 0.04, 0.05, 0.01
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU  WHAT",
	id	=>	"uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)",
	wget	=>	"wget: missing URL
Usage: wget [OPTION]... [URL]...

Try `wget --help' for more options.",
);