#include <winsock2.h> // 因为要使用socket,所以需要包含socket2.1头文件
#include "mysql++.h" // Mysql++头文件
#include <string>
#include <iostream>
using namespace mysqlpp;
using namespace std;
const DWORD SpaceTime = 20 * 60 * 1000; // 20分钟
const int LEN_NAME = 8;
const char CCH[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 随机字串字典
// 简单的随机字串生成函数
char* rand_password( char* str, int len )
{
int i = 0, n = 0;
int nLength = strlen( CCH );
for ( i = 0, n = 0; i < LEN_NAME && n < nLength; ++i, ++n )
{
int x = rand() % ( nLength - 1 );
str[i] = CCH[x];
}
str[ i + 1 ] = ' ';
return str;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "VPN_Modify_MySql_User_Password Tool" << std::endl;
std::cout << "Copyright (C) eliteYang" << std::endl;
std::cout << "http://www.cppfans.org" << std::endl;
mysqlpp::Connection _vpnConn;
// 设置数据库编码
_vpnConn.set_option( new mysqlpp::SetCharsetNameOption( "utf8" ) );
string dbIp, dbUserName, dbPwd, dbName;
cout << "nnnMySql DataBase Info Input" << endl;
cout << "请输入MySQL服务器IP地址 : ";
cin >> dbIp;
cout << "请输入MySql管理员账号 : ";
cin >> dbUserName;
cout << "请输入MySql管理员账号密码 : ";
cin >> dbPwd;
cout << "请输入MySql数据库名 : ";
cin >> dbName;
cout << " 默认端口号为 3306 " << endl;
bool bConnect = _vpnConn.connect( dbName.c_str(), dbIp.c_str(), dbUserName.c_str(), dbPwd.c_str(), 3306 );
if ( !bConnect )
{
cout << "VPN Connect mysql failed! Error: " << _vpnConn.error() << endl;
system( "Pause" );
return -1;
}
else
{
cout << "VPN Connect mysql success!" << endl;
}
DWORD timeSlot = timeGetTime();
char szChar[ 256 ] = { 0 };
string strName = "123";
cout << "请输入需要定时修改密码的用户名 :";
cin >> strName;
sprintf_s( szChar, "select * from vpn_user_info where UserName=%s", strName.c_str() );
while ( true )
{
if ( timeGetTime() - timeSlot < SpaceTime )
{ continue; }
try
{
mysqlpp::Query _query = _vpnConn.query( szChar );
mysqlpp::StoreQueryResult _result = _query.store();
if ( _result.num_rows() != 1 )
{
cout << "UserName[123] repeat, please check" << endl;
timeSlot = timeGetTime();
continue;
}
string strUserName = _result[0][0].c_str();
string strUserPassword = _result[0][1].c_str();
cout << "CurentInfo UserName[ " << strUserName.c_str() << " ] Password[ " << strUserPassword.c_str() << " ]" << endl;
char strTemp[ LEN_NAME + 1 ] = { 0 };
strUserPassword = rand_password( strTemp, LEN_NAME );
char szTemp[ 256 ] = { 0 };
sprintf_s( szTemp, "UPDATE vpn_user_info SET Password = '%s' WHERE UserName = '%s';", strUserPassword.c_str(), strUserName.c_str() );
_query << szTemp << endl;
_query.execute();
cout << "ModifyUserInfo UserName[ " << strUserName.c_str() << " ] Password[ " << strUserPassword.c_str() << " ]" << endl;
}
catch (const mysqlpp::BadQuery& er)
{
// Handle any query errors
cerr << "Query error: " << er.what() << endl;
return -1;
}
catch (const mysqlpp::BadConversion& er)
{
// Handle bad conversions
cerr << "Conversion error: " << er.what() << endl <<
"tretrieved data size: " << er.retrieved <<
", actual size: " << er.actual_size << endl;
return -1;
}
catch (const mysqlpp::Exception& er)
{
// Catch-all for any other MySQL++ exceptions
cerr << "Error: " << er.what() << endl;
return -1;
}
}
_vpnConn.disconnect();
system( "Pause" );
return 0;
}
声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。