I'm coding following CGI script
echo "Content-type: text/html; charset=UTF-8\n\n"
echo "<HTML><HEAD><TITLE>title</TITLE></HEAD>"
echo "<BODY>"
echo "<FORM ACTION="http://exapmle.com/page2.cgi" NAME="PAGE1" METHOD="POST">"
echo "input:<INPUT TYPE=text NAME="data1" SIZE=10 MAXLENGTH=10>"
echo "<INPUT TYPE=submit NAME=nbtn VALUE='GO TO PAGE2'>"
echo "</FORM>"
echo "</BODY>"
echo "</HTML>"
How to get valiable in CGI made by /bin/sh + Apache
I am glad gime me sample cord
In the CGI script, you receive it as a parameter to main, so it should be in some place pointed by argv
Apache runs the cgi just like you:
script.cgi var1
And the parameters are in argv, so this:
printf("%s", argv[1]);
Will output
var1
Related
I need to run this cmd with an expect script:
echo users.1.password=`grep %user% /etc/passwd | awk -F: '{print $2}'` >> /tmp/system.cfg.new
But it errors out because of the $2 in it. How do I fix this? I need the variable to only be visible to the device I am sending the cmd to.
Here is the full script for password change on UBNT equipment via script (works via ssh, but not as script because of $2):
#!/usr/bin/expect
set timeout 30
#Edit for User
set user user
#Edit for Old Password
set old oldpassword
#Edit for New Password
set new newpassword
#get IP List from iplist.txt
set f [open "/iplist.txt"]
set data [read $f]
close $f
foreach line [split $data \n] {
if {$line eq {}} continue
spawn ssh $user#$line
expect {
"assword:" {
send "$old\r"
expect {
"assword:" {
close
continue
}}
expect {
"*" {
send "passwd $user\r"
expect "assword:"
send "$new\r"
expect "assword:"
send "$new\r"
expect "*"
send "grep -v users.1.password= /tmp/system.cfg > /tmp/system.cfg.new\r"
expect "*"
send "echo users.1.password=`grep $user /etc/passwd | awk -F: '{print $2}'` >> /tmp/system.cfg.new\r"
expect "*"
send "cp /tmp/system.cfg.new /tmp/system.cfg\r"
expect "*"
send "save && reboot\r"
close
continue
}}}}
expect {
"*" {
close
continue
}}
expect eof
}
You just have to escape the dollar sign with backslash.
send "echo users.1.password=`grep $user /etc/passwd | awk -F: '{print \$2}'` >> /tmp/system.cfg.new\r"
By the way, using expect * might be dangerous and not a recommended one unless a read need of it.
You could use the a generalized approach for the matching the prompt as,
set prompt "#|%|>|\\\$ $"
Here, we have escaped the literal dollar sign with backslash and the last dollar is meant for the end-of-line regular expression.
After sending any commands to the remote shell, you can expect for the pattern as
expect -re $prompt
I am trying to send an HTML e-mail using this code but all i am getting is FALSE from the mail() function.
The error_log is empty.
Can someone tell me why mail() is not working?
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>SDFSDF</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>VXCVSDF</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$to = 'my_mail#gmail.com';
$subject = 'Website Change Reqest';
$headers = "From: USER NAME"."\r\n";
$headers .= "Reply-To: USER EMAIL"."\r\n";
$headers .= "MIME-Version: 1.0"."\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1"."\r\n";
if (mail($to, $subject, $message, $headers)) {
echo 'Your message has been sent.';
} else {
echo 'There was a problem sending the email.';
}
It's hard to debug PHP mail() function.
After checking your script, I can confirm that your code is working fine. It's something with your server or/and PHP configuration.
Start with this little snippet to see what is happening:
error_reporting(E_ALL);
ini_set('display_errors', -1);
echo '<br>I am : ' . `whoami`.'<br>';
$result = mail('myaddress#mydomain.com','This is the test','This is a test.');
echo '<hr>Result was: ' . ( $result === FALSE ? 'FALSE' : 'TRUE') . ' ('. $result. ')';
echo '<hr>';
echo phpinfo();
After output, check your sendmail_path, in most case sendmail_path uses sendmail MTA:
/usr/sbin/sendmail -t -i
Edit your php.ini file, set the following and don't forget to restart httpd server:
sendmail_path = /usr/sbin/sendmail -t -i
Check log files at /var/log/maillog, it could really help you to solve the problem.
If you still have a problem, just take a good look at PHPMailer, SwiftMailer, PEAR's Mail or Zend Framework's Zend_Mail an excellent, comprehensive, modern PHP mailing library. It will be easy to debug your problem after all.
You can using phpmailer for this way!
https://code.google.com/a/apache-extras.org/p/phpmailer/
Hope this help!
after starting a new session in my login.php i rediredct to home.php and load session variables successfully.But when i try to open menu.php from home.php and use session variables , those same variables are unndefined now. I echoed the session id after both page changes and its exactly the same. So can anyone please tell me why am i losing these variables?
login.php:
session_start();
$_SESSION["user"]=$puser;
$_SESSION["loggedin"]=true;
echo "<script>window.open('../home.php','_self')</script>";
?>
home.php
require("php/objects.php");
session_start();
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"]==true)
{
$user=$_SESSION["user"];
$_SESSION["loggedin"]=true;
echo session_id();
echo $_SESSION["loggedin"];
}
else
{
echo "Login Error";
echo "<script>window.open('default.html','_self')</script>";
exit();
}``
?>
menu.php
require("objects.php");
session_start();
echo session_id();
echo $_SESSION["loggedin"];
/*if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"]==true)
{
$user=$_SESSION["user"];
$utype=isset($user->salary)?"staff":"client";
}
else
{
echo "Login Error";
echo "<script>window.open('../default.html','_self')</script>";
exit();
}*/``
?>
when i click a link in home.php with href=menu.php i get an error that index loggedin is undefined
I don't know what is in your objects.php, calling session should probably come before that.
session_start();
require("objects.php");
echo session_id();
echo $_SESSION["loggedin"];
I am trying to find a way to do this.
So far I have been able to access pages thanks to the cookie parameter:
curl -v --cookie cookies.txt --cookie-jar cookies.txt --user-agent Mozilla/4.0 --data "u=login&p=passwd" http://wiki/doku.php?id=start&do=login
and then
curl --cookie cookies.txt http://wiki/doku.php?id=info
To upload a file I am supposed to get the form parameter on the php page. I don't exactly know what I am looking for :
curl --form "file=#z.xml" --cookie cookies.txt "http://wiki/doku.php?id=start&tab_files=upload&do=media"
I made it work differently.
I have created a upload.php file (source)
<?php
$uploaddir = '/var/www/html/dokuwiki/upload/';
$uploadfile = $uploaddir . basename($_FILES['upload']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['upload']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
And the curl command (source)
curl --form upload=#file --form press=OK http://wiki/upload/upload.php
To finish I have allowed a few hosts in httpd.conf
<Directory "/var/www/html/dokuwiki/upload">
Order allow,deny
Allow from 192.168.1
I'm running a script on a remote server like using this command:
ssh root#host 'bash -s' < script.sh
Now I'm trying to use expect to handle the password prompt. This is the script:
#!/usr/bin/expect
set cmd [lindex $argv 0]
spawn -noecho ssh root#host $cmd
expect {
"password:" {
send "password\r"
}
}
If I run the script, it gives no output:
./ssh.exp 'bash -s' < script.sh
I know that's not the way to use ssh without password, but this is not the question right here.
UPDATE I tried the idea of glenn jackman with a simple script but it's not working. This is the script I'm using:
#!/usr/bin/expect
spawn ssh xxx#xxx
expect "*?assword:*"
send "pwd\r"
send "echo hello world"
This is the output I get:
[xxx#xxx bin]$ expect -d my.exp
expect version 5.43.0
argv[0] = expect argv[1] = -d argv[2] = my.exp
set argc 0
set argv0 "my.exp"
set argv ""
executing commands from command file my.exp
spawn ssh xxx#xxx
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {7599}
expect: does "" (spawn_id exp6) match glob pattern "*?assword:*"? no
xxx#xxx's password:
expect: does "xxx#xxx's password: " (spawn_id exp6) match glob pattern "*?assword:*"? yes
expect: set expect_out(0,string) "xxx#xxx's password: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "xxx#xxx's password: "
send: sending "pwd" to { exp6 }
send: sending "echo hello world" to { exp6 }
write() failed to write anything - will sleep(1) and retry...
UPDATE I managed it to get my script to run. This is the result which works:
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set pwd [lindex $argv 2]
spawn ssh $user#$host bash -s
expect {
"?asswor?: " {
send "$pwd\n"
}
}
while {[gets stdin line] != -1} {
send "$line\n"
}
send \004
expect {
"END_TOKEN_OF_SCRIPT" {
exit 0
}
default {
exit 1
}
}
You need to send the script you read on stdin to the remote host:
while {[gets stdin line] != -1} {
send "$line\r"
}
# then you may have to send ctrl-D to signal end of stdin
send \004
Use expect_user, as shown in the man page:
The following script reads a password, and then runs a program every hour that demands a password each time it is run. The script supplies the password so that you only have to type it once. (See the stty command which demonstrates how to turn off password echoing.)
send_user "password?\ "
expect_user -re "(.*)\n"
for {} 1 {} {
if {[fork]!=0} {sleep 3600;continue}
disconnect
spawn priv_prog
expect Password:
send "$expect_out(1,string)\r"
. . .
exit
}
Here is what I currently have, still improving it though:
#!/usr/local/bin/expect
# For debugging make the following to be line 1:
#!/usr/local/bin/expect -D 1
set timeout 20
send_user "Username?\ "
expect_user -re "(.*)\n"
set user $expect_out(1,string)
send_user "password?\ "
stty -echo
expect_user -re "(.*)\n"
stty echo
set password $expect_out(1,string)
spawn su
expect {
"Password" {send "$password\r"}
"#" {interact + return}
}