SoFunction
Updated on 2025-04-14

Perl program with only one line page 2/3


===========================================================
This is just a cat...it's too simple -- although its process seems so complicated, perl has done a lot for us silently
thing. Let's make it more.
##
# perl -pe 's/vi/emacs/g;' /etc/hosts
127.0.0.1 localhost
192.168.0.3 emacs
##
In this line, we use perl to complete some things that should be within sed's duties. We specify a regular expression to put /etc/hosts
The "vi" in the file is replaced with "emacs". Of course, even if you replace it with hundreds of files, each file contains thousands of strings, its efficiency is
It's the same high. We just need to write the appropriate regular expression based on our own, and then match it with m/// or replace it with s///, and the work will be completed.
Such commands are perfect for processing string replacement in large batches of files, such as processing .c or .html text, or
It is log analysis, etc.
However, this command does not really modify the file. It just executes a streaming match operation similar to sed and outputs the text to the terminal; perhaps
We can use > to perform shell redirection to complete our operations, but in perl, there is an easier way.
##
# perl - -pe 's/vi/emacs/g;' /etc/hosts
# ls -alF /etc/hosts*
-rw-r--r-- 1 root root 167 Feb 2 11:11 /etc/hosts
-rw-r--r-- 1 root root 164 Feb 2 05:25 /etc/
# cat /etc/hosts
127.0.0.1 localhost
192.168.0.3 emacs
##
In the above command sequence, we use a new parameter -i of perl, which function is to directly complete the operation of the file and automatically backup it
Original file! The above command successfully modified "vi" in /etc/hosts to "emacs", and backed up the original file as /etc/.
Here is another useful command:
# perl - -e 's/^(\s+)?(telent|shell|login|exec)/# $2/' /etc/
This command will directly comment out the telnet, shell, login, and exec services in /etc/. Of course, you must execute killall -HUP inetd
It will take effect only after that. Deform it slightly:
# perl - -e 's/(^[^#])/# $1/g;' /etc/
This will prefix all uncommented lines in the specified file, which corresponds to all subservices of inetd that are closed.
4. Mmmm....
===========================================================
In a line of perl, we can also use -M to specify the many extension module names of perl, which greatly expands the functions we can use.
for example,
##
C:\>perl -MWin32 -e "print Win32::DomainName"
FREEDEMON
C:\>perl -MWin32 -e "print Win32::NodeName"
VI
C:\>perl -MWin32 -e "print Win32::LoginName"
elly
##
In this command, we use -M to specify the Win32 extension of perl, and then we can directly call these extensions using the global function name
Module now. In this example, the domain name, host name and current user name on the current Windows host are output respectively.
##
C:\>perl -MWin32API::Net -e "Win32API::Net::UserEnum('127.0.0.1',\@U);print @U;"
ACTUserASPNETellyGuestIUSR_VIIWAM_VISQLDebuggerSUPPORT_388945a0__vmware_user__
##
This item will call the UserEnum function in the Win32API::Net module to enumerate and output all user names on the current system, but unfortunately, it is not
Exposed a small weakness of perl in Windows, that is, when Perl uses -e to execute command line statements on Windows, it can only recognize
Don't ""Double quotes extending strings without identifying them", which means that our Perl program in Windows will not be able to use variables.
Output and meaning-transforming characters, etc... The most direct consequence is that you will never see the line break.
5. The last example
===========================================================
Perl is a good helper for system management. The main aspects of system management include resource management, user management, service management, etc. In terms of user management
There are always problems that give us headaches, such as someone who doesn't set a password, or the password is too simple... There were news reports a few days ago, and I just caught two
The "hacker" actually found someone else's bank account and logged directly to the CCB's online banking page... manually guessed the password... and it's true
Several individuals were guessed and then their deposits were transferred. In any case, it is the system to ensure that all users have a strong enough password on our system.
The first door to safety.
The early Unix passwords were stored in /etc/passwd after 40 or 56-bit DES encryption. Later, shadow appeared, and later each
This system also has its own encryption method, such as blowfish of netbsd or MD5 of freebsd, and the password files are stored in their own locations.
It's different. Password verification can be done with many tools, such as the famous Crack or John Rip, and even Crack written in pure perl
The program, but in fact the simplest thing is:
##
#perl -e '""eq$c&&print"$u = [null]\n"while($u,$c)=getpwent'
pcap = [null]
##Find empty password users
The above sentence will find all users with empty passwords in the current system. Of course, the root identity must be run to be valid.
##
# perl -e 'crypt($u,$c)eq$c&&print"$u = $u\n"while($u,$c)=getpwent'
## Find users with [username=password]
Improve it so that you can find users with the same username and password.
Expand the program like this:
==
#!/usr/bin/perl
while(($u,$c)=getpwent()){
if(crypt($u,$c) eq $c){
print "$u = $u\n";
}
}
Previous page123Next pageRead the full text