bash 密码输入

bash 密码输入

写方法解决linux下C语言没有getch函数问题


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    printf("方法一:写方法实现getch不回显、不回车\n");
    char str[5] = "";
    int i = 0;
    int strlen = sizeof(str);
    printf("input passwd:");
    system("stty -echo");//关闭回显
    system("stty -icanon");//不需回车
    for(i = 0;i < strlen;++i)
    {
        str[i] = getchar();
    }//该收的都收了
    system("stty icanon");//需要回车
    system("stty echo");//打开回显
    printf("\npasswd:%s\n",str);

    printf("方法二:库函数getpass实现关闭回显、需回车\n");
    char *passwd;
    passwd = getpass("input passwd:");
    printf("passwd:%s\n",passwd);
    return 0;
}

这里写图片描述


#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <termios.h>

int my_getch1();
int my_getch2();
void getPassword(char *pcPWD);

int main()
{
    printf("请输入密码:");
    char temp[20] = "";
    getPassword(temp);
    printf("\n");   
    printf("%s\n",temp);    
    return 0;
}


/*****************************************
 * 功能:设置终端为不回显不缓冲,并获取按键的键值
 * 返回值:按键的键值int型
 * 参数:无参数
 ****************************************/
int my_getch1()
{
        struct termios tm, tm_old;
        int fd = STDIN_FILENO, c;
        if(tcgetattr(fd, &tm) < 0)  //获取终端属性
        {
            return -1;
        }
        tm_old = tm;
        cfmakeraw(&tm); //就是将终端设置为原始模式,该模式下所有的输入数据以字节为单位被处理。
                        //在原始模式下,终端是不可回显的,而且所有特定的终端输入/输出模式不可用。
        if(tcsetattr(fd, TCSANOW, &tm) < 0) //不等数据传输完毕就立即改变属性   成功返回0   失败-1
        {
            return -1;
        }
        c = fgetc(stdin);//
        if(tcsetattr(fd, TCSANOW, &tm_old) < 0)
        {
            return -1;
        }
        return c;
}
/*****************************************
 * 功能:调用getch获取键值保存到pcPWD中并单个回显*
 *      可以分别backspace和回车
 *      用于获取密码
 * 返回值:无返回值
 * 参数:字符数组指针pcPWD
 ****************************************/
int my_getch2()
{
    system("stty -echo");
    system("stty -icanon");
    int ch = (int)getchar();
    system("stty icanon");
    system("stty echo");
    return ch;
}
void getPassword(char *pcPWD)
{
    int ch=0;
    int i=0;
    for(i=0;i<20;)
    {
        //ch = my_getch1()
        ch = my_getch2();
        if(ch==-1)
        {
            continue;
        }
        if((ch == 127 || ch == 8) && i>0)
        {
            putchar('\b');
            putchar(' ');
            putchar('\b');
            i--;
        }
        else if(ch==10 || ch == 13)
        {
            pcPWD[i]=0;
            putchar('\n');
            return;
        }
        else if(i==0&&(ch==127 ||ch == 8))
        {
            printf("\a");
        }
        else if(i<19)
        {
            pcPWD[i]=(char)ch;
            putchar('*');
            i++;
        }
        else
        {
            printf("\a");
        }
    }
}

效果图

### Git Bash 密码管理 在处理Git操作时,“密码”的概念主要涉及远程仓库的身份验证,而非Git本身或Git Bash环境的登录凭证。当提到修改或重置与Git Bash关联的密码时,实际上是指更新用于访问远程仓库(如GitHub、Gitee等)的认证信息。 对于通过HTTPS方式克隆下来的项目,在执行推送或其他网络相关指令时遇到`fatal: unable to access 'https://...': The requested URL returned error: 403`这样的权限拒绝错误[^4],表明当前存储于本地缓存中的用户名和密码可能不再有效或是针对不同账户的信息。此时可以通过以下几种方法来解决这个问题: #### 方法一:清除并重新输入凭据 Windows操作系统中,默认情况下会有一个叫做“Credential Manager”的组件负责管理和保存HTTP(S)协议下的身份验证数据。如果想要更改已有的凭据,则可以按照如下路径找到对应的条目进行删除: - 控制面板 -> 用户帐户 -> 凭证管理器 - 或者直接搜索 "credential manager" 之后再次尝试推送到远程服务器时会被提示重新录入新的用户名和密码。 #### 方法二:配置全局SSH密钥 为了避免频繁地手动输入密码,推荐使用SSH方式进行版本库间的交互。这需要先生成一对公私钥对,并将公钥添加至目标平台上的个人设置里(例如 GitHub 的 SSH and GPG keys 页面)。具体步骤如下所示: 1. 检查是否存在已有SSH密钥对;如果没有则创建一个新的; ```bash ls -al ~/.ssh ssh-keygen -t rsa -b 4096 -C "[email protected]" ``` 2. 将新产生的id_rsa.pub文件里的内容复制粘贴到对应网站的安全选项内; 3. 更新现有项目的远程URL地址为SSH格式; ```bash git remote set-url origin [email protected]:username/repo.git ``` 以上两种方案都可以有效地帮助用户完成对旧有凭据信息的替换工作,从而恢复正常的服务调用流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值