Jump to content

Recommended Posts

Posted

Lesson 5

 

Executing system commands

 

Perl provides a function called “system” which canexecute a command or a set of commands directly on the operatingsystem. In fact, Perl passes the command to the operating system, whichexecutes it, and then returns the result back to Perl.

 

So, for instance, the following Perl script prints the content of the current directory:

 

#!/usr/bin/perl

 

system("ls");

 

What actually happens is that the Unix process which runs the Perlinterpreter forks and the newly created child Unix process executes the“ls” command. When the execution finishes, it returns theexit code of the command back to the Perl interpreter.

 

If you are familiar with Unix commands exit codes you can test thesuccess of the execution of your command by assigning the return valueof “system” to a variable, and then evaluate this variable.For instance:

 

$lsExecutedSuccessfully = system(“ls”);

 

Here, if “ls” executes successfully, the variable$lsExecutedSuccessfully receives the value 0. This value corresponds tothe successful exit code of the command “ls”.

Executing system commands and capturing the output

 

Sometimes, when you run a Linux command from your Perl script you'remore interested in what it writes on the screen than in its exit code.For instance, when you execute “ls” you're more likely tobe interested in the list of files being printed on the screen than inthe value 0 returned by “system”.

 

To achieve this, you can use evaluated quotes “`”instead of “system”. Not only does it executes the command,but it also returns what the commands writes in its standard output:

 

@files = `ls`;

 

In this example, the listing of the files returned by“ls” does not appear on the screen. Instead, it gets storedin the @files array.

Changing the working directory

 

In the shell you would type "cd /home" to change the workingdirectory to /home. You could write the following in your Perl script:

 

system("cd /home");

 

But it would have no effect. In fact, since the system call forksfrom the process used by the interpreter, it doesn't change the workingdirectory of your script. Instead, you can use a Perl function called"chdir":

 

chdir "/home";

Interacting with the filesystem

 

Perl provides a lot of functions to interact with the files anddirectories of your filesystem. Here are some of these handy functions:

chmod

 

"chmod" changes the permissions of a file or a list of files andreturns the number of files that were changed. The first argument mustbe the numerical mode. Examples:

 

chmod 0777, "/home/clem/program.pl";

 

chmod 0777, @myFiles;

symlink

 

"symlink" creates a symbolic link. It is the equivalent of "ln -s".The first argument is the file name, the second argument is the linkname. Example:

 

symlink "/home/clem/program.pl", "/usr/bin/program";

mkdir

 

"mkdir" creates a directory. The first argument is the name of thedirectory and the second argument is the octal mode which defines thepermissions for that directory. For example:

 

mkdir "/home/clem/perl_programs", 0664;

rename

 

"rename" is the equivalent of "mv" in Unix. It renames or moves a file. Example:

 

rename "/home/clem/program.pl", "/home/clem/program";

rmdir

 

"rmdir" deletes a directory, but only if it is empty. Example:

 

rmdir "/home/clem/perl_programs";

stat

 

"stat" returns a 13-element array which represent the properties of a file. The elements of the array are :

 

0: $dev, 1: $ino, 2: $mode, 3: $nlink, 4: $uid, 5: $gid, 6: $rdev,7: $size, 8: $atime, 9: $mtime, 10: $ctime, 11: $blksize, 12: $blocks

 

Example:

 

stat "/home/clem/program.pl";

unlink

 

"unlink" deletes a file or a list of files. Example:

 

unlink "/home/clem/program.pl";

Perl Script Exercise: Netswitch

 

In this exercise, we want to be able to switch between networks. Wedefined network configuration files in a directory called "networks".

 

For instance, here is the content of ./networks/home:

 

interface=eth2

 

type=dhcp

 

proxy=none

 

And here is the content of ./networks/work:

 

interface=eth1

 

type=dhcp

 

proxy=www-proxy.work.com

 

proxy_port=8080

 

The following Perl script takes a network name as its command lineargument, opens the file with the same name from ./networks and setsthe network interface with the data taken from the content of that file:

 

#!/usr/bin/perl

 

#default values

 

$interface="none";

 

$type="none";

 

$address="none";

 

$gateway="none";

 

$dns="none";

 

$proxy="none";

 

$proxy_port = "none";

 

#gather information from the network file

 

$network = $1;

 

$networkFile = "./networks/$network";

 

open (NETWORK, "$networkFile") or die "$networkFile not found or not readable\n";

 

while ($line = ) {

 

chomp $line;

 

($variable, $value) = split /=/, $line;

 

if ($variable eq "interface") $interface = $value;

 

elsif ($variable eq "type") $type = $value;

 

elsif ($variable eq "address") $address = $value;

 

elsif ($variable eq "gateway") $gateway = $value;

 

elsif ($variable eq "dns") $dns = $value;

 

elsif ($variable eq "proxy") $proxy = $value;

 

elsif ($variable eq "proxy_port") $proxy_port = $value;

 

}

 

#make sure the type and interface are defined

 

if ($interface eq "none") die "Interface name not defined\n";

 

if ($type eq "none") die "Network type (dhcp, static) not define\n";

 

if ($type eq "dhcp") {

 

print "Network type: dhcp\n";

 

#just get an IP address and settings from the DHCP Server

 

system("dhclient");

 

}

 

elsif ($type eq "static") {

 

print "Network type: static\n";

 

#bring the interface up

 

if ($address eq "none") die ("IP address not defined\n");

 

system("ifconfig $interface $address up");

 

if ($gateway ne "none") {

 

print "Gateway: $gateway\n";

 

system("route add default gw $gateway");

 

}

 

if ($dns ne "none") {

 

print "DNS Server: $dns\n";

 

$strNameServer = "cat "."'"."nameserver $dns"."' > /etc/resolv.conf";

 

system($strNameServer);

 

}

 

}

 

else die "Bad network type : $type. Use dhcp or static.\n";

 

Try to understand each line of that script. The script doesn't setthe proxy in APT, Firefox...etc. See if you can update the script toadd such functionality. Also, it would be great if the script couldlist the possible networks available when the user types "netswitch-l". As there are many ways to solve a problem, especially in Perl,please post your solutions and ideas. Together you should be able towrite a great network switcher.

 

You now know all you need to start writing this script, however if you have any questions do not hesitate to ask.

 

 

i hope i take +1 karma for my job..

 

oh and sry if all these exist.. i try to search but i didnt find anything

 

with love ProJecT

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Posts

    • SocNet is looking for new suppliers! — Snapchat accounts  — Reddit accounts with karma  — LinkedIn accounts with real connections Message us or contact support — let’s discuss terms! We’re also open to other partnership opportunities. Our Online Store Offers: ➡ Accounts: Telegram, Facebook, Reddit, Twitter (X), Instagram, YouTube, TikTok, Discord, VK, LinkedIn, GitHub, Snapchat, Gmail, Emails (Outlook, Firstmail, Rambler, Onet, Gazeta, GMX, Yahoo, Proton, Web.de), Google Voice, Google Ads ➡ Premium Subscriptions: Telegram Premium, Twitter Premium X, YouTube Premium, Spotify Premium, Netflix Premium, Discord Nitro, ChatGPT Plus/PRO, XBOX Game Pass ➡ Additional Services: Telegram Stars, Proxies (IPv4, IPv6, ISP, Mobile), VPN (Outline, WireGuard, etc.), VDS/RDP Servers SMM Panel Services: ➡ Use our SMM Panel to boost Facebook, Instagram, Telegram, Spotify, Soundcloud, YouTube, Reddit, Threads, Kick, Discord, LinkedIn, Likee, VK, Twitch, Kwai, Reddit, website traffic, TikTok, TrustPilot, Apple Music, Tripadvisor, Snapchat, and more. Promo code for store discount: PARTNER8 (8% off) Get $1 trial balance for the SMM Panel: Submit a ticket titled “Get Trial Bonus” via our website (Support) ➡ Go to the SMM Panel (clickable) or contact us through the bot Our Key Products: ➡ Online Store: Click ➡ Telegram Shop Bot: Click ( ENGLISH Version: Click ) ➡ SMM Panel: Click Payments accepted: Bank cards · Crypto · Other popular methods Returning buyers receive extra discounts and promo codes! Support: ➡ Telegram: https://t.me/solomon_bog ➡ Discord: https://discord.gg/y9AStFFsrh ➡ WhatsApp: https://wa.me/79051904467 ➡ Email: solomonbog@socnet.store ➡ Telegram Channel: https://t.me/accsforyou_shop You can also use these contacts to: — Discuss bulk orders — Form partnerships (Current partners: https://socnet.bgng.io/partners ) — Become our supplier SocNet — your digital goods & subscription store
    • SocNet is looking for new suppliers! — Snapchat accounts  — Reddit accounts with karma  — LinkedIn accounts with real connections Message us or contact support — let’s discuss terms! We’re also open to other partnership opportunities. Our Online Store Offers: ➡ Accounts: Telegram, Facebook, Reddit, Twitter (X), Instagram, YouTube, TikTok, Discord, VK, LinkedIn, GitHub, Snapchat, Gmail, Emails (Outlook, Firstmail, Rambler, Onet, Gazeta, GMX, Yahoo, Proton, Web.de), Google Voice, Google Ads ➡ Premium Subscriptions: Telegram Premium, Twitter Premium X, YouTube Premium, Spotify Premium, Netflix Premium, Discord Nitro, ChatGPT Plus/PRO, XBOX Game Pass ➡ Additional Services: Telegram Stars, Proxies (IPv4, IPv6, ISP, Mobile), VPN (Outline, WireGuard, etc.), VDS/RDP Servers SMM Panel Services: ➡ Use our SMM Panel to boost Facebook, Instagram, Telegram, Spotify, Soundcloud, YouTube, Reddit, Threads, Kick, Discord, LinkedIn, Likee, VK, Twitch, Kwai, Reddit, website traffic, TikTok, TrustPilot, Apple Music, Tripadvisor, Snapchat, and more. Promo code for store discount: PARTNER8 (8% off) Get $1 trial balance for the SMM Panel: Submit a ticket titled “Get Trial Bonus” via our website (Support) ➡ Go to the SMM Panel (clickable) or contact us through the bot Our Key Products: ➡ Online Store: Click ➡ Telegram Shop Bot: Click ( ENGLISH Version: Click ) ➡ SMM Panel: Click Payments accepted: Bank cards · Crypto · Other popular methods Returning buyers receive extra discounts and promo codes! Support: ➡ Telegram: https://t.me/solomon_bog ➡ Discord: https://discord.gg/y9AStFFsrh ➡ WhatsApp: https://wa.me/79051904467 ➡ Email: solomonbog@socnet.store ➡ Telegram Channel: https://t.me/accsforyou_shop You can also use these contacts to: — Discuss bulk orders — Form partnerships (Current partners: https://socnet.bgng.io/partners ) — Become our supplier SocNet — your digital goods & subscription store
    • The links are down, why do you publish something that you're just going to delete later?
    • HELLO EVERYONE. WE ARE SELLING A LOT OF ADENA ON L2 REBORN DISCORD - GODDARDSHOP   HURRY TO BUY OR YOU MAY NOT MAKE IT!!!
  • Topics

×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock